Is there anyway to retrieve the name of a view in android? Have been walking through the api and haven't seen any method to do this.
Asked
Active
Viewed 6,721 times
2 Answers
9
use view.getId();
, which is usually used in onClick(View v)
method
// from View.OnClickListener
@Override
public void onClick(View v){
switch( v.getId() ) { // <= this is it
case R.id.first_id:
// do something
break;
}
}

Marek Sebera
- 39,650
- 37
- 158
- 244
-
Well, this is not a solution for me. I have a Radio group and each radio button name ends with a "index" in its name. I need to retrieve this to do some processing. I can't rely on the real radio button group index. – Notbad Mar 12 '12 at 10:40
-
Post your code then, so we can think about solution for your case. This is common solution, looks like you're looking for something else. – Marek Sebera Mar 12 '12 at 10:42
-
Thinking a bit more about my problem, I think your solution could fit with some changes to my code. As an android newbie I was facing the problem from another point of view. Thanks. – Notbad Mar 12 '12 at 11:03
9
View only contains id not name, I think it's not able to retrieve it.
From your comment, I think you can put the index to the android:tag attribute
such as
<RadioButton android:tag="1" />
Then you can invoke button.getTag to fetch the tag value. Hope this helps you get rid of the switch case statements..

Qiang Jin
- 4,427
- 19
- 16