-1

So I wanted to see how my project looks like when in landscape and for sure i didnt like the appearance. I was wondering how can i make my project stay on portrait? I will appreciate any help.

android.app.FragmentManager myFragment = getFragmentManager();
        FragmentTransaction fragmentTransaction = myFragment.beginTransaction();

        Configuration configInfo = getResources().getConfiguration();
        if(configInfo.orientation ==Configuration.ORIENTATION_LANDSCAPE){
            FragmentLandscape lScape = new FragmentLandscape();
            fragmentTransaction.replace(android.R.id.content,lScape);
        }else{

            FragmentPortrait lportrait = new FragmentPortrait();
            fragmentTransaction.replace(android.R.id.content,lportrait);
        }
        fragmentTransaction.commit();
Madona wambua
  • 1,408
  • 1
  • 21
  • 37
  • 2
    Take a look at [this](http://stackoverflow.com/questions/4675750/lock-screen-orientation-android) answer. I believe you can set this in the manifest. Also, the developer [manifest docs](http://developer.android.com/guide/topics/manifest/activity-element.html#screen) – PPartisan Aug 14 '15 at 20:47
  • Thanks. Will look at it. – Madona wambua Aug 14 '15 at 21:09

1 Answers1

2

There are multiple ways to force portrait mode, but my preferred way to do so is the to use the following code for each activity inside AndroidManifest.xml

android:screenOrientation="portrait"

Later on, if and when you decide to create an alternate landscape layout for tablets, you can change those same lines in AndroidManifest.xml to this:

android:screenOrientation="nosensor"

The "nosensor" setting tells your app to use the natural orientation for the device, and will not change if the user rotates the device. So tablets will always use landscape orientation for your app, and phones will always use portrait orientation.

joshgoldeneagle
  • 4,616
  • 2
  • 23
  • 30