Search code examples
androidxmlviewflipper

How to make xml definition to be re-usable (some kind of template)?



My situation is following. I have one Activity that contains three main parts. At the top of this activity there is an tools panel with two ImageButtons. At the bottom of the activity there is also tools panel similar to top.

Main component of this activity is at the center of the screen which is LinearLayout that contains 3x3 ImageButtons grid. To get the context I quickly describe purpose of these grid buttons.

This 3x3 grid describes states of this buttons (lets say toggle button and its states are on/off) for particular day. So for one day you have one set of 3x3 and these have its own state for particular day.

Now what I trying to achieve is to have functionality which allows user to scroll between grids or dates respectively. My idea is to have ViewFlipper as the main component of the activity (instead of LinearLayout) and one or more grid of buttons. This is what I already have. I am able to switch 3x3 to another 3x3 (with states) for another day. For this I simply copy/paste my 3x3 grid XML definition and put this as children to view flipper (x-times).

My idea is to have one definition of 3x3 and reuse it x-times like some kind of template so I will be able to say "I want to have another 3x3 with so and so states for this day and I want it to add this as next child to ViewFlipper".

For this purpose (to have component of 3x3) I simply made class which extends LinearLayour and within it I want to generate/reuse 3x3 definition for buttons. Sure I am able to do it by java code in this new class but is some way to do it with reusing of xml (which is better to maintain and to read)?

I tried to use LayoutInflater but this is probably not what I am looking for.

Thanks in advance

Edit

Here is code for main layout with flipper

<ViewFlipper android:id="@+id/view_flip" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#FF0000">
    <!-- Here goes 9x9 cubbie layout programaticaly -->
</ViewFlipper>

To this fipper I adding view with this layout (this code is assigned via LayoutInflater).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_weight="1" android:gravity="center">
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:orientation="vertical">
        <ImageButton android:id="@+id/life_att_1" style="@style/AttributeButton" />
        <TextView android:id="@+id/life_att_text_1" android:text="test"
            style="@style/AttributeText" />
    </LinearLayout>
    ...
</LinearLayout>
...
</LinearLayout>

EDIT 2

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background" android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:layout_weight="1" android:paddingLeft="16dip">

Solution

  • Only way I found here is to add paddingLeft attribute to wrapper LinearLayout (please refer to EDIT 2). I do not think that this is right solution but only solution I currently found.