0

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.

MikkoP
  • 4,864
  • 16
  • 58
  • 106

4 Answers4

1

Add the following to your Activities onCreate:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Aashir
  • 2,621
  • 4
  • 21
  • 37
0

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.

Lai Xin Chu
  • 2,462
  • 15
  • 29
0

In your AndroidManifest.xml set...

android:screenOrientation="portrait"

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

donkey
  • 204
  • 1
  • 7
0

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...

TobiasW
  • 861
  • 3
  • 13
  • 36