4

I have an Android app built on a phone that I am going to update now to also support tablet-sized screens (in particular Galaxy Tab 10.1). I figured out the whole res/layout thing, so that's fine. However, I built my app to have a fixed screen orientation in AndroidManifest.xml, set as android:screenOrientation="portrait".

Now, I want to have the following: have a fixed orientation for each screen size, the one fixed in portrait (layout-small) and the other as landscape.

Using res/layout-small-land and res/layout-large-port doesn't do what I want, because it still switches from portrait to landscape and back. Worse even, the app crashes when I rotate my phone to landscape, because the res/layout-small-land doesn't exist.

Can I do this by just defining XML files, or do I have to add code?

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • have you defined android:configChanges="keyboardHidden|orientation" in Activity tag ? – Dhruvil Patel Oct 31 '12 at 12:34
  • No. But that seems to be when the orientation changes. When the tablet is held in portrait mode and the app starts, I also want it to run in landscape. – Bart Friederichs Oct 31 '12 at 12:41
  • for Which type of devices you want to set the landscape mode? I means(hdpi/ldpi/mdpi)? I think you have to mention that. Even if you want to change the orientation as per the device screen layout then dont put fix orientation to the manifest file for that activity. Please try it and let me know. – Dhruvil Patel Oct 31 '12 at 12:50
  • I want landscape for screens that are `large` or larger, and portrait for screens that are `normal` or smaller. – Bart Friederichs Oct 31 '12 at 12:53
  • See accepted answer here: [SO][1] It will looks like your answer. [1]: http://stackoverflow.com/questions/2402639/how-to-create-layout-small-land-folder – Shreyash Mahajan Oct 31 '12 at 13:02
  • I believe this answers your question. http://stackoverflow.com/questions/10491531/android-restrict-activity-orientation-based-on-screen-size – Stefan Ronnkvist Apr 05 '15 at 22:16

3 Answers3

6

Instead of fixing the orientation on the manifest, you could use setRequestedOrientation. On the onCreate method of each activity, check if you are on a tablet or smartphone and set the desired orientation.

Another option could be Creating Multiple APKs for Different Screen Sizes, depending on your needs.

Marcelo
  • 1,471
  • 3
  • 19
  • 22
-1

Yes, you can create the layout for the same. See accepted answer here: SO

It will solved your question.

UPDATE

If you want to set it as per the Screen size then please see below code to get the density of the screen. and based on that and Screen Size, you have to set the orientation programetically.

Code to check Density:

public void ScreenSupport(){
     Display display = getWindowManager().getDefaultDisplay();
     DisplayMetrics dm = new DisplayMetrics();
     display.getMetrics(dm);

     int width = display.getWidth();
     int height = display.getHeight();
     int density = dm.densityDpi;
     String densityString = null;

     if(density == DisplayMetrics.DENSITY_HIGH) {
         densityString = "HDPI";
     } else if(density == DisplayMetrics.DENSITY_MEDIUM) {
         densityString = "MDPI";
     } else if(density == DisplayMetrics.DENSITY_LOW) {
         densityString = "LDPI";
     }else if(density == DisplayMetrics.DENSITY_DEFAULT) {
         densityString = "DEFAULT";
     }

    /* System.out.println("Screen Height is: "+height+ " and Width is: "+width);
     System.out.println("Screen Support: "+densityString);*/
}
Community
  • 1
  • 1
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • No, because it does not stop the app from rotating when I rotate the device. I want to fix the orientation based on screen size. – Bart Friederichs Oct 31 '12 at 13:09
  • Listern My friend, there are different layout available to supports all the available device. You just stop fixing the orientation from the manifest and then run your app. It will works. As i have tried it earlier. – Shreyash Mahajan Oct 31 '12 at 13:11
  • Also refer this: http://stackoverflow.com/questions/2124046/how-do-i-specify-different-layouts-for-portrait-and-landscape-orientations – Shreyash Mahajan Oct 31 '12 at 13:17
  • I did remove the fixing from the manifest, have a `res/layout-large-land` and a `res/layout-small-port`, and when I **rotate the device** the app rotates (because when the tablet is in portrait mode, the `small-port` layout is chosen). I do not want that; I want to **fix** the orientation based on screen size. – Bart Friederichs Oct 31 '12 at 13:18
  • Okkk... So if you want to set the fix layout only based on the screen size then you can only able to do it by Java code. – Shreyash Mahajan Oct 31 '12 at 13:22
  • Thanks for the updated answer, but it has nothing to do with my question. I am talking about screen size, not density. The question was only if I could do it in XML or did I need code. I know I need code now (and have implemented it already). – Bart Friederichs Oct 31 '12 at 13:35
-2

Just remove android:screenOrientation="portrait" from manifest.xml

And add layout-large-land,layout-small-land and also keep layout-large,layout-small folders.

Hope it works for you. Enjoy coding :)

Dhruvil Patel
  • 2,910
  • 2
  • 22
  • 39