isa leap
isa leap

Reputation: 11

How can I prevent Android Activity from entering portrait mode breifly when I have specified landscape?

I have set my default activity to landscape in the manifest file.I have also tried setting landscape orientation using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

But still the activity runs in portrait mode for few seconds and then automatically converts to landscape.

I want to directly load it in landscape mode without portrait mode running briefly.

I have also referred this link.

How can I prevent Android Activity from briefly entering portrait mode when I have specified landscape?

This is the manifest file activity part.

    <activity
        android:name=".Launcher"
        android:clearTaskOnLaunch="true"
        android:launchMode="singleTask"
        android:screenOrientation="landscape"
        android:stateNotNeeded="true"
        android:theme="@style/Theme"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Upvotes: 1

Views: 189

Answers (4)

Tamal Samui
Tamal Samui

Reputation: 738

It is very difficult to achieve as your launcher is always running in Portrait mode and you are launching your activity in Landscape mode.

So to achieve this workaround is needed as described by "F43nd1r".. But additionally few more things needs to be done. Try this..

You need a dummy activity in portrait mode which will be your launcher activity and will be with "NoActionBar.Fullscreen" mode.

public class DummyActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRotationAnimation();
    ImageView imageView = new ImageView(this);
    imageView.setImageResource(android.R.color.white);

    setContentView(imageView);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            startActivity(new Intent(DummyActivity.this, YourActivity.class));
            finish();
        }
    },500);
}

private void setRotationAnimation() {
    int rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
    Window win = getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();
    winParams.rotationAnimation = rotationAnimation;
    win.setAttributes(winParams);
}
}

YourActivity should also have and call the setRotationAnimation() method from the oncreate()..

public class YourActivity extends Activity {

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

private void setRotationAnimation() {
    int rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
    Window win = getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();
    winParams.rotationAnimation = rotationAnimation;
    win.setAttributes(winParams);
}

}

Now the most important thing is your theme in the manifest. It should look like..

    <activity
        android:name=".DummyActivity"
        android:label="@string/app_name"
        android:configChanges="orientation"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".YourActivity"
        android:label="@string/app_name"
        android:configChanges="orientation"
        android:screenOrientation="landscape"
        android:theme="@android:style/Theme.Holo.Light">
    </activity>

Note: 1> This is only to launch the app. When you press back button and to exit the app the animation will be shown.

2> In DummyActivity the 500ms can be reduced according to your requirement, depending on your device performance.

Upvotes: 0

Luke Villanueva
Luke Villanueva

Reputation: 2070

Try adding android:screenOrientation="landscape" on your application in your manifest.

Upvotes: 0

Tejas Patel
Tejas Patel

Reputation: 250

Try to add configChanges after declare screenOrientation.

android:screenOrientation="landscape"

android:configChanges="orientation|screenSize"

Upvotes: 0

F43nd1r
F43nd1r

Reputation: 7759

Workaround

Create a new transparent activity. Also request landscape for this activity. As soon as you register that this activity has changed to landscape, start your real activity. Now you always have to start this new activity instead of the real activity.

Note: untested

Upvotes: 0

Related Questions