Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Resource Name from Resource id

Tags:

android


In my layout I have defined something like this .

<RadioButton     android:id="@+id/radio1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="Dnt want this text" /> 

Assume that some function in activity returns me this id (id of radioButton). Now i want to get this text radio1 from this id. In short I want to retrieve text radio1 written in android:id="@+id/radio1"

Can somebody tell me how is it possible ?

like image 618
Code_Life Avatar asked Apr 13 '12 08:04

Code_Life


People also ask

How do I get resource ID?

int resourceId = this. getResources(). getIdentifier("nameOfResource", "id", this.

What is the use of resource ID in Android?

drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R. drawable. icon ). This integer is the resource ID that you can use to retrieve your resource.

What is a resource name?

The unique identifier for an entity in the Google Ads API is called a resource name, and is represented as a string with a predictable format. If you know the constituent components of a resource name, you can generate resource names using helper methods present on many Service objects.


2 Answers

In your Activity, try these:

  1. to get string like radio1:

    getResources().getResourceEntryName(int resid); 
  2. to get string like com.sample.app:id/radio1:

    getResources().getResourceName(int resid); 

In Kotlin Now :

val name = v.context.resources.getResourceEntryName(v.id) 
like image 92
Shubhayu Avatar answered Sep 22 '22 12:09

Shubhayu


You have id('long' type) from that id you want to access radio button id(name) that is radio1. You use this

getResources().getResourceEntryName(id); 

in using above you can get name of radio button i.e. radio1. here parameter id is which you have(long type). Try this it will helps you 100%.

like image 31
Anil Jadhav Avatar answered Sep 21 '22 12:09

Anil Jadhav