0

I need to be able to provide a different UI for phones than tablets on Android. But I've been having a heck of a time figuring out a simple, straightforward way to do a runtime check of the screen size/resolution. I've searched quite a bit, and all the answers I get either tell me the screen density, which isn't what I want, or fall in the category of "use dp" or "put different images in the drawable folders." None of those quite cover what I need. All the answers I've seen searching StackOverflow have either fallen into the above categories, or have given answers with deprecated solutions.

I thought I had a workable solution:

Configuration configuration = getResources().getConfiguration();
boolean bigScreen = configuration.screenLayout == configuration.SCREENLAYOUT_SIZE_LARGE
    || configuration.screenLayout == configuration.SCREENLAYOUT_SIZE_XLARGE;

if(bigScreen)
{
    Intent userCreationIntent = new Intent(getApplicationContext(), AboutUs.class);
    startActivityForResult(userCreationIntent, 0);
}
else
{
    Intent userCreationIntent = new Intent(getApplicationContext(),AboutUsSmall.class);
    startActivityForResult(userCreationIntent,0);
}

The problem is that this code is also sending my Nexus 7, a 7-inch tablet, to the AboutUsSmall layout/class.

What am I doing wrong? More to the point, how can I do this right?

Andy Isbell
  • 259
  • 1
  • 2
  • 10
  • @ChiefTwoPencils: Thanks, but I need to be able to use entirely different layout/class pairs because there are functional differences due to both phone display and memory constraints. – Andy Isbell Jul 22 '14 at 05:50
  • You can write different functionality in the same class just check whether it is a tab or not and vary your functionality accordingly – George Thomas Jul 22 '14 at 05:56
  • Check out this : http://stackoverflow.com/questions/24709630/multiple-screen-support-for-my-app/24712380#24712380 – Haresh Chhelana Jul 22 '14 at 09:07

3 Answers3

3

Change it to :

public static boolean isLargeScreen(Context context)
{
    return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

This returns true for Nexus 7.change SCREENLAYOUT_SIZE_XLARGE to SCREENLAYOUT_SIZE_LARGE

Arash GM
  • 10,316
  • 6
  • 58
  • 76
1

I'm guessing you have very good reasons to avoid the android standard for different screen sizes. I believe that Arash has the cleaner solution, but this is another solution that probably better answers the question that you asked in the title:

DisplayMetrics dm = getResources().getDisplayMetrics();

double density = dm.density * 160;
double x = Math.pow(dm.widthPixels / density, 2);
double y = Math.pow(dm.heightPixels / density, 2);
double screenInches = Math.sqrt(x + y);
log.info("inches: {}", screenInches);

This and this post explains it pretty well. Good luck!

Community
  • 1
  • 1
Rawa
  • 13,357
  • 6
  • 39
  • 55
0

For Different screen size, The following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for small, medium, high, and extra high density screens. you could use different size of the layout files in res folder and also vary for drawable images based on the density..

res/layout/my_layout.xml             // layout for normal screen size ("default")
  res/layout-small/my_layout.xml       // layout for small screen size
  res/layout-large/my_layout.xml       // layout for large screen size
  res/layout-xlarge/my_layout.xml      // layout for extra large screen size
  res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

Android will automatically open the particular layout corresponding to the screen size. You can take a look at the developers site about multiscreen here

George Thomas
  • 4,566
  • 5
  • 30
  • 65