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

vestlen
  • 1,103
  • 11
  • 17
user2955394
  • 1,063
  • 4
  • 17
  • 34
  • Often you don't need to know as long as you provide the proper resource files. See [this answer](http://stackoverflow.com/a/41646301/3681880). – Suragch Jan 14 '17 at 03:47

5 Answers5

63

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

Ouadie
  • 13,005
  • 4
  • 52
  • 62
Tobliug
  • 2,992
  • 30
  • 28
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

Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
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.

user2759617
  • 597
  • 1
  • 8
  • 20
  • 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 '15 at 01:00
  • dont we need to multiply by 160 like px * 160)/dpi – H Raval Oct 15 '16 at 07: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);
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
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

Rustam
  • 6,485
  • 1
  • 25
  • 25
  • 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 '16 at 05:15
  • 7
    this will not return SW. it should be `screenWidth = dm.widthPixels /dm.density;` – VSB Sep 25 '16 at 05:39