Reputation: 11251
I have a requirement where I have to provide different layout
resources with my application.
This application is supposed to run on many screen sizes (as always) and I want to be able to address many of them as precisely as possible.
As of now, I am targeting following screen sizes:
For this, I created a layout structure like this:
And in XML, I have declared the following support:
<supports-screens android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
I am testing in it emulator and the problem is: it accepts layout-w480dp-land
xml, but when I go in portrait mode, it falls back to default layout.
Any idea where I might be going wrong?
Upvotes: 1
Views: 2200
Reputation: 25864
As I said in the Android
Chatroom, Pixels
and Density-independent pixel (dp)
are different:
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
When you have a folder named layout-w480dp what you're saying is:
"Use this layout folder as long as the width dimension is equal to or greater than 480 dp."
Now, if you have a device with 240dpi with a screen size 480x800 pixels (which is very likely) then you actually only have 320dp to play with - explaining why you're folder is now being skipped.
dp = px / (dpi / 160)
320dp = 480px / (240dpi / 160)
Upvotes: 3
Reputation: 7892
I can refer you to one of the Google I/O 2011 session from Reto Meier:
One way to support the latest devices while also supporting older versions is to use a "shim" activity that determines what platform the device is running. It doesn't have a UI. Reto shows some code for launching different activities based on system version. This is a parallel activity pattern...
Link to session - His presentation is also available to download, the parallel pattern principle start from slide 10
Upvotes: 1
Reputation: 951
Well, I never did it your way, but what I do is measure Screen Size and density on run time and apply the layout in onCreate() in the beginning. Here is the code for your reference and might help you out:
/**
* To acknowledge the density of the application so as to use the layout
* according to the phone density
*/
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
setContentView(R.layout.x);
break;
case DisplayMetrics.DENSITY_MEDIUM:
setContentView(R.layout.y);
break;
case DisplayMetrics.DENSITY_HIGH:
setContentView(R.layout.z);
break;
Upvotes: 0
Reputation: 3521
Use
ldpi Resources for low-density (ldpi) screens (~120dpi).
mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
hdpi Resources for high-density (hdpi) screens (~240dpi).
xhdpi Resources for extra high-density (xhdpi) screens (~320dpi).
instead of w480dp
and follow following table. Android docs
Upvotes: 0
Reputation: 128428
I have heard about below layout resources:
res/layout-sw600dp
res/layout-sw720dp
Not sure about your problem but i haven't heard about w480dp, it may be sw480dp.
Upvotes: 0