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).
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.