Detect 7 inch and 10 inch tablet programmatically

2020-01-24 06:16发布

Is there a way to programmatically find whether the device the app is installed on is a 7 inch tablet or a 10 inch tablet?

12条回答
神经病院院长
2楼-- · 2020-01-24 06:45

They way that Android specifies screen sizes is through four generalized sizes: small, normal, large and xlarge.

While the Android documentation states that the size groups are deprecated

... these size groups are deprecated in favor of a new technique for managing screen sizes based on the available screen width. If you're developing for Android 3.2 and greater, see [Declaring Tablet Layouts for Android 3.2]( hdpi (high) ~240dpi) for more information.

Generally the size qualifier large specifies a 7" tablet. And a size qualifier of xlarge specifies a 10" tablet:

enter image description here

The nice thing about triggering on the the size qualifier, is that you can guarantee that your assets and code are in agreement on which asset to use or code path to activate.

To retrieve the size qualifier in code make the following calls:

int sizeLarge = SCREENLAYOUT_SIZE_LARGE // For 7" tablet
boolean is7InchTablet = context.getResources().getConfiguration()
    .isLayoutSizeAtLeast(sizeLarge);

int sizeXLarge = SCREENLAYOUT_SIZE_XLARGE // For 10" tablet
boolean is10InchTablet = context.getResources().getConfiguration()
    .isLayoutSizeAtLeast(sizeXLarge);
查看更多
一纸荒年 Trace。
3楼-- · 2020-01-24 06:45

Voila!

查看更多
贼婆χ
4楼-- · 2020-01-24 06:48

Great information, just what I was looking for! However, after trying this out I found that when using the metrics mentioned here the Nexus 7 (2012 model) reports having dimensions 1280x736. I also have a Motorola Xoom running Jelly Bean and it incorrectly reports a resolution of 1280x752. I stumbled upon this post here that confirms this. Basically, in ICS/JB the calculations using the metrics mentioned above appear to exclude the dimensions of the Navigation Bar. Some more research led me to Frank Nguyen's answer here that uses different methods that will give you the raw (or real) pixel dimensions of the screen. My initial testing has shown that the following code from Frank correclty reports the dimensions on the Nexus 7 (2012 model runnin JB) and my Motorola Xoom running JB:

int width = 0, height = 0;
final DisplayMetrics metrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
Method mGetRawH = null, mGetRawW = null;

try {
    // For JellyBeans and onward
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        display.getRealMetrics(metrics);

        width = metrics.widthPixels;
        height = metrics.heightPixels;
    } else {
        mGetRawH = Display.class.getMethod("getRawHeight");
        mGetRawW = Display.class.getMethod("getRawWidth");

        try {
            width = (Integer) mGetRawW.invoke(display);
            height = (Integer) mGetRawH.invoke(display);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
} catch (NoSuchMethodException e3) {
    e3.printStackTrace();
}
查看更多
贼婆χ
5楼-- · 2020-01-24 06:50

Another way:

  • Create 2 more folders: values-large + values-xlarge

  • Put: <string name="screentype">LARGE</string> in values-large folder (strings.xml)

  • Put: <string name="screentype">XLARGE</string> in values-xlarge folder (strings.xml)

  • In code:

    String mType = getString(R.string.screentype);

    if (mType != null && mType.equals("LARGE") {

    // from 4~7 inches

    } else if (mType != null && mType.equals("XLARGE") {

    // from 7~10 inches

    }

查看更多
我只想做你的唯一
6楼-- · 2020-01-24 06:52

The above doesn't always work when switching portrait vs. landscape.

If you are targeting API level 13+, it is easy as described above -- use Configuration.smallestScreenWidthDp, then test accordingly:

resources.getConfiguration().smallestScreenWidthDp

Otherwise, if you can afford this, use the following method which is a very accurate approach to detect 600dp (like 6") vs. 720dp (like 10") by letting the system tell you:

1) Add to layout-sw600dp and layout-sw720dp (and if applicable its landscape) an invisible view with proper ID, for example:

For 720, on layout-sw720dp:

<View android:id="@+id/sw720" android:layout_width="0dp" android:layout_height="0dp" android:visibility="gone"/>

For 600, on layout-sw600dp:

<View android:id="@+id/sw600" android:layout_width="0dp" android:layout_height="0dp" android:visibility="gone"/>

2) Then on the code, for example, the Activity, test accordingly:

private void showFragment() {
    View v600 = (View) findViewById(R.id.sw600);
    View v720 = (View) findViewById(R.id.sw720);
    if (v600 != null || v720 !=null)
        albumFrag = AlbumGridViewFragment.newInstance(albumRefresh);
    else
        albumFrag = AlbumListViewFragment.newInstance(albumRefresh);
    getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.view_container, albumFrag)
        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
        .commit();
}
查看更多
叼着烟拽天下
7楼-- · 2020-01-24 06:53

I have two android device with same resolution

Device1 -> resolution 480x800 diagonal screen size -> 4.7 inches

Device2 -> resolution 480x800 diagonal screen size -> 4.0 inches

It gives both device diagonal screen size -> 5.8

the solution to your problem is..

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)width/(double)dens;
double hi=(double)height/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);

see details here..

查看更多
登录 后发表回答