3

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?

Laur Ivan
  • 4,117
  • 3
  • 38
  • 62

1 Answers1

8

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);
Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
  • That's Ok, but the problem is that in some cases I have the "details" view and in some other cases I don't have it. – Laur Ivan Jul 07 '16 at 15:14
  • Yeah but that details view is added only if it is tablet right? If it is not a tablet the details view won't be there. – Arpit Ratan Jul 07 '16 at 15:17
  • yes, but my current code calls `detail.update()` in the case it's a tablet and the user pressed on an item from the list. Wouldn't I get a compile error if I try to use `binding.detail.update()`? Should I use another variable for the detail view (with its own binding)? – Laur Ivan Jul 07 '16 at 16:47
  • No there wont be any compilation error. Only change you need to do is : if(isTablet) details.update(); – Arpit Ratan Jul 07 '16 at 17:59
  • is `w900dp` the definitive way to do it these days? A few years ago it used to be `sw600dp`, see http://stackoverflow.com/a/40637603/550471 – Someone Somewhere Feb 19 '17 at 16:26
  • It depends on the requirement, if you see in the question @LaurIvan has provided alternate resources only for w900dp folder. Hence he will need to provide isTablet true only for the devices which is using alternate resource. – Arpit Ratan Feb 22 '17 at 13:04