Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the id of a view as String in onClick event

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));
}
like image 561
lorless Avatar asked Apr 25 '13 18:04

lorless


2 Answers

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.

like image 50
Narendra Singh Avatar answered Sep 22 '22 06:09

Narendra Singh


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.

like image 44
GrandeMajo Avatar answered Sep 22 '22 06:09

GrandeMajo