Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace layout on orientation change

My app has a webview and some buttons inside a LinerLayout.

the problem is, I want the buttons to be on bottom in portrait mode and on left in landscape mode while the webview maintains it's state.

Two different layout doesn't work as it force recreation of the activity that refresh the webview. for now I use android:configChanges="orientation" in activity tag so webview doesn't get refreshed on orientation change.

Is there anyway to replace the layout of buttons on the change of screen mode?

enter image description here

portrait mode

enter image description here

landscape mode

like image 248
Ali Avatar asked Dec 12 '12 20:12

Ali


People also ask

What happens when orientation is changed in Android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

How to change orientation of layout in Android?

First, add android:configChanges="orientation|screenSize|keyboard|keyboardHidden" so the app handles the config changes instead of android. Make two different layout for landscape and portrait. In both layouts instead of webview place a FrameLayout which acts as a placeholder for the webview.

How can we cope with screen orientation changes?

Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.


2 Answers

I tested fragments, but dealing with fragment makes things much more complex and the fragment itself needs saving and restoring which may not work in a webview which has javascript state, So I searched more and find a nice article somewhere and with some modification I came to a solution which I suggest:

First, add android:configChanges="orientation|screenSize|keyboard|keyboardHidden" so the app handles the config changes instead of android.

Make two different layout for landscape and portrait. In both layouts instead of webview place a FrameLayout which acts as a placeholder for the webview.

Define initUI method like this and put everything related to UI initialization in this method:

public void initui()
{
    setContentView(R.layout.main);
    if (wv == null) wv = new WebView(this);
    ((LinearLayout)findViewById(R.id.webviewPlace)).addView(wv);
    findViewById(R.id.home).setOnClickListener(this);
}

If the webview doesn't exist yet it will be created and after setContentView(R.layout.main) it will be added to the layout. Any other UI customization you need came after this.

and in onConfigurationChanged:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    ((LinearLayout)findViewById(R.id.webviewPlace)).removeAllViews();
    super.onConfigurationChanged(newConfig);
    initUI();
}

In onConfigChange the webview is removed from old placeholder and initui will be called which will add it back to the new layout.

In oncreate() call initui() so the ui will be initialized for the first time.

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    initUI()
}

I wish it would be helpful for someone.

like image 64
Ali Avatar answered Sep 20 '22 16:09

Ali


If you are using android:configChanges="orientation|screenSize"

In manifest,it ignores the XML in "layout-land". If you create a different XML for landscape don't use the android:configChanges="orientation|screenSize" tag for that activity in manifest.

like image 25
Pratibha Sarode Avatar answered Sep 19 '22 16:09

Pratibha Sarode