How to know smallest width (sw) of an android device?-ThrowExceptions

Exception or error:

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)?

How to solve:

you can try this:

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

For Details :here


Best solution for min API 13 and above:

Configuration config = getResources().getConfiguration();

The last line return the SW value in dp !

Source google :


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


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.


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 class & just copy paste this code

import android.content.Context;
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;
        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);

Leave a Reply

Your email address will not be published. Required fields are marked *