Reputation: 85
I need to change layout of my Android (4.1 API 16) application when orientation is changed.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main_2);
System.out.println("Orientation changed!");
}
Also I have added the next line to manifest file
android:configChanges="orientation"
I use Ctrl+F11 to change orientation. My screen rotates but layout stays the same and nothing is printed in LogCat. Feels like onConfigChanged event doesn't occur.
Where is my mistake?
Thank you.
Upvotes: 0
Views: 2320
Reputation: 4411
try this one....
android:configChanges="orientation|keyboardHidden|screenSize"
Upvotes: 0
Reputation: 16354
Try using android:configChanges="orientation|keyboardHidden|screenSize"
Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
Source : Documentation
Hence, also add "|screenSize" to configChanges
if your application targets API 13 and above.
Upvotes: 4
Reputation: 395
Lets say your portrait orientation layout file is in layout/my_layout.xml
Place the layout you want to be used in the landscape mode in layout-land folder with the same layout file name. i.e. layout-land/my_layout.xml
Do not add android:configChanges="orientation" to the the manifest for that activity.
You do not need to explicitly change the layout. You do not need to override the onConfigurationChanged(Configuration newConfig) function
Upvotes: 1