1

I want to allow landscape and portrait on tablets, portrait only on phones. That question has already been asked, but I'm facing a bug with any of the proposed solutions.

I have various dimens defined depending on the height of the device (h500dp, h400dp, etc.). So if I have my phone (411dp width, 731dp height on portrait) rotated to landscape, and I launch the app afterwards, even though the activity appears on portrait, it has taken the h400dp dimens; as if the height was evaluated depending on my current phone orientation (landscape). If I have the phone on portrait before launching the app, it takes the h500dp dimens, which would be the correct one.

The code I'm using is the one from this answer; the relevant code is on the Activity onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (!isTablet()) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

Manifest:

    <activity
        android:name=".login.LoginActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Community
  • 1
  • 1
RominaV
  • 3,335
  • 1
  • 29
  • 59

2 Answers2

0
<activity
            android:name=".MainActivity"
            android:label=""
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">

See https://developer.android.com/training/basics/supporting-devices/screens.html

If you want to provide a special layout for landscape, including while on large screens, then you need to use both the large and land qualifier:

MyProject/
    res/
        layout/              # default (portrait)
            main.xml
        layout-land/         # landscape
            main.xml
        layout-large/        # large (portrait)
            main.xml
        layout-large-land/   # large landscape
            main.xml

/

determine the type of device using screen dimensions. You could refer to this post

On the second step you can change the screen orientation using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);. There is another post here.

Community
  • 1
  • 1
nzala
  • 374
  • 4
  • 10
0

Please remove configuration changes from AndroidManifest.xml of Activity

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

will restrict the layout from being updated on Orientation change.

Mahesh
  • 974
  • 6
  • 12