I’m making a custom XML layout to display a cocktail recipe. Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/drinkname" android:layout_height="wrap_content"
android:textSize="30sp" android:text="@string/drink_name"
android:layout_width="wrap_content" android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView android:layout_width="fill_parent"
android:layout_height="2dp" android:background="#FFFFFFFF"
android:layout_below="@id/drinkname" />
<TextView android:layout_height="wrap_content" android:id="@+id/ingredient_label"
android:text="Ingredients: " android:layout_width="wrap_content"
android:layout_below="@+id/drinkname" android:layout_marginLeft="10dp"
android:layout_marginTop="16dp" />
<TableLayout android:layout_height="wrap_content"
android:id="@+id/ingredient_table" android:layout_width="wrap_content"
android:layout_alignTop="@+id/ingredient_label"
android:layout_alignLeft="@+id/drinkname"
android:layout_alignParentRight="true">
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="• 2 oz. Gin (Boodles)"android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="• 2 oz. Vodka (Stolichnaya)"
android:id="@+id/textView2" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="• 1/2 oz. Vermouth, dry" android:id="@+id/textView3"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</TableRow>
<TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="• 3 pieces Olive" android:id="@+id/textView4"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<TextView android:id="@+id/instruction_label"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_below="@id/ingredient_table" android:layout_alignLeft="@id/ingredient_label"
android:text="Instructions: " android:layout_marginTop="25dp" />
<TextView android:id="@+id/instructions"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignTop="@id/instruction_label"
android:layout_alignLeft="@id/drinkname"
android:layout_alignParentRight="true" android:text="@string/drink_instructions" />
<TextView android:id="@+id/glass_label" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@id/instructions"
android:layout_alignLeft="@id/ingredient_label" android:text="Glass:"
android:layout_marginTop="25dp" />
<TextView android:id="@+id/glass_type"
android:layout_toRightOf="@id/glass_label" android:text="@string/drink_glass"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_alignTop="@id/glass_label" android:layout_alignLeft="@+id/drinkname" />
As you can see all the content displayed is written into the layout itself or is taken from a string resource. My question is: what would I have to do to keep the layout I created below, but be able to fill in the content programmatically?
You would have to do something like this…
Use
findViewto get a reference to a particular view on the activity. Pass it the Resource identifier you gave in the XML (the id property). Cast it to the correct View type, and then set it’s properties.The resource id’s are created automatically from the
idattribute in XML, and added to theRclass insideR.id..As an example from the application I’m currently writing, I usually define the views I’m going to be modifying as class level fields near the beginning of the class like so:
Then near the start of onCreate I will populate them all with the references to the actial views like this:
Then I can just refer to the fields I defined whenever I need to access them like this:
In your XML it looks like you may actually need to dynamically create additional text views on the fly based on the recipe. You can do this like so (where llIngredients maps to some LinearLayout in your XML):