This seems like a simple question that I should be able to find easily but I’m having trouble understanding exactly how to create a reusable UI component for Android. Specifically I’m trying to create this snippet that I use EVERYWHERE into a reusable component to make the code smaller.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="0dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textColor="@color/text_normal"
android:id="@+id/my_text_id"
android:text="My field"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="@color/text_normal"
android:text="My value"
android:id="@+id/my_value_id"/>
</RelativeLayout>
These are used like this
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background_normal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="0dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textColor="@color/text_normal"
android:id="@+id/my_text_id"
android:text="My field"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="@color/text_normal"
android:text="My value"
android:id="@+id/my_value_id"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="0dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textColor="@color/text_normal"
android:id="@+id/my_text_id2"
android:text="My field2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="@color/text_normal"
android:text="My value2"
android:id="@+id/my_value_id2"/>
</RelativeLayout>
.... etc ....
</LinearLayout>
</ScrollView>
I’m hoping to be able to access both text boxes to assign values inside that will be added with the <include /> tag to a layout. Does anyone know how to do this?
I’ve seen http://developer.android.com/resources/articles/layout-tricks-reuse.html but I don’t seem to see how to assign multiple of the same field.
Any help is appreciated.
Update These are used to put a TextView on the left of the screen and another on the right that are lined up together. Not sure how to describe it, but these are used in a ScrollView.
Use
<include />include tag as stated in Android documentationTo find the component inside your layout:
where
R.id.includedComponentis a name of the included component.