Reputation: 274
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?
Upvotes: 1
Views: 1870
Reputation: 16991
You would have to do something like this...
(TextView) this.findViewById(R.id.drinkname).setText("Water");
Use findView
to 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 id
attribute in XML, and added to the R
class inside R.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:
TextView tvGameDate;
TextView tvGameTime;
TextView tvHomeTeam;
TextView tvAwayTeam;
TextView tvHomePitcher;
Then near the start of onCreate I will populate them all with the references to the actial views like this:
tvGameDate = (TextView) this.findViewById(R.id.tvGameDate);
tvGameTime = (TextView) this.findViewById(R.id.tvGameTime);
tvHomeTeam = (TextView) this.findViewById(R.id.tvHomeTeam);
tvAwayTeam = (TextView) this.findViewById(R.id.tvAwayTeam);
tvHomePitcher = (TextView) this.findViewById(R.id.tvHomePitcher);
Then I can just refer to the fields I defined whenever I need to access them like this:
tvHomeTeam.setText(attributes.getNamedItem("home_long").getNodeValue());
tvHomePitcher.setText(" (" + attributes.getNamedItem("home_pitcher").getNodeValue() + ")");
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):
TextView tv = new TextView(context);
tv.setTextColor(Color.BLACK);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("Some text for the new view");
llIngredients.addView(tv);
Upvotes: 1
Reputation: 9157
Not sure I follow you. I'm guessing this resource represents an activity. You can get a reference to your text view, lets say glass_type
by saying -
TextView glassTypeTxtView = findViewById(R.id.glass_type)
and set its content dynamically by saying glassTypeTxtView.setText("new text")
.
Please correct me if I misunderstood your question completely.
Upvotes: 1