Why does not the layout change (Portrait / Landscape) when the device is rotated?

2

I have implemented tabs with the viewpager, depending on the orientation of the screen (Portrait / Landscape) the layout assigned for each view must be changed, but it does not change, it remains with the layout that started the activity, some idea

probe with changing the following property to the activity in the manifest:

....
android:configChanges="orientation|keyboardHidden|screenSize"
....

but without any result

    
asked by Mark Dev 12.01.2017 в 00:45
source

2 answers

3

What you mention in your question that you tried:

android:configChanges="orientation|keyboardHidden|screenSize"

only serves to not destroy the Activity when rotating the device but does not serve in any way to allow the rotation.

Check within your AndroidManifest.xml if you do not have an orientation defined, either in the application or in an Activity:

 android:screenOrientation="< orientación >".

If you defined

 android:screenOrientation="portrait"

would only allow vertical orientation.

If you defined

 android:screenOrientation="lanscape"

would only allow horizontal orientation.

Programatically you can do the same, check if the orientation is not blocked:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

   //Define únicamente orientación vertical. 
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

The other option is that in the settings of your device this rotation is blocked! = P

    
answered by 12.01.2017 в 01:15
1

This statement means nothing more than notifying the system that you plan to manage these changes in the configuration with your own code.

Then you have to implement:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

and within that, make the changes to the Views that you need.

    
answered by 12.01.2017 в 01:04