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 programmatically for but this specific use I need to achieve it using resources).

I tried something like

in res/layout/may_layout.xml:

...
<ImageView
      android:id="@+id/image1"
      android:visibility="@integer/visible_in_portrait"   <<-- is this allowed?
      ...
/>
<ImageView
      android:id="@+id/image2"
      android:visibility="@integer/visible_in_landscape"   
      ...
/>

in res/values/integers.xml:

...
<!-- NOTE: 0 and 8 are VISIBLE and GONE respectively -->
<integer name="visibile_in_portrait">0</integer>
<integer name="visibile_in_landscape">8</integer>

in res/values-land/integers.xml:

...
<integer name="visibile_in_portrait">8</integer>
<integer name="visibile_in_landscape">0</integer>

But I get a runtime error (index out of bound) when trying to inflate the images. When I remove the android:visibility statements, the program runs but I see both images.

Q: What is the way to use a resource as a value for the android:visibility attribute?

(if you wonder why setting it programmatically will not help me, it has to do with automatic landspace/portrait switch of app widgets with file uri bitmaps).

border visibility controlling

I want to know if I can control the border visibility of canvas. and other components. I mean can I enable the border in specific place and disable it in others. In my task I want to show the border

Controlling Visibility of ListView Footer

Scenario: Keep reference to footer in activity. set footer visibility to GONE. add footer to ListView. ListView.setAdapter(adapter). Now how can I get footer to show? I tried setting visibility back V

Controlling a button text from another view

I have two views and i used two view controllers. From the main view i want to initialize the button in the second view with a certain text. If I press the button in the second view, then i can store

Controlling visibility of enum values

Consider a C++ class that exports an enum, maintains an internal array over that enum, and wants to export a command that accepts values from the enum. class foo { public: enum color { red, yellow, gr

How to access My.Resources from a razor view

I have a razor .vbhtml view and would like to use string resources normally accessible via My.Resources. Seems like I can only get to My.Computer, My.Log and couple other namespaces from the view, but

MonoDroid Toggle View Visibility

I’d like to be able to show or hide a tableview with the click of a button. I know how to set the visibility of the view, just not how to toggle back and forth. Edit: Another option that seems to work

Controlling activity instance visibility through a service?

I have an activity which can be launched from two sources. Each source launches it’s own instance of the activity: 1. From inside an application 2. From an external service My problem is with the serv

Android View Visibility GONE

If i set a views visibility which as been inflated, to gone, will it Speed up my UI?

Difference between removing a view and setting it’s visibility as GONE?

What is the difference between removing a view from it’s parent and setting it’s visibility as GONE?

Class controlling other view

I have a view controller with this code on the ViewDidLoad… UIImage *imageU = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@transparent ofType:@png]]; TouchImage

Answers

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)

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.

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

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

values/strings.xml

<string name="visible_in_portrait">2</string> <!-- This is GONE -->

values-land/strings.xml

<string name="visible_in_landscape">0</string> <!-- 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/strings.xml

<!-- These are alias for the constants we'll reference below -->
<string name="view_visible">0</string> <!-- This is VISIBLE -->
<string name="view_invisible">1</string> <!-- This is INVISIBLE -->
<string name="view_gone">2</string> <!-- This is GONE -->

<string name="visible_in_portrait">@string/view_gone</string> <!-- We're referencing the visibility alias we declared above -->

values-land/strings.xml

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

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