How to get the name of a button in Android - android

I need to get the name of a Button which I gave it with
android:id="#+id/button2"
I tried with button.getId() but then I only get this number: 2311296256.
Also .getTag() doesn´t work (I get a null value)
Does anybody know the solution?

getResources().getResourceEntryName(int id)
is what you are looking for. From the doc:
Return the entry name for a given resource identifier.
here the documentation.

So you want to get resource identifier name from resource identifier number. This should work:
getResources().getResourceEntryName(button.getId());

If you want to use getTag() you have to give the Button a tag first like this:
android:tag="sometag"

Related

Is there a way to programmatically get the id of an attribute? [duplicate]

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 ?
In your Activity, try these:
to get string like radio1:
getResources().getResourceEntryName(int resid);
to get string like com.sample.app:id/radio1:
getResources().getResourceName(int resid);
In Kotlin Now :
val name = v.context.resources.getResourceEntryName(v.id)
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%.
Kotlin:
val name = v.context.resources.getResourceEntryName(v.id)
If I am right, what you wanted to retrieve is the word "radio1" (from the id itself?) so if that's the case then first you need to get its id.
int intname= buttonname.getId();
then get the result of it
String stringname= getResources().getResourceEntryName(intname);
hoped I helped
You mean you want to take the string text of the id?
Since you have defined it you should know what this is.
If you have a layout and you want to find if a View has a specific id, you can traverse the whole layout and check with getId(), if the id of each View is the id you are looking for..
Hope this helps (if I have understand correct your question.. :) )

How do I get the resource name of an image if I know its resource id? [duplicate]

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 ?
In your Activity, try these:
to get string like radio1:
getResources().getResourceEntryName(int resid);
to get string like com.sample.app:id/radio1:
getResources().getResourceName(int resid);
In Kotlin Now :
val name = v.context.resources.getResourceEntryName(v.id)
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%.
Kotlin:
val name = v.context.resources.getResourceEntryName(v.id)
If I am right, what you wanted to retrieve is the word "radio1" (from the id itself?) so if that's the case then first you need to get its id.
int intname= buttonname.getId();
then get the result of it
String stringname= getResources().getResourceEntryName(intname);
hoped I helped
You mean you want to take the string text of the id?
Since you have defined it you should know what this is.
If you have a layout and you want to find if a View has a specific id, you can traverse the whole layout and check with getId(), if the id of each View is the id you are looking for..
Hope this helps (if I have understand correct your question.. :) )

android email address logcat shows only numbers

in my android app i have into my strings.xml the following value:
<string name="Email">mail#domain.com</string>
in my main.class i have this log.e:
Log.e("-->", ""+R.string.Email);
The output is:
2131099691
Why?
R.string.email is a string resource id. In order to get the value associated with it, you need to call getResources().getString() & pass in the resource's id as an argument:
Log.e("-->", ""+getResources().getString(R.string.Email));
R.string.Email is actually a resource id of an string in xml resource.
That's why it is printing it inside log.
If you want to log actual strung value , you have to use:
context.getString(R.string.Email);
Hope it helps.
Try this:
Log.e("-->", ""+getResources().getString(R.string.Email));

Android get resource type of id

I search for a way to say which resource type is behind a integer ID. For example i have the id of a drawable and another one for a color. I can load the drawable with Resource.getDrawable(id) and the color with Resource.getColor(ID) but how can i know which method is the right one for a given ID. I look for something like Resource.isColor(id)?
Has anybody a good idea. I wrote a image loader which loads images from web, filesystem and drawables but now also from hex color or color ID.
Check out this function from android Resources class. Resources.getResourceTypeName(int resid) (also this )
To find the resource type the safest solution is to use the below code
String viewName = findViewById(resourceId).toString();
String resourcetype = viewName.substring(0,viewName.indexOf("{"));
The resoucetype then contains the full classname for the resource, eg in case of EditText, it shows
android.widget.EditText
Tried some other ideas but noted that getResourceTypeName only returns 'id' as value and getTag() on View does not work in all conditions.

Convert android integer resource ID to hex/name

I used context.getApplicationInfo().icon to get the integer resource id of the icon in my application. (ex. 2130837504) Is there a way to convert that into the name? (ex. R.drawable.icon)
You can find your answer in get a resource (a string) from its unique integer.
Please let me know if you are looking for something else!

Categories

Resources