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.
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").
Cyril Mottier has a great post on customizing the send/done/return key on the Android soft keyboard. When trying out the code, I (and several others in the comments) noticed that setting the imeActionId with a new ID in XML (e.g. #+id/...) returns a 0 to the OnEditorActionListener, when the key is hit by the user, not the unique ID. However, if you set an ID in ids.xml and set imeActionId to that (e.g. w/ #id/...) it causes a layout inflation error.
The only way I could successfully get the imeActionId to be set to a unique ID was to set it programmatically in Java. So what is the correct usage of the XML attribute imeActionId?
Here is a Gist with all of my code: https://gist.github.com/gsysko/d46adbe27d409bde0299
Thanks for considering this question.
The reason is that imeActionId is a slight misnomer in this case. The Javadoc for imeActionId says:
Supply a value for EditorInfo.actionId used when an input method is connected to the text view.
It is looking for you to assign a value. A resource ID is for identifying resources in your app and does not have a guaranteed value. In some cases you can make comparisons based on resource ID's, such as View.getId(), but it is not good to mix resource ID's in with constant values that EditorInfo uses. Android may try to prevent you from doing this when it parses in your XML files by throwing exceptions like you saw, but there's not many checks it can do at runtime if you set it programmatically.
Instead, you can define an integer value in your resources like so:
<!--res/values/integers.xml-->
<resources>
<item type="integer" name="customImeActionId" format="integer">100</item>
</resources>
and use it like
android:imeActionId="#integer/customImeActionId"
In your code you can then retrieve it
int imeActionId = getResources().getInteger(R.integer.customImeActionId);
edit: OK, this has piqued my interest so looking further in the Android source code, TextView parses the attribute like:
mEditor.mInputContentType.imeActionId = a.getInt(attr, mEditor.mInputContentType.imeActionId);
It will use mEditor.mInputContentType.imeActionId as the default value -- which is 0 in this case -- if it can't find the int value of attr, which explains why it returns 0 if you use a newly created ID. I haven't found the cause of the inflation error.
I want to get a string resource name or ID by passing its value.
Example :
<string name="stringName">stringValue</string>
I want to pass the stringValue and get the stringName or ID (id value)
Based on ID i will do some calculation to get another ID of another String resource
I don't know if that's possible and I think the IDs can change without warning if the R class gets newly generated. You could of course try some reflection magic on this class, but I would recommend against it.
I also have this problem, and can think of 2 possible workarounds:
load all the strings and their names into a table, and look in the
table.
Or cycle through my complete list of names, getting the string resource
for each one, and comparing it to my known string resource.
I am implementing the 2nd one, as my list of string resources is not very big, and I don't have to do this operation very often. Once the name is known, it's possible to get the Resource Id via:
//Get resource id from name
var resourceId = (int) typeof (MyApp_droid.Resource.String).GetField(MyStringName).GetValue(null);
(code is C# because I'm working in Xamarin).
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.