How to retrieve this programmatically? android:id="#+id/topRight"? - android

How to retrieve the value "topRight" on this function on the layout? I need to get this value in order for me to know the name of the container so that i can set the the arrangement of my child. I tried to use getId() but it returns an integer of course not the value "topRight". Please help guys.
android:id="#+id/topRight
Thanks.

When you define an ID in your layout, it simply modifies the auto-created R file with a new field. The name you specified will be the name of the variable (R.id.topRight).
I guess you might be able to access the name of the variable via some reflection magic, but I feel like you're going about this in a wrong way.
IDs are used to reference the view from the layout and create instances during runtime.
If you want to have some string stored in the layout elements which you can use to determine what view you'd like to use, I would probably go with the "Tag" property rather than ID.
Hope this helps.

Look at this, I have a textView with editTextUserName as an id so I get its value like the one I posted. Try it, I hope it works for you.
EditText editTextUserName = (EditText) findViewById(R.id.editTextUserName);
<EditText
android:id="#+id/editTextUserName"
android:hint="User Name"
android:layout_width="match_parent"
android:layout_height="wrap_content">

Related

Getting a name id from the value of a string in android

I have a string I'm using in a text view for example
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvText"
android:text="#string/yes"/>
I want to be able to get the key "yes" from this Textview , not the value of this string but just the key that leads to it , is there some way I can use the value of the string to get it ?
there is no option for getting string resource id (you are naming it "key") after setting with android:text. TextView is resolving it in its runtime, fetching String and "forgets" way how it was set up. same case when setting e.g. image for ImageView (android:src="#drawable/drawable_name") or some dimensions for all Views (e.g. android:padding="#dimen/some_dimension")
if you really need this reference in code then it would be better to NOT set this text via XML, instead store this reference under some final int variable in code and use it for setting text for TextView programmatically, further use same variable in another place you need

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.. :) )

Loading data to TextView in Android

I have a XML tag like "Yes,No,Dontknow " and I am parsing the XML file and getting the data. Now I need to display each option in separate TextView, i.e: 'yes' should be displayed in one TextView, 'No' should be displayed in another TextView, and 'Dontknow' should be displayed in another TextView, but how can I do this, can anyone give me some idea (I am new to Android).
Use setText() method of TextView to load text into it.
You can use string tokenizer:
StringTokenizer tokens = new StringTokenizer(theString, ",");
while( tokens.hasMoreTokens() ){
String token = tokens.nextToken();
// here you must have the reference to the text view...
textView.setText(token);
}
If you are creating the text views programmatically, then you must create or reference those text views inside the loop. Other wise, if the text views are static, you better put each token inside an array or something (words[0] will be Yes, word[1] will be No, etc) and then you set those strings manually.
You can just declare 3 separate TextView in you Activity layout file. Using attribute android:text you can assign the text for the TextView.
Example:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
/>
parse xml file store that in a string.take an array like String[] array = parsedstring.split(","); then take 3 text views ,put array[0],array[1],array[2] on to textview
If you want to split comma-separated strings, take a look at using java.util.StringTokenizer. You can tell it to use , as the token separator.

How to retrieve EditText's xml id

I have a form with many EditTexts, and when I press a certain button, I need to retrieve all these controls and put them into a HashMap so the key is the name (key1 int the following code)
<EditText android:id="#+id/key1"
style="#style/keys" />
and the value, whatever text the user enters.
My question is, how can I retrieve the name of the EditText for the Hashmap's keys ? getId() returns a number.
Thanks
Android generates a handle for that View in R.java whenever you build your project. For instance, once you build you can access your EditText by calling R.id.key1. You don't have to store the ids anywhere because you can access the id directly at any time in your code. With this id you can call findViewById() as dave.c mentions to get whatever view you need from your XML.
I finally solved it using android:tag and getTag()
Actually, getId() is the integer value of the component IDs, it's the same as described in the R generated class.
You need the actual name as you wrote? If you need just a reference the int value is enough.

Categories

Resources