0

I have a series of buttons in my mainactivity and when i click one of them i want to display their id but using view.getId() i get an integer and converting it to string i get a string of numbers,how can i get the id that i written on the xml?

public void giocaCarta(View view){
    Toast.makeText(this,Integer.toString(view.getId()),Toast.LENGTH_LONG).show(); }

this is the code of the button:

<Button
            android:id="@+id/btn_g2_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="giocaCarta"
            android:text="Button" />

i want to read on the toast "btn_g2_1" and not a sequence of numbers,there is some method that can provide this?

2 Answers2

1

You can use this:

String viewIdName = getResources().getResourceEntryName(myView.getId())
S-Sh
  • 3,564
  • 3
  • 15
  • 19
1

I wondering why you need that but for getting that id Use this code

view.getResources().getResourceName(view.getId());
Radesh
  • 13,084
  • 4
  • 51
  • 64