tom91136
tom91136

Reputation: 8962

Android layout include no layout

In my app, portrait would have a Button where landscape would NOT have a Button, so I use <include> and did this:

layout/activity_main.xml

...
<include layout="@layout/my_button" />
...

values/layout.xml

...
<item name="my_button" type="layout">@layout/my_button_layout</item>
...

values-land/layout.xml

....
<item name="my_button" type="layout">@layout/empty_layout</item>

layout/my_button_layout.xml

...
<Button...... <!-- my button XML -->
...

layout/empty_layout.xml

<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />

Now, this works, but I don't quite like the idea of having an useless view whenever the app's in landscape.

I tried to do:

<item name="my_button" type="layout">@null</item>

but this would throw a ResourceNotFound Exception

Is there a way around this?

(Yes I know I can do this programmatically, but then it defeats the purpose of Android's layout system)

Upvotes: 0

Views: 296

Answers (1)

Stratus_Hunter
Stratus_Hunter

Reputation: 301

You could use two styles. One in an xml file in a portrait folder and the other in another xml file in the landscape folder. These styles could then set android:visibility="gone" and android:visibility="visible" then just set the style onto the button using style="@style/MyButtonStyle"

Upvotes: 1

Related Questions