Reputation: 983
I have a table layout in xml with many rows and columns. I have given a particular tag to each and every column of the table.
Now I want to access a particular column based on some condition. How to I get the tag to match a string so that i can access the column?
for example, my xml code is like this
<TableLayout>
<TableRow
android:padding="10dp"
android:id="@+id/row1"
android:layout_height="wrap_content"
android:weightSum="100"
android:background="#ffffff"
android:layout_margin="1dp"
android:layout_weight="16.66">
<TextView
android:text="Day"
android:textStyle="bold"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="11.11" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="11.11"
android:id="@+id/tym1"
android:tag="1"
android:text="9:30" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="11.11"
android:id="@+id/tym2"
android:tag="2"
android:text="10:30" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="11.11"
android:id="@+id/tym3"
android:tag="3"
android:text="11:30" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="11.11"
android:id="@+id/tym4"
android:tag="4"
android:text="12:30" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="11.11"
android:id="@+id/tym5"
android:tag="5"
android:text="1:30" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="11.11"
android:id="@+id/tym6"
android:tag="6"
android:text="2:30" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="11.11"
android:id="@+id/tym7"
android:tag="7"
android:text="3:30" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="11.11"
android:id="@+id/tym8"
android:tag="8"
android:text="4:30" />
</TableRow>
</TableLayout>
as you can see, there is a tag to each column. In my code, when the user enters the tag value, i want to access the column that matches the tag value.
for example in the java class--
String test="4";
TextView column_var;
now i want to set column_var to the TextView column in xml that has the same tag as the test string.
How to achieve that? please help!!!
Upvotes: 3
Views: 6209
Reputation: 186
You can find the view with specific tag with findViewWithTag()
But you should use IDs instead, because:
It is generally preferable to use IDs (through the android:id attribute) instead of tags because they are faster and allow for compile-time type checking.
EDIT: The method findViewWithTag() is a member method of View class. You have to find the parent view like this
View parentView = findViewById( R.id.row1 );
Then you can find your textview
TextView column_var = parentView.findViewWithTag(test);
Upvotes: 7