Reputation: 4488
Hi im trying to get the ID i set on each of these linear layouts but instead im getting
android.widget.LinearLayout@41032a40 or similar which isn't much use to me.
I have set the id to row1, and thats what i would like to return.
Im sure I've done something similar before, so i cant figure out why it is returning the above.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/row1"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="@android:color/black"
android:onClick="xmlClickHandler" />
<LinearLayout
android:id="@+id/row2"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="@android:color/white"
android:onClick="xmlClickHandler" />
<LinearLayout
android:id="@+id/row3"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="@android:color/black"
android:onClick="xmlClickHandler" />
<LinearLayout
android:id="@+id/row4"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="@color/white"
android:onClick="xmlClickHandler" />
<LinearLayout
android:id="@+id/row5"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="@color/yellow"
android:onClick="xmlClickHandler" />
<LinearLayout
android:id="@+id/row6"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:background="@color/blue"
android:onClick="xmlClickHandler" />
public void xmlClickHandler(View v) {
Log.d("CLICK ROW", String.valueOf(v));
}
Upvotes: 6
Views: 18084
Reputation: 93
The Simple Solution which is just a one-line code. It can Fetch any id from the view.
String id=id=getResources().getResourceEntryName(view.getId());
Upvotes: 2
Reputation: 4001
View ID can be checked using v.getID()
You can simply check as
if(v.getId()==R.id.row1)
and perform your desired task accordingly.
Upvotes: 15
Reputation: 101
You can do it in this way:
public void xmlClickHandler(View v) {
String viewID = getResources().getResourceName(v.getId());
}
And than you have to parse the result string.
Upvotes: 10
Reputation: 169
Try to set android:tag="row1"
and use Log.d("CLICK ROW", String.valueOf(v.getTag()));
to print the id.
Upvotes: 6
Reputation: 4488
I've just seen another question, apparently you cannot return the id as a string.
Upvotes: 0
Reputation: 82553
Log.d("CLICK ROW", String.valueOf(v.getId()));
v
is an instance of the View class, and you can use the getId()
method to get the View's ID.
Upvotes: 1