Android orientation change

My tabbed application does not display a view with a change in orientation.

I added

android:configChanges="keyboardHidden|orientation" 

to the main activity of the tab and to each activity in the manifest.

I added this method to each view:

 public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); setContentView(R.layout.active_alt); mColorLegendBtn = (Button) findViewById(R.id.colorbtn); mStatusView = (TextView) findViewById(R.id.celltitle1); TextView mStatusView1 = (TextView) findViewById(R.id.celltitle2); mStatusView1.setText(mStatusView1.getText()+"testcase1"); mStatusView.setText(mStatusView.getText()+"testcase"); initUI(); } public void initUI() { l1 = (ListView) findViewById(R.id.ListView01); EfficientAdapter efficientAdapter = new EfficientAdapter(mContext); l1.setAdapter(null); l1.setAdapter(efficientAdapter); } 

When launched, tabs, list, button and text view are displayed. When I change the orientation in the emulator, only the tabs are displayed by none of the other widgets, the screen is black.

What am I missing?

+5
source share
4 answers

I had exactly this problem. After much trial and error, I eventually solved it by making a one-line change to the manifest.

The trick is to add

 android:configChanges="orientation|keyboardHidden" 

to your TabActivity in the manifest. Leave all children's activities alone. Don't even bother with implementing onConfigurationChanged() , even in TabActivity .

I donโ€™t know how and why this works, but the effect is that the layout is updated, and both the tabs and the contents of the child activity are correctly redrawn in the new orientation.

+5
source

With success, I have found that the best way to change the screen with most controls is to make your xml layout for landscape mode in a separate xml, for example:

 res/layout-land/youractivity.xml 

using / layout / and / layout-land / for your layouts, as well as Graham Borland's answer is gold.

  <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="unspecified" android:launchMode="standard" android:configChanges="orientation|keyboardHidden" > 

the snippet above is what did my job. :)

ohh I really believe that โ€œunspecifiedโ€ is what allows the system to do what it thinks is better ...

Good luck

+1
source

In Mono for Android with a destination API greater than 13 , I found that a line that will go into the namespace but outside the Activity class :

 [Activity (Label = "Viewer", ConfigurationChanges = ConfigChanges.Orientation|ConfigChanges.ScreenSize)] 

leads to the launch of OnConfigurationChanged() , although there was no change in the manifest.

+1
source

Maybe your layout does not work in landscape / portrait mode? Try to start the application after the rotation, check if it gives the same result. If yes: correct your layout: D

0
source

Source: https://habr.com/ru/post/908308/


All Articles