How to know smallest width (sw) of an android device?
Asked Answered
A

5

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

Amaryllis answered 23/10, 2014 at 4:1 Comment(1)
Often you don't need to know as long as you provide the proper resource files. See this answer.Damask
S
6

you can try this:

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

For Details :here

Stuckup answered 23/10, 2014 at 4:26 Comment(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 ... ?Ontologism
this will not return SW. it should be screenWidth = dm.widthPixels /dm.density;Tenor
T
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

Temperate answered 30/3, 2016 at 9:25 Comment(0)
V
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

Vested answered 17/4, 2017 at 10:58 Comment(0)
B
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.

Bedrail answered 3/6, 2015 at 20:11 Comment(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...Chevrette
dont we need to multiply by 160 like px * 160)/dpiCusco
M
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);
Maryammaryann answered 26/2, 2017 at 17:15 Comment(0)
S
6

you can try this:

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

For Details :here

Stuckup answered 23/10, 2014 at 4:26 Comment(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 ... ?Ontologism
this will not return SW. it should be screenWidth = dm.widthPixels /dm.density;Tenor

© 2022 - 2024 — McMap. All rights reserved.