Reputation: 33491
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?
Upvotes: 4
Views: 4542
Reputation: 23596
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);*/
}
Upvotes: -1
Reputation: 1501
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.
Upvotes: 6
Reputation: 2930
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 :)
Upvotes: -2