Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i support multiple screen sizes in android with single xml layout file

Tags:

android

xml

I want to support my android screen in multiple screen sizes but i can do it with maintaining multiple xml layout file's

but according to requirement it i supposed to be done with the single XML layout in order to optimize the app usage.

so please can you help me i have gone through multiple tutorials multiple links but not able to get last option with stack overflow

like image 972
Mayank Purohit Avatar asked Jan 12 '17 06:01

Mayank Purohit


People also ask

How can I make Android apps compatible with all screen sizes?

The best way to create a responsive layout is to use ConstraintLayout as the base layout in your UI. ConstraintLayout enables you to specify the position and size of each view according to spatial relationships with other views in the layout. All the views can then move and resize together as the screen size changes.

How do I manage different screen size and orientation in Android?

res/sw600dp/layout. xml -> will be used for all screen sizes bigger or equal to 600dp. This does not take the device orientation into account. 2) Available Screen Width - Specifies a minimum available width in dp units at which the resources should be used.

How to make Android application support multiple screen sizes and densities?

The Android system itself also provides scaling and resizing to make the Android application support multiple screen sizes and densities, to some extent. Still you should always try to provide such support yourself. 1. Set support screens in the Manifest file 2. Provide various layouts for various screen sizes 3.

How to make the app load different layout files for multiple screens?

To make the app load different layout files for multiple size screens, we need to follow the below steps. Modify app / res / layout / activity_multiple_fragment.xml file as below. Now the layout file only contains one left fragment. Create a folder layout-large under app / src / main / res folder.

How to make the layout-large folder visible in Android Studio?

To make the layout-large folder visible in android studio, you need to switch to Project mode in the left panel. large is just one layout qualifier, the layout file under layout-large folder will be used when android OS thinks the device screen size is large.

Can Android program load layout files dynamically based on screen resolution?

There are a lot of android devices with different screen sizes and resolutions. This article will tell you how to make an android program load layout files dynamically based on the device’s screen resolution or size at run time.


5 Answers

drawable:

For images, you have to manage different drawable folders as per screen resolution:

drawable-ldpi        //240x320
drawable-mdpi        //320x480
drawable-hdpi        //480x800
drawable-xhdpi       //720x1280
drawable-xxhdpi      //1080X1920
drawable-xxxhdpi     //1440X2560
drawable-tvdpi       // nexus 7 etc 
drawable-xlarge-xhdpi //tablet like nexus 10  

dimes: For dimens, If you are using different static dimes as per your UI then you have to define them respected to their values... folders.

Values folder for different smart phones resolutions:

values-ldpi\dimens.xml
values-mdpi\dimens.xml
values-hdpi\dimens.xml
values-xhdpi\dimens.xml
values-xxhdpi\dimens.xml
values-xxxhdpi\dimens.xml

Note: If you are using sp/sip/dp/dip then these values will be adjusted based on the density of device. e.g Suppose you had set 10sp for TextView in mdpi(320X480) resolution device. Then this same value will be auto adjusted for other resolutions.

mdpi(10sp==10px)
hdpi(15px)
xhdpi(20px)
xxhdpi(30px)
xxxhdpi(40px)

These are the some general usage values folder which are used to manage dimens related to their screen resolutions.

Different values folder for different screens:

values-sw720dp          10.1” tablet 1280x800 mdpi

values-sw600dp          7.0”  tablet 1024x600 mdpi

values-sw480dp          5.4”  480x854 mdpi 
values-sw480dp          5.1”  480x800 mdpi 

values-xxxhdpi                 1440X2560 xxxhdpi

values-xxhdpi                  1080X1920 xxhdpi

values-xhdpi            4.7”   1280x720 xhdpi 
values-xhdpi            4.65”  720x1280 xhdpi 

values-hdpi             4.0” 480x800 hdpi
values-hdpi             3.7” 480x854 hdpi

values-mdpi             3.2” 320x480 mdpi

values-ldpi             3.4” 240x432 ldpi
values-ldpi             3.3” 240x400 ldpi
values-ldpi             2.7” 240x320 ldpi

For knowledge in depth go with Support screen resolution

like image 50
Ready Android Avatar answered Oct 16 '22 10:10

Ready Android


Create three different Layouts Folder in your res folder for all devices and use the dimensions accordingly.

Generic Layout Folders

res/layout-small
res/layout-normal
res/layout-large
res/layout-xlarge

After you are done with making your Normal/Medium Layouts follow these steps:

  1. Convert the Normal Dimensions for other Screen Sizes.
  2. Copy your Normal Layout xml files in to other Folders.
  3. Change the suffix of the dimensions used according to the folder that you are in
  4. Resize the Image Resources in your drawable folder (Width and Height - Same technique as we used for converting the dimens) and put them in their respective drawable folder (drawable-ldpi, drawable-mdpi, drawable-hdpi, drawable-xdpi and so on).
  5. Then your Layouts should work on every device with correct positioning.

For converting Values

0.75 - ldpi  (small)   //mdpi dimens *0.75
1.0  - mdpi  (normal)  //First create these dimensions
1.5  - hdpi  (large)   //mdpi dimens *1.5
2.0  - xhdpi (xLarge)  //mdpi dimens *2.0

For Example

android:layout_width="66dip" //in normal
android:layout_width="100dip"//in large 66*1.5=100(approx)
android:layout_width="52dip" //in small 66*0.75=52(approx)

Also new Qualifier has been introduced - SmallestWidth - AvailableScreenWidth - AvailableScreenHeight

read more about it here https://developer.android.com/guide/practices/screens_support.html

I hope this helps.

like image 23
Jayanth Avatar answered Oct 16 '22 09:10

Jayanth


May be you can try below library which manages all the screen size resolution automatically.

compile 'com.intuit.sdp:sdp-android:1.0.4'

You need to just add the dependency in your build.gradle file and you are done.

You need to specify like:

android:layout_height="@dimen/_10sdp"

Instead of usual:

android:layout_height="@dimen/10dp"
like image 20
Pranav Darji Avatar answered Oct 16 '22 08:10

Pranav Darji


If you provide text size as sp and other dimensions as dp. Android will automatically adjust for different layouts based on density of devices. If you want to control the values, you can put values in dimens.xml in each of values folders. The values folder inside res for different density devices will be following :

values-ldpi\dimens.xml
values-mdpi\dimens.xml
values-hdpi\dimens.xml
values-xhdpi\dimens.xml
values-xxhdpi\dimens.xml
values-xxxhdpi\dimens.xml
like image 2
Sangeet Suresh Avatar answered Oct 16 '22 09:10

Sangeet Suresh


Well that depends on your code

  1. Dont use Static values
  2. Try to use Wrap content
  3. Use Relative and Linear Layout Depends on your Requirement
  4. For Drawables Use every DPI Folder

Look at these Links

1)https://developer.android.com/training/multiscreen/screensizes.html

2)How to support different screen size in android

3)Supporting multiple screen size - Android

like image 1
Quick learner Avatar answered Oct 16 '22 09:10

Quick learner