Bob
Bob

Reputation: 23000

How can I get the associated Id of a View as a string?

When you create a layout, you define an id for your views like this:

<ImageView
   android:id="@+id/myImage"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:src="@drawable/main01" />

and in your code you have:

ImageView img = (ImageView) findViewById(R.id.myImage);

now you have the object of img.

How can I get the id of img ("myImage") as a string? I need something like this:

String strId = getStringId(img);
// now strId is "myImage"

Upvotes: 0

Views: 65

Answers (2)

Chirag
Chirag

Reputation: 56925

Try this.

Log.i("============ Id","::"+getResources().getResourceEntryName(img.getId()));

Output : ============ Id :: myImage

Upvotes: 2

Zyber
Zyber

Reputation: 1034

Is this what you're looking for?

String value = getResources().getString(R.id.myImage);

Upvotes: 0

Related Questions