MikkoP
MikkoP

Reputation: 5092

Locking phone in portrait mode

I want to change the rotation mode in my application programmatically. All the discussions I found were about disabling rotation in specific Activities. I want to lock the whole phone in portrait mode. How do I accomplish this?

Edit. To clarify, I want to lock the whole phone including all other apps, settings etc. Not just my own app.

Upvotes: 0

Views: 231

Answers (4)

TobiasW
TobiasW

Reputation: 891

This post should be enough to answer your question :)

stackoverflow.com/questions/582185/

If you are working with fragments you just have to define this once else you need to define it in each activity tag...

Upvotes: 0

donkey
donkey

Reputation: 204

In your AndroidManifest.xml set...

android:screenOrientation="portrait"

...as an attribute for your Activity/Activities.

Upvotes: 0

Lai Xin Chu
Lai Xin Chu

Reputation: 2482

The easiest, fastest, and guaranteed-to-work way to do this is to define a parent Activity class:

public class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

and then have all your activities inherit the parent Activity:

Activity 1:

public class MainActivity extends MyActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Activity 2:

public class LoginActivity extends MyActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }
}

This way, you only need to define it once.

Upvotes: 0

Aashir
Aashir

Reputation: 2621

Add the following to your Activities onCreate:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Upvotes: 1

Related Questions