0

I have an application that inside resources folder has icons for tabHost (drawable-hdpi and drawable-mdpi with double resolution). That icons are called from proper folder if smartphone or tablet. However, I need to get icons from data/data path and not res/drawable folder. How could I discriminate then if smartphone or tablet in order to load proper image? Thank you.

Jaume
  • 3,672
  • 19
  • 60
  • 119

2 Answers2

1

You can do it by setting a threshold for screen size as per your requirements and then later use the following method to see if the device fulfills your threshold:

/**
 * Checks if the screen is above the given size
 * @param activity activity screen
 * @param screenSize size of screen to evaluate
 * @return True if its equal/above, else false
 */
public static boolean isTablet(Activity activity, int screenSize)
{
    Display display = activity.getWindowManager().getDefaultDisplay();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    display.getMetrics(displayMetrics);

    int width = displayMetrics.widthPixels / displayMetrics.densityDpi;
    int height = displayMetrics.heightPixels / displayMetrics.densityDpi;

    double screenDiagonal = Math.sqrt( width * width + height * height );
    return (screenDiagonal >= ((double)screenSize) );
}
waqaslam
  • 67,549
  • 16
  • 165
  • 178
0

You can also use the following code to check tablet or not

if(((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE)){
     //Then tablet 
}  

Refer getConfiguration and Configuration

Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112