Seth R.
Seth R.

Reputation: 25

Is there a way to programmatically create copies of a layout in android?

The main goal is to use a pre-made layout to create separate modules that can be edited, and then programmatically add them to the root layout. To clarify, several modules stuck together would look like this. I would like to dynamically create each clickable block that consists of a name, date, and letter. The .axml code for each block is as follows:

        <RelativeLayout
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/borderLayout"
            android:background="@drawable/line"
            android:paddingBottom="1dp"
            android:paddingTop="1dp">
            <RelativeLayout
                android:id="@+id/relativeLayout1"
                android:padding="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:background="#ff2f2f2f">
                <TextView
                    android:text="C"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView1"
                    android:layout_alignParentLeft="true"
                    android:textSize="30dp" />
                <TextView
                    android:text="Washington"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/textView1"
                    android:id="@+id/textView2"
                    android:layout_alignParentRight="true"
                    android:gravity="right" />
                <TextView
                    android:text="6-8-17"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/textView2"
                    android:id="@+id/textView3"
                    android:gravity="right"
                    android:layout_alignParentRight="true" />
            </RelativeLayout>
        </RelativeLayout>

The main problem I am having is formatting the views programmatically in the same way that I formatted them in the .axml file.

Upvotes: 1

Views: 1513

Answers (2)

SushiHangover
SushiHangover

Reputation: 74134

Lets assume you have a LinearLayout with an orientation of vertical in your main axml that you wish to attach multiple views to.

Get a reference to that "parent" LinearLayout:

var linearLayoutParent = FindViewById<LinearLayout>(Resource.Id.linearLayout1);

Then in some loop, use LayoutInflater.Inflate to inflate your repeating layout, use the view returned and FindViewById on that View each of the elements you need to update and then add that view to the parent view with an increasing index:

index++;
var view = LayoutInflater.Inflate(Resource.Layout.RepeatingLayout, linearLayoutParent, false);
var letter = view.FindViewById<TextView>(Resource.Id.textView1);
letter.Text = index.ToString();
// FindViewById for textView2, textView3 and assign the text on each....
linearLayoutParent.AddView(view, index);

enter image description here

Note: If you have a lot of these repeating elements and you will have to scroll them (off screen), look at using a RecyclerView instead, it will save you a lot of headaches into terms of memory management, scrolling performance, etc... ;-)

Upvotes: 2

Yuri S
Yuri S

Reputation: 5370

Use inflater to create a view from resource. Then you can add it programmatically

context.LayoutInflater.Inflate(Resource.Layout.oneimg_twolbl, null);

Upvotes: 2

Related Questions