Skip to content Skip to sidebar Skip to footer

Controlling View Visibility From A Resources

I have a layout that contains two ImageViews. I want one of them to be visible in portrait and the other in landscape. How do I achieve it using resources? (I know how to set it pr

Solution 1:

This is an old question that has already been accepted, but the following solution may help someone else:

If you check res/values/attrs.xml in Android source code, you'll see the definition of visibility property like this:

<!-- Controls the initial visibility of the view.  --><attrname="visibility"><!-- Visible on screen; the default value. --><enumname="visible"value="0" /><!-- Not displayed, but taken into account during layout (space is left for it). --><enumname="invisible"value="1" /><!-- Completely hidden, as if the view had not been added. --><enumname="gone"value="2" /></attr>

This attribute expects a string value (visible, invisible, gone) that will be converted to (0, 1, 2) respectively. So, you can declare integer resources containing these values like this:

values/integers.xml

<integername="visible_in_portrait">2</integer><!-- This is GONE -->

values-land/integers.xml

<integername="visible_in_landscape">0</integer><!-- This is VISIBLE -->

However, if you want to make it even better in order to stop guessing these numeric constants every time, you could do like this:

values/integers.xml

<!-- These are alias for the constants we'll reference below --><integername="view_visible">0</integer><!-- This is VISIBLE --><integername="view_invisible">1</integer><!-- This is INVISIBLE --><integername="view_gone">2</integer><!-- This is GONE --><integername="visible_in_portrait">@integer/view_gone</integer><!-- We're referencing the visibility alias we declared above -->

values-land/integers.xml

<integer name="visible_in_landscape">@integer/view_visible</integer>

You can use this approach or the one suggested by Keyhan. Choose the one that fits you better.

Solution 2:

it will be possible when you use this trick, add your visibility line to a style and put two instances of that file in -land and normal mode.

I mean in file styles.xml in folder values put a style with name s1, and put android:visibility=visible in that, and in styles.xml in folder values-land put a style with name s1, and put android:visibility=gone.

also, in file styles.xml in folder values put a style with name s2, and put android:visibility=gone in that, and in styles.xml in folder values-land put a style with name s2, and put android:visibility=visible.

and then, set s1 to first imageview and s2 to second.

solution given by dear Calvin is also correct, but when you have a complex layout that may change during time, having one layout file would be better, and will have less need to change.

Solution 3:

It is not possible. (I tried define fill_parent as string and use it for layout_width and it fails too.)

Why don't you use 2 layouts?

  1. res/layout/may_layout.xml
  2. res/layout-land/may_layout.xml

Which each define the correct imageview to show.

In addition, having values-land may cause you problem when you needs to support multi-languages. (You will need to have value-xx-land for each language)

Post a Comment for "Controlling View Visibility From A Resources"