Laur Ivan Laur Ivan - 4 months ago 25
Android Question

Programatically determining whether device is a Phone or Tablet

I've got an application with an activity containing 2 layouts:


  • layout/activity_main.xml - "phone mode", with a single view inside (a list)

  • layout-w900dp/activity_main.xml - "tablet mode" with 2 views (list and details)



Normally, I check the detail side of functionality with:

if (findViewById(R.id.application_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-w900dp).
// If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
}


How can I convert such approach to use data binding?

Answer

You could simply define a resource in your strings.xml file

In values/strings.xml.

<bool name="is_tablet">false</bool>

In values-w900dp/string.xml

<bool name="is_tablet">true</bool>

Access this resource from any where in your code. This should solve your issue.

For example :

 boolean isTablet = getResources().getBoolean(R.bool.is_tablet);
Comments