Let's assume I have this in layout of res in my app
<TextView android:id="#+id/titleText" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/app_name"
android:textColor="#ffffffb0" android:padding="5px" />
In my activity, I get the TextView using this command
TextView tv = (TextView)findViewById(R.id.titleText);
But I am looking for another method like this
TextView tv = (TextView)findViewByString("R.id."+"titleText");
Because I need to enumerate those ids. Can any of you give a hint or clue how I can go about it?
You can use something like this:
Resources res = getResources();
int id = res.getIdentifier("titleText", "id", getContext().getPackageName());
And then use the id.
In Kotlin you can use this line to get the ID.
val id: Int = resources.getIdentifier("titleText", "id", packageName)
The returned value can be use as parameter of the function findViewById<TextView>(id).
NOTE: If is called outside of an activity use the context.
val id: Int = context.resources.getIdentifier("titleText", "id", context.packageName)
Related
I have a string that contains an id for a button:
val stringContainingId = "R.id.testBtn"
And I testBtn is set as id in activity_main:
<Button
android:id="#+id/testBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
So, I want to set the text of this button in my MainActivity. stringContainingId is a string, so I have to convert to id with type Int:
val id: Int = applicationContext.resources.getIdentifier("R.id.testBtn", "id", applicationContext.packageName)
Then changing the text of testBtn:
val testBtn = findViewById<Button>(id)
testBtn.text = "Other text"
But at runtime app crashes, and shows this error:
testBtn must not be null
But if changed to findViewById<Button>(R.id.testBtn), eveything goes smoothly.
What's wrong with variable 'id' that is getting the id from string?
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
Use just “testBtn”, not “R.id.testBtn” in getIdentifier().
I have a user interface with multiple buttons. They have the IDs "button1", "button2", ...
I want to set an OnClickListener for all of them in a for loop. I dont want to type a line like button1.setOnClickListener for every button.
I have found one solution that works in java here: Android: Using findViewById() with a string / in a loop
And I tried to adapt it in Kotlin.
var buttons = ArrayList<Button>()
for (i in 1..7) {
var idString = "Button%i"
var buttonID = getResources().getIdentifier(idString, "id", packageName)
buttons.add( findViewWithTag(buttonID))
buttons[i].setOnClickListener(buttonclicked)
}
This throws an "Unresolved Reference" error. How can I access all buttons without typing a line for each of them?
Thanks in advance to all of you.
You call findViewWithTag() instead of findViewById() in your code.
Also you are not doing string interpolation correctly by var idString = "Button%i".
Change to this:
val buttons = ArrayList<Button>()
for (i in 1..7) {
val idString = "Button$i"
val buttonID = resources.getIdentifier(idString, "id", packageName)
buttons.add(findViewById(buttonID))
buttons[i].setOnClickListener(buttonclicked)
}
I'm trying to identify a button using getResources() and getIdentifier(), then set the text for it. The below code shows how I thought it should work (simply setting the ID and setting the text for the object).
while (c < 65) {
val resID = getResources().getIdentifier("S1", "id", getPackageName())
resID.text = ""
}
Instead I get an error, with ".text" showing red. How should I go about setting this up to run how I expected.
resID is the integer id of a Button "named" S1.
Use it to find the Button:
val resID = resources.getIdentifier("S1", "id", getPackageName())
val button = findViewById<Button>(resID)
button.text = ""
based on Docs getIdentifier returns Int , so you need findViewById and use the getIdentifier result in order to use the object
Kotlin Extensions do not work if the referenced int is not coming from R.id.
Button is a view, then you can access to it by using
findViewById<Button>(buttonId)
If you want to retrieve id from resources, just getResources().getIdentifier("S1", "id", getPackageName())
In conclusion, you see how this is going to be:
val buttonId = getResources().getIdentifier("S1", "id", getPackageName())
val button = findViewById<Button>(buttonId)
button.setText("")
I want to make a function which debug when image's name equal "aaa". So I think that I should get image's name.
for example, there is a ImageView.
ImageView img1 = (ImageView) findViewById(R.drawable.button);
public void passMSg(){
if(name == "button"){ // "name" means image's name. = button
Toast. ~
}
I find other questions. But I can't understand and I think that is not answer which I want. Who can help me?
if your image is a file like 'aaa.jpg/png..'
try to use the APIs of String such as subString().indexOf()
and your code may be like this:
int index = name.indexOf(".");
String name = name.subString(name,0,index);
If you assigned a tag for your ImageView then you can get it like this
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:tag="aaa" />
ImageView v = (ImageView)findViewbyId(R.id.img);
String backgroundImageName = String.valueOf(v.getTag());
I've set up an onTouch class to determine when one of my 40 buttons is pressed.
The problem I am facing is determining which button was pressed.
If I use:
int ID = iv.getId();
When I click on button "widgetA1"
I receive the following ID:
2131099684
I would like it to return the string ID "widgetA1"
from:game.xml
<ImageView android:layout_margin="1dip" android:id="#+id/widgetA1" android:src="#drawable/image" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
from:game.java
public boolean onTouch(View v, MotionEvent event) {
ImageView iv = (ImageView)v;
int ID = iv.getId();
String strID = new Integer(ID).toString();
Log.d(TAG,strID);
//.... etc
}
+-+-+-+-+-+-
I other wise works fine, it knows what button you are pressing. I am quite new to this Android JAVA. Let me know if you guys can help me.
Edit - TL;DR:
View v; // handle to your view
String idString = v.getResources().getResourceEntryName(v.getId()); // widgetA1
Original:
I know it's been a while since you posted, but I was dealing with a similar problem and I think I found a solution by looking at the Android source code for the View class.
I noticed that when you print a View (implicitly calling toString()), the data printed includes the ID String used in layout files (the one you want) instead of the integer returned by getId(). So I looked at the source code for View's toString() to see how Android was getting that info, and it's actually not too complicated. Try this:
View v; // handle to your view
// -- get your View --
int id = v.getId(); // get integer id of view
String idString = "no id";
if(id != View.NO_ID) { // make sure id is valid
Resources res = v.getResources(); // get resources
if(res != null)
idString = res.getResourceEntryName(id); // get id string entry
}
// do whatever you want with the string. it will
// still be "no id" if something went wrong
Log.d("ID", idString);
In the source code, Android also uses getResourcePackageName(id) and getResourceTypeName(id) to build the full string:
String idString = res.getResourcePackageName(id) + ":" + res.getResourceTypeName(id)
+ "/" + res.getResourceEntryName(id);
This results in something to the effect of android:id/widgetA1.
Hope that helps!
You cannot get that widgetA1 string...You will always get an integer. But that integer is unique to that control.
so you can do this to check, which button is pressed
int ID = iv.getId();
if(ID == R.id.widgetA1) // your R file will have all your id's in the literal form.
{
}
Xml
android:tag="buttonA"
Src
Button.getTag().toString();
Of course you can get widgetA1, simply do:
ImageView iv = (ImageView)v;
int id = iv.getId();
String idStr = getResources().getResourceName(id);
It should give you your.package.name:id/widgetA1, then do what you want with that string.
We will get the Integer value corresponds to the views id. For finding on which button you are clicking, better to use tags. you can set tag in the layout xml file and can compare which item you have clicked using.
<Button
android:id="#+id/btnTest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button"
android:tag="btntestTag"/>
This is sample usage.
i think that you can use this
try it
ImageView imgView = new ImageView(this);
String imgName = "img"//in your case widgetA1
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
For the implementation you could use the hint attribute to store the button name, which is accessible. For example in the button code (.xml file) use the following line:
android:hint="buttonName"
Then in the onClicked function (.java file), write the following to get the hint:
String hint = ((Button)v).getHint().toString(); //v is the view passed onClick