Android Devices with Different Height Takes Same Layout Folder

Android devices with different height takes same layout folder

Well you are right in some sense android should take layout dependent on different densities but some mobile do not fall under specific density. Therefore android will pick up default layout from layout directory.

to support multiple screen resolution provide different layout for different screen sizes, you can make following directories in res directory like this

layout-hdpi

layout-mdpi

layout-xhdpi

layout-xxhdpi

layout-w320dp-h408dp

layout-w480dp-h800dp

layout-w480dp-h854dp

layout-w720dp-h1280dp

layout-w1080dp-h1920dp

when you provide layout in all this directories you will give multiple screen support for different sizes as well
layout-w1440dp-h2560dp

Use "dip" instead they will help you in debugging your layout as they will try to keep a coherent size to multiple screen resolutions,

<ImageView
android:id="@+id/avtar_animation_11"
android:layout_width="45dip"
android:layout_height="45dip"
android:src="@drawable/avtar011"/>

while supporting multiple screen when you give "dp" to dimensions, Actually android expects you to provide different values for different screen resolution. Lets say below is your imagview dimensions make few folders in res folder in your android project like these below

Sample Image

values-hdpi, values-mdpi, values-ldpi, values-xhdpi, values-xxhdpi

and in them make a dimens.xml file each of them and write

<dimen name="image_view_width">28dp</dimen>
<dimen name="image_view_height">28dp</dimen>

now that i have mentioned "dp" here instead of dip android wants me to keep track for different dimensions for different screen resolution, So i will change the image_view_width and image_view_height values are in separate values folders where dimens.xml is located. Make sure your dp values change as per your screen resolution you want your view to fit in.

 <ImageView
android:id="@+id/avtar_animation_11"
android:layout_width="@dimen/image_view_width"
android:layout_height="@dimen/image_view_height"
android:src="@drawable/avtar011"/>

hard part is over now android will pick one of dimens.xml values depending on which screen your app is running, Voila now your layout rocks

Android layout folders for different screen sizes

So this seems to be the only arrangement that worked for me...

layout (Nexus 4 & 5)

layout-sw440dp-port (Note 4, Nexus 6)

layout-sw720dp-port (Samsung 10" tablet)

(It I take off the "port" it doesn't work...) ;-)

Thanx for all the suggestions!

Selecting a different layout directory based on screen Height

After digging even deeper, i found out that you can choose different layouts inside the onCreate() method based on info from DisplayMetrics like so:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;

if (...) {
setContentView(R.layout.ex1);
} else {
// .....
}

By using the height and width variables as the conditions inside the if statement , i managed to select the appropriate layout files for each screen size!



Related Topics



Leave a reply



Submit