Reputation: 221
I'm developing an Android app (API 11+) and I want to support multiple screen size and densities, my project has different density folders to provide drawable resources (mdpi, hdpi, xhdpi and xxhdpi) and different layout folders with qualifiers (small, normal, large and xlarge).
From Google docs:
xlarge screens are at least 960dp x 720dp
large screens are at least 640dp x 480dp
normal screens are at least 470dp x 320dp
small screens are at least 426dp x 320dp
I think that I understand the "dp" unit. If I'm not wrong a 480x800px mdpi phone screen has 480x800dp units because in the mdpi devices 1px=1dp, so this screen should be large. A tablet with the same size and density is large too right?
How can I provide a different layout for this 2 devices? How can I differentiate them?
Upvotes: 2
Views: 1991
Reputation: 2034
It is likely, that mdpi device what you are talking about is fairly big. As bis as a tablet. But because of these disambiquities (and because different devices reported their size/dpi values wrongly) Android decided to come up with a different approach. If you can reconsider targeting Android 13+ only (which is reasonable, since the market share of 11-12 is around 0%), you could use the swXXXdp (Smallest Width in dp) filters:
res/layout-mdpi/content.xml for phones and phablets
res/layout-sw600dp-mdpi/content.xml for 7" tablet
res/layout-sw720dp-mdpi/content.xml for 10" tablet and above
And the dpi value doesn't really matter here. Just if you want to make those images look more crisp/polished.
Upvotes: 1
Reputation: 8702
The same way there are different folders for drawables, you can create different layouts for screen size.
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
From the Android docs: http://developer.android.com/guide/practices/screens_support.html
EDIT: To programatically detect whether the device is a handset or a tablet, you can use this code:
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
Then, in your activity, you can set a different layout depending on the previous value:
@Override
onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if isTablet(this)
setContentView(R.layout.handset_main_layout);
else
setContentView(R.layout.tablet_maint_layout);
}
This is just an example.
Upvotes: 1
Reputation: 236
If you don't use pixels for anything and provide the different assets for ldpi, mdpi, xdpi, ... you will get a long way. The system will determine what layout file and asset it should use for the particular device.
Upvotes: 0