Android: screen rotation inside Activity does not switch portrait / landscape

I am creating an Android application and now I want to add layouts for different orientations. I created a folder with layouts and placed a different layout for my first launch activity "myStartActivity" (with the same name as before than for both orientations).

Depending on the orientation of the screen, before I started the application, the correct layout was selected: "myLayout.xml" from the "layout" folder when I start in portrait and "myLayout.xml" from "layout-land" when I start in the landscape.

The problem is that when I rotate the device, when I am already in Activity, after rotation I do not get a new layout. For example: turning from portrait to landscape, it still shows “myLayout.xml” from inside the “layout” folder, not the “layout-earth” folder, as it should be.

I did not overwrite any OnConfigurationChange methods or anything else. All I do in "myStartActivity" is to create some buttons and give them some listeners. I want to use a different layout in the landscape to reorder the buttons.

+4
source share
3 answers

In my case, the described problem occurs only when I have android: configChanges = "orientation" in the activity in the manifest.

Otherwise, the correct layout is automatically used when turning.

+3
source

What you can do is use Activity.getResources (). getConfiguration (). orientation

Then, depending on the orientation result, set the content view to your oncreate () method, which is called during rotation.

protected void onCreate(Bundle savedInstanceState) { int result = this.getResources().getConfiguration().orientation; if (result == 1){ //set content view to portrait setContentView(R.layout.portrait); } else{ //set content view to landscape} setContentView(R.layout.landscape); } 

Or stick with the case argument :)

0
source

If you only test on an emulator, you may have problems detecting a change in orientation. I at least experienced this.

0
source

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


All Articles