ammar26
ammar26

Reputation: 1612

Get Identifier using ID in Android

I'm developing an android app in which when user clicks on a text view a listener is triggered. I can obtain the ID from the View.getId(); method. But is there is any way i can obtain its Unique Identifier String that i mentioned in XML file ? Like

public void onClickTV(View v)
    {
        int ID = v.getId();
    }

<TextView
 android:id="@+id/myTextView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Click Here"
 android:layout_weight="1"
 android:gravity="center" 
 android:onClick="onClickTV" 
 android:clickable="true" />

It gives me only ID which is integer, can i get the identifier through any method which is "myTextView"

Upvotes: 0

Views: 531

Answers (1)

goto10
goto10

Reputation: 4370

You can do this:

public void onClickTV(View v)
{
    int ID = v.getId();
    String myResourceName = getResources().getResourceEntryName(ID);
}  

That returns the the value you're looking for.

See the Resources documentation for other methods of reading resource properties.

Upvotes: 2

Related Questions