Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Avoid layout duplication

I have a huge layout file called visit_registration.xml. In order to support different screen sizes, I've duplicated the file and created the files layout-sw600dp/visit_registration.xml and layout-sw720dp/visit_registration.xml.

There is a single small difference between the layouts:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/app_margin"
    android:paddingRight="@dimen/app_margin">

    <TextView
        android:id="@+id/visitRegistrationTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/visit_registration" />

    <!-- TONS of UI elements here! -->
</LinearLayout>

The original layout file (above) shows the TextView with the id "visitRegistrationTextView".

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/app_margin"
    android:paddingRight="@dimen/app_margin">

    <TextView
        android:id="@+id/visitRegistrationTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:text="@string/visit_registration" />

    <!-- TONS of UI elements here! -->
</LinearLayout>

Each copy uses a different visibility. The example above uses "gone". Sometimes the changes are different, but small, like a UI element moved to another place in the layout. The rest of the code remains the same.

The main question is about layout duplication. I don't want to maintain three different layouts because of the classical duplication problems like "What would happen if I need to change a common UI element present in three files and forget to change it in a single place?"

Is it possible to have a single "base layout" with the common UI elements and include the small layout changes on it accordingly with the current screen size ?

like image 934
Thom Thom Thom Avatar asked Mar 24 '26 10:03

Thom Thom Thom


1 Answers

Try to make resources file eg. values-sw600dp/strings.xml, values-sw720dp/strings.xml with

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="visibility">gone</string>
</resources>

or

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="visibility">visible</string>
</resources>

and your layout file (only one):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/app_margin"
    android:paddingRight="@dimen/app_margin">

    <TextView
        android:id="@+id/visitRegistrationTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="@string/visibility"
        android:text="@string/visit_registration" />

    <!-- TONS of UI elements here! -->
</LinearLayout>
like image 79
dieter_h Avatar answered Mar 25 '26 23:03

dieter_h



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!