Nikoloz14
Nikoloz14

Reputation: 267

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!

Upvotes: 5

Views: 11971

Answers (6)

Ariel
Ariel

Reputation: 1

Though it might not be obvious to some; if you are using a remote device (using wifi), you will need to uninstall the app and then reinstall it via the wire. Hopefully this will fix your issue.

Upvotes: -2

Kourosh
Kourosh

Reputation: 2299

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

Upvotes: 1

johnrao07
johnrao07

Reputation: 6928

First

Check your theme styles if you have code there for screen orientation, if so remove it.

Then Remove

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

I have no idea why you using this. Try removing it and check

Upvotes: 0

saeed shahini
saeed shahini

Reputation: 1847

When you use :

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

This commend ignore all settings for orientations that are in manifest file ,even if screen rotation was off, it will rotate.

You must first remove this command from onCreate method then write this to your manifest, and it will work:

   <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Upvotes: 0

Quang Doan
Quang Doan

Reputation: 662

Try this.

PORTRAIT YourActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

LANDSCAPE YourActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Upvotes: 0

Jas
Jas

Reputation: 3212

Adding android:screenOrientation="portrait" could simply do the trick :

<activity android:name=".YourActivity"
    android:configChanges="orientation"
    android:screenOrientation="portrait"/>

Upvotes: 0

Related Questions