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.
Related
<WebView android:id="#+id/googleWebview"/>
Here I can set an ID as the string "googleWebview" but if I try to use setId() programatically it expects an integer. Why is this?
In Android all view IDs are integers - #+id/googleWebView is just a label for an integer ID.
In this case the #id/ indicates that it's handling an ID reference, the + means that this is a new ID that should be generated. Under the hood, Android stores these generated IDs in the R file, and you can access reference them programmatically as R.id.{label}.
When the app is build "#+id/googleWebview" is actually turned into an int. It will go to the R class. To access any ids you defined before in your xml files you can simply write R.id.idName in your code so in your case:
view.setId(R.id.googleWebview);
for example
I want to use the string resources to save some key values. So, I have something like this:
<string name="key_name">key_name</string>
The resource name and the resource value are the same. There is no need for me to make a difference between them, as it is just to cleanly store one value.
To reduce redundant information,
is there a way to just tell android-studio that the name equals the value? Something like this?
<string name="key_name"/>
I think you can't do it. It looks like you create a variable, you must name it and when you need you can set its value.
You can't create a variable with same values with name at the same time.
I think you have the wrong resource type. A string resource is used for mapping a name to a text. From what you're describing here you only need the name (and then can extrapolate the text from it).
Using strings with matching name and value shouldn't be necessary:
If you're going to be referencing them from Java or Kotlin code, you still need to know the name (i.e. R.string.key_name), so you might as well just use the string value there.
If it's for using inside XML code (where you want to pass the value of a string, e.g. text="#string/key_name") most of the time you can just use the raw string there (i.e. text="key_name").
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">
What is the co-relation between View's getId() method and the id defined in the XML file? Is there any? My IDs look like this: "field1", "field2"... This digits at the end are very important for my application and I need to retrieve them. Is there any way to do it?
To get resource id name:
res.getResourceEntryName(view.getId())
You will get "field1"
What is the co-relation between View's getId() method and the id defined in the XML file?
getId() returns the android:id value, or the value you set via setId().
My IDs look like this: "field1", "field2"
In your XML, they may look like #+id/field1 and #+id/field2. Those are allocating ID resources, which are turned into numbers at compile time. These are the numbers you refer to in Java code as R.id.field1 and R.id.field2.
This digits at the end are very important for my application and I need to retrieve them. Is there any way to do it?
Have a big switch statement comparing the ID to R.id.field1, etc. Or, set up a HashMap to map between the getId() values and your "very important" numbers. Or, find some other solution that does not involve magic names on your ID values.
What is the co-relation between View's getId() method and the id
defined in the XML file? Is there any?
From documentation:
Returns this view's identifier.
Related XML Attributes
android:id
Returns
a positive integer used to identify the view or NO_ID if the view has no ID
It returns a positive number which android studio gives to every component of android app for backup use, In case the developer does not provide the ID of that component, android studio uses that number as a unique ID for that component.
App version 2.3.3
Here is what i am looking for...
I have few strings in "values/strings.xml". I wish to retrieve the values depending on the string's "name" attribute and use it my application. The input to which string in the xml should be used comes dynamically. Here is an example...
My input from other file :: "A" => This value changes. It can be any value in A,B,C,D.
I have different strings in strings.xml, like...
<string name="A">AforApple</string>
<string name="B">BforBall</string>
<string name="C">CforCat</string>
<string name="D">DforDog</string>
Now, how do i programmatically get the value(AforApple) of the String with name="A".
String str = context.getResources().getString(R.string.A)
or u can use
textBox.setText(R.string.A);
do not forget to import the package com.yourpackackage.R.
You need to use getResources() method:
String a = getResources().getString(R.string.A);
Just for the record, you can also dynamically generate it using reflection.
int stringRes=R.string.class.getField("Here you put the dynamically generated input, such as A").getInt(null);
textView.setText(stringRes);
This will return the resource int value from string XML based on the input, as long as the input value "A" matches string name in the XML, this will retrieve them dynamically.
To access a String resource/value that you’ve defined in an XML file from your Android/Java code, use the Android getString method, like this:
String A = getString(R.string.a_string);
If your code gets a string like "A" and you are trying to dynamically find the string in your resources that matches that name, I don't think you can do that.
Instead of using the strings.xml, you might want to use arrays.xml and build a HashMap from that before you need to access those strings
Try this:
String word = getResources().getString(R.string.A);
Check out the link here.
You can use this code:
getText(R.string.A);
Basically, you need to pass the resource id as a parameter to the getText() method.