Sirop4ik
Sirop4ik

Reputation: 5243

How to keep screen orientation in portrait mode with Android 5.0?

I need to keep the screen in portrait mode. For this I use this line in the manifest file

android:screenOrientation="portrait"

Here my file

<activity
        android:name=".activities.Splash"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

I use a Samsung S5, and it still can rotate in landscape...

Accorging documentation with Android 5.0 we get wide setting options

android:screenOrientation=["unspecified" | "behind" |
"landscape" | "portrait" |
"reverseLandscape" | "reversePortrait" |
"sensorLandscape" | "sensorPortrait" |
"userLandscape" | "userPortrait" |
"sensor" | "fullSensor" | "nosensor" |
"user" | "fullUser" | "locked"]

I have tried some of them but it still rotate...

What am I doing wrong?

Entire manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.camera2basic"
android:versionCode="1"
android:versionName="1.0">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />

<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<!-- For Google+ LogIn -->
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

<!-- Для регистрации приложения в GCM всякий раз, когда телефон перезагружается. -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android..RECEIVE" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<permission
    android:name="com.example.android.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.android" />

<application
    android:name=".authorization.MyFacebook"
    android:allowBackup="true"
    android:icon="@drawable/title"
    android:label="@string/app_name"
    android:theme="@style/MaterialTheme">
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

    <activity
        android:name=".activities.Splash"
        android:label="@string/app_name"
        android:screenOrientation="locked">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <receiver
        android:name=".gcm.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.example.android" />
        </intent-filter>
    </receiver>

    <service android:name=".gcm.GcmIntentService" />

    <activity
        android:name=".activities.AcceptNotAccept"
        android:theme="@style/AppTheme.CustomStyle" />
    <activity
        android:name=".activities.PopUpActivity"
        android:theme="@style/AppTheme.CustomStyle" />
    <activity android:name=".tools.TestDeleteIt" />
    <activity android:name=".activities.Welcome2" />
    <activity android:name=".activities.WebActivity" />
    <activity android:name=".authorization.AuthorizationActivity" />
    <activity android:name=".activities.WebViewAcceptReject" />
    <activity android:name=".activities.CameraActivity" />
    <activity android:name=".authorization.LogIn" />
    <activity android:name=".authorization.RegistrationActivity" />
    <activity android:name=".activities.ForgotYourPassword" />
    <activity android:name=".activities.MainActivity" />
    <activity android:name=".activities.VideoActivity"/>
    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />
</application>

</manifest>

Java code for splash

public class Splash extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

//      Hide the status bar.
    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);

    TextView tvSplash = (TextView) findViewById(R.id.tvSplash);
    tvSplash.setVisibility(View.INVISIBLE);
    UtilClass.setFont(getApplicationContext(), tvSplash);

    Thread logoTimer = new Thread() {
        public void run() {
            try {

                ImageView imageView = (ImageView) findViewById(R.id.ivSplash);
                Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale);
                imageView.startAnimation(animation);

                int logoTimer = 0;
                while (logoTimer < 2500) {
                    sleep(100);
                    logoTimer = logoTimer + 100;
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        TextView textView = (TextView) findViewById(R.id.tvSplash);
                        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
                        textView.startAnimation(animation);

                    }
                });

                logoTimer = 0;

                while (logoTimer < 2500) {
                    sleep(100);
                    logoTimer = logoTimer + 100;
                }

                if (UtilClass.checkLogIn(getApplicationContext())) {
                    startActivity(new Intent(getApplicationContext(), MainActivity.class)
                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                                    Intent.FLAG_ACTIVITY_CLEAR_TASK));
                } else {
                    startActivity(new Intent(getApplicationContext(), AuthorizationActivity.class)
                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                                    Intent.FLAG_ACTIVITY_CLEAR_TASK));
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    logoTimer.start();
}
}

Upvotes: 1

Views: 2736

Answers (5)

Rifaat Hassan
Rifaat Hassan

Reputation: 1

android:exported="true" android:screenOrientation="locked"

this is work for me

Upvotes: 0

Yogesh Rathi
Yogesh Rathi

Reputation: 6499

In your manifest file after your main activity Paste below line.

android:screenOrientation="portrait"

java through setContentView() Paste below line.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

see documentation.

Upvotes: 1

ThomasV
ThomasV

Reputation: 881

Can you add android:configChanges="orientation|keyboardHidden" ?

<activity
    android:name=".activities.Splash"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:configChanges="orientation|keyboardHidden">

Upvotes: 0

Abhijeet Singh
Abhijeet Singh

Reputation: 45

In your manifest file after your main activity Paste below line.

android:screenOrientation="portrait"

Or in your coding after setContentView() Paste below line.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Upvotes: 1

Naveen Tamrakar
Naveen Tamrakar

Reputation: 3339

Locks the orientation to its current rotation, whatever that is. Added in API level 18.

android:screenOrientation="locked"

android:screenOrientation="locked"

Upvotes: -1

Related Questions