30

I am developing a program on android version2.2. I have read many documentation on supporting multiple screen sizes but still confused. I designed a layout file, that supports for large and normal screens, when am trying it with small screen it is not adjusting the layout to fit the screen. I used this code in the manifest also.

<supports-screens 
        android:resizeable="true"
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:anyDensity="true"
        />

The image for the small screen is here. How can I set the screen that compatible with small screen? Somewhere I found by using the folder "layout-small" but if I use this, the project size is increasing, I don't want that, so can any one suggest me the best way to do this?

2

8 Answers 8

39

Solution for all screen and support all layout.

Icons:

                                 mdpi        hdpi       xhdpi       xxhdpi       xxxhdpi  
Launcher Icons (App Icons)      48 x 48     72 x 72    96 x 96     144 x 144    192 x 192
Action Bar,Toolbar,Tab Icons    24 x 24     36 x 36    48 x 48      72 x 72      96 x 96 
Notification Icons              24 x 24     36 x 36    48 x 48      72 x 72      96 x 96 

Background Image Resolution:

ldpi:    Portrait: 240 X 320px.      Landscape: 320 X 240px.
mdpi:    Portrait: 320 X 480px.      Landscape: 480 X 320px.
hdpi:    Portrait: 480 X 800px.      Landscape: 800 X 480px.
xhdpi:   Portrait: 640 X 960px.      Landscape: 960 X 640px.
xxhdpi:  Portrait: 960 X 1600px.     Landscape: 1600 X 960px.
xxxhdpi: Portrait: 1280 X 1920px.    Landscape: 1920 X 1280px.  

Drawable Folder:

res/drawable        (default)
res/drawable-ldpi/  (240x320 and nearer resolution)
res/drawable-mdpi/  (320x480 and nearer resolution)
res/drawable-hdpi/  (480x800, 540x960 and nearer resolution)
res/drawable-xhdpi/  (720x1280 - Samsung S3, Micromax Canvas HD etc)
res/drawable-xxhdpi/ (1080x1920 - Samsung S4, HTC one, Nexus 5, etc)
res/drawable-xxxhdpi/ (1440X2560 - Nexus 6,Samsung S6edge).


ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi

Layout:

Portrait: 
res/layout/main_activity.xml           # For handsets (smaller than 600dp available width)
res/layout-large/main_activity.xml     # For small tablets (640dp x 480dp and bigger)
res/layout-xlarge/main_activity.xml    # For large tablets (960dp x 720dp and bigger)
res/layout-w600dp/main_activity.xml    # For 7” tablets or any screen with 600dp
                                       # available width (possibly landscape handsets)

Landscape:
res/layout-land/main_activity.xml           # For handsets in landscape
res/layout-sw600dp-land/main_activity.xml   # For 7” tablets in landscape

Refer links:

Different resolution support android

https://developer.android.com/guide/practices/screens_support.html

https://design.google.com/devices/

Is there a list of screen resolutions for all Android based phones and tablets?

http://www.emirweb.com/ScreenDeviceStatistics.php

Most popular screen sizes/resolutions on Android phones

22

Please go through the following links. These might help you:

Supporting Different Screen Sizes

Supporting Multiple Screens

Supporting Different Densities

Supporting Tablets and Handsets

AFAIK, the only way to support all screens is by doing that folder bifurcation. Every XML file goes up to a few kilo bytes. So, size shouldn't be too much of an issue as such.

5
  • 1
    Thank you, it,s working fine, but i want to know is there any better way to support the screen sizes without bifurcation the folders?
    – wolverine
    Commented Feb 29, 2012 at 10:33
  • You're welcome. AFAIK, there isn't a work around. This is probably the only way. If there were any other way of doing it, then the method would've been incorporated in the android developers' website.
    – Ghost
    Commented Feb 29, 2012 at 10:53
  • 1
    That solution works perfectly. But my concern here is, when I have an android application having 8-9 screens, then it means that I will have 8-9 different .xml layout files. Now to support all screens by folder bifurcation , it means I have manage almost above fifty xml files for layouts and for a simple change in UI have to go to all the folders and implement that change in xml file. So can there be a better way , I mean such a layout that can just resize the controls by itself or something like that? Commented Mar 6, 2014 at 9:56
  • @MuhammadSalmanFarooq: Actually, I left Android development 2 years ago.. So, I could only speculate now.. I would suggest you to search online or come up with another question on SO that could answer this query of yours.
    – Ghost
    Commented Mar 10, 2014 at 7:23
  • What is bifurcation? Commented Mar 28, 2019 at 11:31
12

Yes i have got the cure to your problem, you are right i personally think that making layouts for every screen resolution is time taking and making your project size go big.

To make a layout that fits across all screen resolution i have implemented my own technique i.e setting width and height in terms of percentage

The Problem occurs when we set Views/Layouts with some constant width or height value lets say 100dp.

Solution is quite simple try to use match_parent so that the view fill up empty space or use weight and define every View relative to other Views this will help your layout to look good in almost every screen resolutions and at run time set LayoutParams of only those Views/Layouts that has some constant width or height in terms of Percentage.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/mLayout"
        android:layout_width="280px"
        android:layout_height="300px" />


</RelativeLayout>

Notice: I have used px for fixed sized layout's width/height because in LayoutParams layoutParams = new LayoutParams(int width, int height); the width and height take value as pixels

Here is an example code

final ViewTreeObserver mLayoutObserver = mLayout.getViewTreeObserver();

    mLayoutObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() 
    {

        @Override
        public void onGlobalLayout() 
        {
            DisplayMetrics metrics = getResources().getDisplayMetrics();

            int deviceWidth = metrics.widthPixels;

            int deviceHeight = metrics.heightPixels;

            float widthInPercentage =  ( (float) 280 / 320 )  * 100; // 280 is the width of my LinearLayout and 320 is device screen width as i know my current device resolution are 320 x 480 so i'm calculating how much space (in percentage my layout is covering so that it should cover same area (in percentage) on any other device having different resolution

            float heightInPercentage =  ( (float) 300 / 480 ) * 100; // same procedure 300 is the height of the LinearLayout and i'm converting it into percentage

            int mLayoutWidth = (int) ( (widthInPercentage * deviceWidth) / 100 );

            int mLayoutHeight = (int) ( (heightInPercentage * deviceHeight) / 100 );

            LayoutParams layoutParams = new LayoutParams(mLayoutWidth, mLayoutHeight);

            mLayout.setLayoutParams(layoutParams);
        }
    });

I guess the code is pretty much self explanatory if any one still need help you can ask right away

Conclusion: If you need to set some constant width/height for your Views/Layouts always set value in px in layout file (i.e xml) and then programmatically set LayoutParams.

Suggestion: I think Google Android Guys should seriously think of replacing the dp/dip units to percentage.

7
  • Muhammad Babar you are partially right.However what about the icons where u cannot apply nine patch and stretch them uniformly in such cases u have to define layouts for different screens to take diferent images.
    – Raj
    Commented Jul 11, 2013 at 7:50
  • @joy i guess you have drawable folders ldpi, mdpi, hdpi, why not use them Commented Jul 11, 2013 at 9:23
  • Can you please refer this question of mine i tried referring the images from these folders and created different layouts for the same xml file but these images were not displayed according to the screen resolution.stackoverflow.com/questions/17590254/…
    – Raj
    Commented Jul 11, 2013 at 10:56
  • @MuhammadBabar please help me! I get an error when i try to add your method to my xml. "Current Android SDK version does no support RelativeLayout as XML tag. I am using Flash CS6 and Flash Develop AS3 to create this Android AIR application. My SDK is version 4.0. It is the newest SDK.
    – Nathan
    Commented Mar 20, 2014 at 21:43
  • 2
    I created a new size unit that is effectively the percentage you wanted, you can find it on github. Commented Feb 18, 2015 at 14:10
8

Now to support different screen size is more easy! Use new size unit SDP.

SDP - a scalable size unit

An android SDK that provides a new size unit - sdp (scalable dp). This size unit scales with the screen size. It can help Android developers with supporting multiple screens.

for text views please refer to ssp which is based on the sp size unit for texts.

https://github.com/intuit/sdp

3
  • 2
    One of the best solutions I came across. Just include "implementation 'com.intuit.sdp:sdp-android:1.0.6'" to your dependencies and you can right away start using. +1 for this answer
    – Arjun
    Commented Aug 16, 2019 at 12:01
  • I cant seem to find the "ssp" unit for text, only the ssp. Doesnt matter for now but do I need different libraries for the two units?
    – Kleysley
    Commented Jul 24, 2021 at 10:44
  • I tried have added the library but when i add the dimension to my layout, i get a resource not found error message Commented Sep 15, 2021 at 11:47
1

Scalable DP (sdp) is the best solution to use. It will resize the widgets depends on the screen size. We can also apply this for text for different text sizes.

To add sdp to your project (Using Android Studio and Gradle):

add implementation 'com.intuit.sdp:sdp-android:1.0.5' to your build.gradle dependencies block.

for example:
dependencies {'com.intuit.sdp:sdp-android:1.0.5' }
1
  • has anyone else used it and has feedback regarding the same.. I am using it seems very easy to use only problem is you can't have perfect control over the sizes...
    – DragonFire
    Commented Oct 22, 2019 at 3:03
0

try this one

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="1:1"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    android:src="@drawable/myimage"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"/>

0

Try this library is awesome

implementation 'com.intuit.ssp:ssp-android:1.0.6'

Example

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sign up"
        android:textSize="@dimen/_16ssp" />
-1

I'm not sure that the solution but I want sharing with you will work everywhere, it covers most of the area.

I've solve this problem by partitioning a screen by used LinearLayout and define its orientation (horizontal or vertical), layout_weightsum = 3 (3 is to divide a screen into 3 parts).

Then inside that Root Linear layout, I again take LinearLayout with layout_height = "0dp" (if you want vertical partition only otherwise layout_height = "wrap_content" and if you need horizontal then make layout_width = "0dp" and layout_weight =1 (1 is for 1 part of the screen you can also put the value in float like 1.2).

Can try this it helps me, maybe and hope will help you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.