From WebView Errors to ImageView Cover-ups to Screen Orientation

It all started with a simple WebView within an Android app.

This view loads info from a public URL. Nothing unusual there.

However ... if there should be a network interruption, you can easily end up with an error displayed prominently in the WebView.

Well, that's no good, right? So I searched the docs and elsewhere for a way to suppress this, or trap it ... and found no leads. With that, I inquired on #android-dev on IRC: I know about WebView.onReceivedError, but can the visual portion be suppressed? (Instead, I wanted to display a Dialog warning of said network problem, with an option to retry.)

Answer: You can't. :(

Very well. Time for plan B. Perhaps an ImageView might fill the bill?

Should there be any kind of WebView error, the ImageView can be used as a makeshift curtain. It overlays the WebView at the start (before the URL is loaded), then it fades to reveal the WebView behind it. If the WebView works, wonderful! But if onReceivedError kicks in, we put the curtain back up immediately and show the aforementioned dialog. Once WebView gets an onPageFinished, the curtain fades once more.

... and THIS ... works WONDERFULLY!

Until I was asked to support screen orientation changes. (Foreshadowing: "Things went downhill from here.")

Up until now the app was using android:screenOrientation="portrait" in the Manifest's main activity. So I removed that part, and of course we ran into the infamous "activity is being restarted" issue, which blows away and then brings back the WebView.

Of course it's not an issue per se. This is Android OS working as designed.

Also, we want the ImageView to display properly depending on orientation. Thankfully, we have alternate resources we can leverage! Meanwhile, we still have this WebView we don't want to reload. We want it to be left alone and let the view do its own thing, be it in portrait or landscape at the time.

I found a few well-explained ways to handle screen orientation, plus I have already read through author Mark Murphy's eBooks (highly recommended reading). Yet I was still stuck! With the ImageView now in the mix, none of the techniques seem to fill the bill:

  1. Forcing portrait mode is, of course, out of the question (or is it?).

  2. android:configChanges="keyboardHidden|orientation" and onConfigurationChanged seem to take care of the WebView, which blissfully handles the screen resizing gracefully on its own. Of course, the ImageView doesn't get the memo either, and it keeps using its original (portrait/landscape) resource!

    An ill-conceived call to activity.setRequestedOrientation(newConfig.orientation) within the config change handler didn't help matters.

  3. Since the WebView doesn't need to be persisted between app executions, onRetainNonConfigurationInstance looked like another possibility. Alas, that didn't seem to work either. Resetting webview with the last nonconfig instance (since that's all I really passed in) resulted in a blank WebView. Not quite what I was after.

    On the other hand, the ImageView seemed to responding nicely to orientation changes. Go figure.

  4. How about onSaveInstanceState? Same gotchas. WebView has a saveState method, but it doesn't seem to be saving it at a level deep enough to keep the transition seamless. If so, then it's probably not supposed to either, so this might be the wrong way to go too.

As to the wisdom of using configChanges, the documentation recommends this very technique, in fact:

Standard behavior for an Activity is to be destroyed and recreated when the devices orientation is changed. This will cause the WebView to reload the current page. If you don't want that, you can set your Activity to handle the orientation and keyboardHidden changes, and then just leave the WebView alone. It'll automatically re-orient itself as appropriate.

Of course I could also be committing some sort of pilot error here, but I have yet to figure out what that might be.

I have read and re-read the documentation and tried every which way to hack through this on my own. Perhaps I'm missing something (obvious?) in the docs, or the solution is non-obvious (but someone can shed light) ... or this simply isn't possible. Hoping it's not the latter.

Clues welcome/appreciated!

Asked by: First Name504 | Posted: 25-01-2022

Answer 1

Glad you like the books!

In the chapter on rotation, where I suspect you got many of the approaches you tried, you'll note that the onConfigurationChanged() flavor calls setContentView() again, forcibly reloading the layout and therefore getting the proper resource. You, of course, don't want to do that per se, because that'd blow away your WebView.

However, I would expect that a call to setImageResource() in onConfigurationChanged(), for your ImageView, should pull in the proper version of your "curtain". Use that to "touch up" your image to the proper one, and see if that helps.

Answered by: Clark589 | Posted: 26-02-2022

Answer 2

Did you try manipulating your Android Manifest file.

Instructions -->

Answered by: Chester164 | Posted: 26-02-2022

Similar questions

prevent imageView reloading when change of orientation in android

I read somewhere in this forum that android starts the activity all over again after changing the orientation of the phone, is it the case? if so, is there anyway that I could prevent it, there are some images in my activities that is downloaded from the internet when the activity starts, I only want them to download once. Or do I need a cache for all of the bitmaps? By the way, it turns out that ListView would not be relo...

android - ImageView becomes blank on Screen orientation change

I am using the below line to set my ImageView. Bitmap bm = Media.getBitmap(getContentResolver(), capturedImage); Bitmap bm1=Bitmap.createScaledBitmap(bm, 300, 300,true); pic.setImageBitmap(bm1); But when I rotate my mobile, the imageView becomes blank. Can anyone help me. Any help is appreciated.

android - Changing image in ImageView on orientation change does not work

I have activity with android:configChanges="orientation|keyboardHidden" I am trying to change image in ImageView when the device orientation changes with: public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // refresh the instructions image ImageView instructions = (ImageView) findViewById(; instructions.setIm...

android - Get width and height of ImageView on orientation change

I'm trying to fit a bitmap into an ImageView that changes size when rotating the device. It changes size because I load different layouts for portrait and landscape mode respectively. I calculate the final bitmap size based on how much available screen space the ImageView has when it has been placed on the layout. At the moment I'm trying to solve this by taking care of the configuration change (rotation) myself.

imageview - Android Image is seen/not seen when orientation is changed

I'm having some problems with ScrollView and an ImageView. What I want is: I have an ImageView set always at the top of the screen. Then, I have an ScrollView where go all elements of the View, and inside it I have another ImageView which it is set at the bottom of the screen when previous elements don't fill scre...

Android: How to change the orientation of an image to set it in imageview?

I am setting an image to ImageView, however the image is not picked from the device storage, the image is sent by the server, I want to know about the orientation of the image before setting it to the imageview. So, is it possible to check the orientation the jpg image coming from the server using its url and ExifInterface and then change its orientation as per required to set to the imageview?

Set ImageView orientation directly in Android?

I understand that I can rotate an ImageView in Android by matrix.postRotate(finalOrientation - initOrientation, width, height); But I want to set it as finalOrientation directly instead of rotating it by the difference, finalOrientation - initOrientation. So I am expecting something like: matrix.setOrientation(...

java - Image orientation is changed on ImageView When fetching from Gallery and upload

I simply want to fetch Images from gallery and upload then download from Server and set it on an ImageView on Activity. The image View is placed at the center of the Activity. All is working fine just a screenOrientation variation problem with adjusting Image on ImageView after fetching it from Server, download and set to image View. Please have a look over code what i am using to download Image Bitmap from Server and set ...

Android - ImageView Orientation changes depending on device

I made a custom camera application, and I want to display pictures on ImageView. I launched my program on 3 different phones (HTC Desire Z, Galaxy S4, and Galaxy S2). On the HTC, Orientation of the picture is respected in ImageView, but not with the Galaxys. My program works like this : -> I take a picture with the custom camera app -> I set the EXIF flags of the picture, depending on phone orienta...

android - How to scale ImageView after the orientation is changed?

When I rotate the screen, ImageView doesn't rescale image, but native Imageview rotates the image. How can I achieve this? Project Manifest: <activity android:name="ScaleTypeCenterInsidePortrait" android:configChanges="orientation|screenSiz...

android - appWidget Orientation Code

I have code in an appwidget that I want to run when the phone's orientation changes on the home screen, ie like when the keyboard flips out. I have an image that I want to change in an imageview in my appwidget. I can't use different layouts linked to the orientation (ie "layout" and "layout-land") because I don't know the name of the image file until runtime, it is created at runtime. Is there anyway to trigger code to ru...

Android Screen orientation, shake cellphone crashes the application

I am having search application, which loads data retreived from the webservice. While application on create it shows progress dialog once its done with loading data it dismiss the progress dialog. problem is while loading if i change the orientation of the phone it works fine it try to load activity again, but if i start shaking the cellphone while it is loading data application get crashed, any solution ??...

Android TimePicker hour field disappears after orientation change

I am using a TimePicker under API 1.5 and when the orientation is changed on my device (a stock G1 running 1.6 - though NOT on the 1.5/1.6 emulator), the hour field goes blank. It still remembers the hour, it just doesn't show it. Is there any workaround for this? The same problem was described by someone else here:

Android Bluetooth crashes after orientation change

My code is based on the BluetoothChat sample from the API. I connect to another device just fine and I can read and write to the connection no problem. But then if I turn the device the orientation of the UI changes automatically. After this, if I try to write to the Bluetooth connection, my application crashes! It seems weird that the orientation of the screen somehow affects the Bluetooth. I am new to Android, an...

android - How to change orientation of xml "layout" area in Eclipse

The xml files that contains the Activity layouts can be rendered by Eclipse. How do I get that Eclipse screen area to have vertical orientation, not horizontal (inside Eclipse - it correctly shows as vertical in the emulator)? Peter

Android - Forced locale reset on orientation changes

I try to force the locale in my app to one the user specified. As this may be used to demonstrations, I want to change the language/locale in the app and not anytime for the whole device. I looked around SO and tried to use every hint I found here. The result: I can restart my test activity with the new language, but if I change the orientation, the locale will always be reseted to the device one. I

How can the landscape screen orientation be restricted in android?

How can the landscape screen orientation be restricted in android?

Problem saving WebView scroll position during orientation change in Android 2.1

I have a problem preserving the current scroll position of content loaded in Android's WebView control during orientation changes. The issue currently exist only in Android 2.1 and Android 2.1 Update 1 The code that I use is based on the default Browser application and looks like this: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mWebView = n...

android - How to detect orientation change in home screen widget?

I am writing a home screen widget and want to update the home screen widget when the device orientation changes from portrait to landscape or the other way. How can I make it? Currently, I tried to reigster to CONFIGURATION_CHANGED action like the code below, but the Android didn't allow me to do that by saying "IntentReceiver components are not allowed to register to receive intents". Can someone help me? Thanks.

android - Gracefully handling screen orientation change during activity start

I'm trying to find a way to properly handle setting up an activity where its orientation is determined from data in the intent that launched it. This is for a game where the user can choose levels, some of which are int portrait orientation and some are landscape orientation. The problem I'm facing is that setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) doesn't take effect until the activity...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android