Android and getting a view with id cast as a string
int resID = getResources().getIdentifier("button_%i",
"id", getPackageName());
View addButton = findViewById(resID);
where %i
is replaced by some valid index.
The getResources()
method belongs to the Context
class, so you can use that directly from an Activity
. If you are not inside an activity, then use a context to access: (myCtxt.getResources()
).
Easier way to get view's Id (string) by its Id (int)
The approach is misguided to begin with. If you want to associate a piece of arbitrary data (e. g. a string) with a view, that's what tag
is for. The ID is numeric and it better stay that way. A word of caution though, tags are not unique in Android, watch for accidental tag collisions within the same view tree.
EDIT much later: the OP's issue was a case of an XY problem. That said, the question title alone is a legitimate question in its own right.
Android: Using findViewById() with a string / in a loop
You should use getIdentifier()
for(int i=0; i<some_value; i++) {
for(int j=0; j<some_other_value; j++) {
String buttonID = "btn" + i + "-" + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = ((Button) findViewById(resID));
buttons[i][j].setOnClickListener(this);
}
}
Converting string containing id to an id type Int
In the Android Java framework, android.R.id.testBtn is an identifier of a Button . You can find it in many layouts from the framework (select_dialog_item, select_dialog_singlechoice, simple_dropdown_item_1line, etc.). In Android framework xml, it is represented by @+id/testBtn
Mate, you get id = 0 cause by R.id.testBtn
not a String. It actually an int
variable. If you want to get identifier, you just need add testBtn
to find it.
val id: Int = applicationContext.resources.getIdentifier(testBtn, "id", applicationContext.packageName)
Read more in document official android and I believe you know more than.
e.g: https://www.codota.com/code/java/methods/android.content.res.Resources/getIdentifier
What is "android.R.id.text1"?
https://developer.android.com/reference/kotlin/android/R.id
FindViewById where ID is dynamic string
You can get an identifier from a string by using:
notes = (TextView)findViewById(getResources().getIdentifier(VIEW_NAME, "id", getPackageName()));
Where VIEW_NAME
is whatever identifier string you're generating. After that, you can set the text of it like you currently are. This also works if you need to get strings and drawables as well, just change id
to the appropriate type.
android studio how to convert string with id to int?
What you are asking is completely wrong.
"R.id.id"
is a String
that can't be converted in a int.
It will always throw a NumberFormatException
.
Check the javadoc:
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.
If the resource used in your strings.xml
with the same id
is a integer, you have to use somenthing different:
<resources>
<string name="mystring">12</string>
</resources>
Use this code to retrieve a string:
String string = getString(R.string.mystring);
try{
int number = Integer.parseInt(string);
} catch (NumberFormatException nfe){
//.....
}
Accessing Android drawable resource with a string
Create an Array of integers to hold the references to your images, e.g.
int[] images = new int[5];
images[0] = R.drawable.image001;
images[1] = R.drawable.image002;
images[2] = R.drawable.image003;
images[3] = R.drawable.image004;
images[4] = R.drawable.image005;
And then increment your counter on each click, and set the image resource using a value from your array:
imgButton.setImageResource(images[counter]);
Easy when you know how... ;)
Android one line getText() declarations. What is the difference?
First of all findViewById(R.id.signup_email)
returns the View
object, so when you write this statement findViewById(R.id.signup_email).getText()
here getText()
applies on view
object (apparently View
class does not contain this method).
But when you separate in two lines, here textView = findViewById(R.id.signup_email);
the View
object will be type cast to TextView
or EditText
(which you defined) object. so from here you will get this method.
If you want to keep in single line you can use
string = ((TextView) findViewById(R.id.signup_email)).getText().toString();
Related Topics
How to Get Ruby Generated Hmac for Sha256 That Is Url Safe to Match Java
Aes/Cbc Encrypt in Java, Decrypt in Ruby
Java Client and a C++ Server Send and Receive via Tcp Socket
Java to Ruby Aes/Ecb/Pkcs5Padding Encryption
How to Get Java Output with Ruby
Why Ruby Modulo Is Different from Java/Other Lang
Jruby: Watir Is Hanging When Launching Browser
How Run Cookbook with Dependencies in Chef
How to Get from Jruby a Correctly Typed Ruby Implementation of a Java Interface
Simple Sso - Using Custom Authentication - Cas or Some Oauth or Openid Server
Save JSON Outputted from a Url to a File
Fixing 'Path' in Environment Variables in Windows 7 for Multiple Applications
Hbase Client Connectionloss for /Hbase Error
Advanced Java-Like Enums in Ruby
Is There an Advantage to Running Jruby If You Don't Know Any Java
Integration Testing Frameworks for Testing a Distributed System