5

I have a TextView in my activity that should be visible normally, but gone for tablet devices.

I know I can create a new layout file for the tablet, but that seems like a lot of duplication, so what I am trying to do is set in my (single) layout file something like...

android:visibility="@string/my_textview_visibility"

...for the TextView. And then, in my strings resources files, set...

<string name="my_textview_visibility">gone</string> (in values/strings.xml)

...and...

<string name="my_textview_visibility">visible</string> (in values-sw720dp/strings.xml)

...to hide the TextView for tablets.

However, when I try this, the app crashes when trying to show that activity.

Do I need to use the constant values instead of the above string values - e.g.,

"visible" -> 0

"gone" -> 8

..and, if so, what is the correct way to add/reference those values to/from my XML files?

Or is there another/better way of doing it?

NB - I do not want to show/hide the TextView programatically in my Java code.

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253

3 Answers3

9

You should be using /values/integers/ instead:

values/integers.xml

<integer name="my_textview_visibility">0</integer> <!-- 0 = View.VISIBLE -->

values-sw720dp/integers.xml

<integer name="my_textview_visibility">2</integer> <!-- 2 = View.GONE -->

Then called like so:

android:visibility="@integer/my_textview_visibility"
ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
  • Just for the sake of completion, you don't have to use `/values/integers/` nor `strings.xml`. You just need to define an android resource of type `integer` – Robert Estivill Mar 31 '17 at 16:13
  • Conventionally, where is the best place to declare these integers? – ban-geoengineering Mar 31 '17 at 16:14
  • 1
    I don't think there's a convention. A good idea might be having an `values/integers.xml` file with all your integers, similar to what is usually done with `values/strings.xml`. – Robert Estivill Mar 31 '17 at 16:16
6

ChrisStillwell's answer is correct. But the values are incorrect according to the Android Documentation

enter image description here

Robert Estivill
  • 12,369
  • 8
  • 43
  • 64
  • You are correct, the `8` value in my original answer gives an error. I didn't bother checking the values, just went off on what OP said. Thanks for the correction. – Chris Stillwell Mar 31 '17 at 16:15
  • I got the `8` from here: https://developer.android.com/reference/android/view/View.html#GONE – ban-geoengineering Mar 31 '17 at 16:17
  • 3
    I see, but you wanna use the attribute value and not the java constant value. https://developer.android.com/reference/android/view/View.html#attr_android:visibility – Robert Estivill Mar 31 '17 at 16:19
0

As you said, the best way to do this is to create a specific layout for tablets where your TextView will be hidden. However, it is possible to do this using xml boolean resources : (as res/values-sw600dp/):

<resources>
    <bool name="isTablet">true</bool>
</resources>

Because the sw600dp qualifier is only valid for platforms above android 3.2. If you want to make sure this technique works on all platforms (before 3.2), create the same file in res/values-xlarge folder:

<resources>
    <bool name="isTablet">true</bool>
</resources>

Then, in the "standard" value file (as res/values/), you set the boolean to false:

<resources>
    <bool name="isTablet">false</bool>
</resources>

Then in you activity, you can get this value and check if you are running in a tablet size device:

boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
    // do something
} else {
    // do something else
}

Font : Determine if the device is a smartphone or tablet?

Community
  • 1
  • 1
Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42