30

I have 4 different devices:

  1. Asus tablet, screensize 7"
  2. Lenovo tablet, screensize 7"
  3. HTC mobile phone, screensize 5"
  4. HTC mobile phone, screensize 4.7"

I want to know the smallest width (sw) of my device to make a support layout for it.

I want to make a resource folder like "layout-sw600dp" but I don't know the value of the smallest width (sw).

I tried to print the sw using this code:

DisplayMetrics metrics = getResources().getDisplayMetrics();
Log.i("Density", ""+metrics.densityDpi);

but i don't know if this is the correct value.

How do I find the smallest width (sw)?

1
  • Often you don't need to know as long as you provide the proper resource files. See this answer.
    – Suragch
    Jan 14, 2017 at 3:47

5 Answers 5

64

Best solution for min API 13 and above:

Configuration config = getResources().getConfiguration();
config.smallestScreenWidthDp

The last line return the SW value in dp !

Source google : http://android-developers.blogspot.fr/2011/07/new-tools-for-managing-screen-sizes.html

22

Here is an another easy way .

You can check the Smallest Width of the Device from Developer Options (In Android Studio Emulator & It's not found in my Real Device) .

enter image description here

0
13

Correction to the above answer, the right code to find the correct sw try this:

DisplayMetrics dm = getApplicationContext().getResources().getDisplayMetrics();
float screenWidth = dm.widthPixels / dm.density;
float screenHeight = dm.heightPixels / dm.density;

whichever one of screenWidth and screenHeight is smaller is your sw.

2
  • It seems like this is the correct answer. The accepted answer gives me 2.79 on the Note 5. The Note 5 is grabbing values from my sw360dp folder, so that must not be the accurate way to determine the smallest width. Wish Google had the formula on an official doc somewhere just for reassurance instead of making me comb StackOverflow...
    – welshk91
    Sep 30, 2015 at 1:00
  • dont we need to multiply by 160 like px * 160)/dpi
    – H Raval
    Oct 15, 2016 at 7:47
10

Min api 13 and above go with @Tobliug answer- best solution.

 Configuration config = getResources().getConfiguration();
 config.smallestScreenWidthDp;// But it requires API level 13

Below API level 13 try this answer

Create SmallWidthCalculator.java class & just copy paste this code

import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;

 public class SmallWidthCalculator {
private static SmallWidthCalculator ourInstance = new SmallWidthCalculator();

public static SmallWidthCalculator getInstance() {
    return ourInstance;
}

private Context mContext;

private SmallWidthCalculator() {
}

public double getSmallWidth(Context context) {
    mContext = context;
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    int width = dm.widthPixels;
    int height = dm.heightPixels;


    double dpi = getDPI(width, height);


    double smallWidthDPI = 0;
    int smallWidth = 0;

    if (width < height)
        smallWidth = width;
    else
        smallWidth = height;

    smallWidthDPI = smallWidth / (dpi / 160);

    return smallWidthDPI;
}

private double getDPI(int width,
                      int height) {
    double dpi = 0f;
    double inches = getScreenSizeInInches(width, height);
    dpi = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)) / inches;
    return dpi;
}

private double getScreenSizeInInches(int width, int height) {
    if (mContext != null) {
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        double wi = (double) width / (double) dm.xdpi;
        double hi = (double) height / (double) dm.ydpi;
        double x = Math.pow(wi, 2);
        double y = Math.pow(hi, 2);
        return Math.sqrt(x + y);
    }
    return 0;
}

}

From your activity or Fragment just pass your context and get your small Width

double smallWidthDp=SmallWidthCalculator.getInstance().getSmallWidth(this);
6

you can try this:

DisplayMetrics dm = mActivity.getApplicationContext()
                    .getResources().getDisplayMetrics();
            float screenWidth = dm.widthPixels / dm.xdpi;
            float screenHeight = dm.heightPixels / dm.ydpi;

For Details :here

2
  • yes you can find out in runtime but you had to create your folder before compile. so what is the buckets for sw when you want to use sw instead of mdpi, hdpi and ... ?
    – Mahdi
    Aug 28, 2016 at 5:15
  • 7
    this will not return SW. it should be screenWidth = dm.widthPixels /dm.density;
    – VSB
    Sep 25, 2016 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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