Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Screen Orientation: Portrait doesn't work

I want my Activity to run only in portrait mode, so i tried different things, but none of them worked... here they are:

manifest:

<application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            android:configChanges="keyboardHidden|orientation"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="intent.to.front" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

Didn't work, still could rotate, even if my phone had screen rotation turned off.

next I tried from code. removed added staff from manifest, and wrote this in mainactivity class:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        Utilities.setLocale(getApplicationContext());
        setContentView(R.layout.activity_main);

here's my layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:fitsSystemWindows="true" tools:openDrawer="start">

    <include layout="@layout/app_bar_main" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView android:id="@+id/nav_view"
        android:layout_width="wrap_content" android:layout_height="match_parent"
        app:itemIconTint="@color/grey"
        android:layout_gravity="start" android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

result was: It doesn't let me rotate, but when I do, whole app freezes and can't do anything until I rotate it back to portrate.

Help me guys!

like image 785
Nikoloz14 Avatar asked Sep 14 '25 16:09

Nikoloz14


1 Answers

When you declare one of the landscape or portrait values, it is considered a hard requirement for the orientation in which the activity runs. As such, the value you declare enables filtering by services such as Google Play so your application is available only to devices that support the orientation required by your activities. For example, if you declare either "landscape", "reverseLandscape", or "sensorLandscape", then your application will be available only to devices that support landscape orientation. However, you should also explicitly declare that your application requires either portrait or landscape orientation with the element. For example, . This is purely a filtering behavior provided by Google Play (and other services that support it) and the platform itself does not control whether your app can be installed when a device supports only certain orientations.

If you would like to ignore this error, do the following:

    <activity
        android:name="YourActivity"
        ...
        android:screenOrientation="portrait"
        tools:ignore="LockedOrientationActivity">

You can read activity attributes in this page

like image 137
Kourosh Avatar answered Sep 17 '25 08:09

Kourosh