-2

Suppose I define a View with an Id like this, in an Android XML layout file:

<View
    android:id="@+id/my_view_id"
    android:layout_height="match_parent"
    android:layout_width="wrap_content" />

In Java code, I can retrieve the Id of the View as an int, like this:

int id = view.getId(); // This returns something like 234567890

But I cannot tell what this object is from the number only, so I want to read the String representation of the int, like "my_view_id", as per the original XML file.

Is there a way to do this? Can I convert the int id to a human-readable name of the id?

Mr-IDE
  • 7,051
  • 1
  • 53
  • 59
max
  • 5,963
  • 12
  • 49
  • 80

2 Answers2

3

thanks for @David Medenjak this is Answer

getResources().getResourceEntryName(int resid);
max
  • 5,963
  • 12
  • 49
  • 80
  • There is also `getResources().getResourceName()` which will return the full package name, like `"com.company.MyApp:id/button_login"` – Mr-IDE May 28 '20 at 15:09
0

There is no way to do this as far as I know. If you want to get the string you used in "@+id/myviewid", you could do something like android:tag="myviewid". This will store your view id name in the views tag, which can be accessed via view.getTag().

A. Steenbergen
  • 3,360
  • 4
  • 34
  • 51