How to detect 7" Android tablet in code

三世轮回 提交于 2019-12-21 22:36:39

问题


I am trying to detect 7" tablets in my code (i.e. Kindle Fire & Nook Color). However, simply testing for minimum dimensions 1024x600 is not good, because then the Galaxy Nexus would pass this test as well. Anybody has experience with detecting this kind of information?

Thanks, Igor

EDIT: I have found one way to detect Kindle Fire & Nook Color devices with the following code:

Activity activity = (Activity) activityContext;
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
if ((width == 1024 && height == 600) || (width == 600 && height == 1024)) {    
        //Detects 7" tablets: i.e. Kindle Fire & Nook Color
        isTablet = true;
    }

回答1:


To calculate the height and width of the device (in inches) you can use the following code.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

float widthInInches = metrics.widthPixels / metrics.xdpi;
float heightInInches = metrics.heightPixels / metrics.ydpi;

Then the size can be calculated

double sizeInInches = Math.sqrt(Math.pow(widthInInches, 2) + Math.pow(heightInInches, 2));
//0.5" buffer for 7" devices
boolean is7inchTablet = sizeInInches >= 6.5 && sizeInInches <= 7.5; 

As per Commonsware suggestion above, a potentially faster, but less obvious implementation.

double sizeInInchesSquared = (widthInInches * widthInInches) + (heightInInches * heightInInches);
//0.5" buffer for 7" devices (6.5^2 = 42.25) (7.5^2 = 56.25)
boolean is7inchTablet = sizeInInchesSquared >= 42.25 && sizeInInchesSquared <= 56.25; 



回答2:


If it's the tablet you care about rather than dimensions, specifically: there is the Build class, which can tell you things like model and product.




回答3:


See the following. Seems to not work on all devices. If the two tablets are the only devices you are worried about, I'd give either implementation a try.

https://stackoverflow.com/a/10080537/300972

https://groups.google.com/group/android-developers/browse_thread/thread/cae5ff90157098b1?pli=1




回答4:


I know it's old thread but what about this way of:

public boolean isLargeScreen() {

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.device_screen, null);
    FrameLayout menu = (FrameLayout) view.findViewById(R.id.menu);


    if (left_menu != null) {
        return true;
    } else {
        return false;
    }
}

then:

if ( isLargeScreen() ) {
    // do something for large screen & extra large screen
} else {
    // do something for normal screen size
}

and xml layouts (for example):

res/layout/device_screen.xml // layout for normal screen size ("default")

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    [leave it blank]

</LinearLayout>

res/layout-large/device_screen.xml // layout for large screen size tablet 7'

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/menu"
        ... />

</LinearLayout>

res/layout-xlarge/device_screen.xml // layout for extra large screen size > tablet 10'

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/menu"
        ... />

</LinearLayout>

Tested on HTC Desire & Samsung Galaxy Note 10.1 - works great!



来源:https://stackoverflow.com/questions/10724338/how-to-detect-7-android-tablet-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!