I am doing a small library to encapsulate adapters functionality and I need get the name of a view programmatically. For example:
<ImageView
android:id="#+id/**ivPerson**"
android:layout_width="80dp"
android:layout_height="80dp"/>
Now, in the code I have the view (ImageView) and I need obtain the name of the view --> ivPerson. I can obtain the id with view.getId() but that's not what I need.
It is possible?
I tried with this:
String name = context.getResources().getResourceEntryName(view.getId());
But not work.
You can use getResourceName(...) instead.
String fullName = getResources().getResourceName(view.getId());
But there is a some problem. This method will give you a full path of view like: "com.google:id/ivPerson"
Anyway, you can use this trick to get only name of a view:
String fullName = getResources().getResourceName(view.getId());
String name = fullName.substring(fullName.lastIndexOf("/") + 1);
Now name will be exactly "ivPerson"
You can do it by setting tag as the id i.e android:tag="ivPerson"
and use String name = view.getTag().toString() to get the name of the tag
For the View v get its name as follows:
String name = (v.getId() == View.NO_ID) ? "" :
v.getResources().getResourceName(v.getId()).split(":id/")[1];
If the view v has no name, then the name is an empty string "".
There is no way to get ivPerson id using getId() or any other method, however you can get it by setting tag value as mentioned in rahul's answer
set tag in xml :
android:tag="ivPerson"
fetch it in your activity
view.getTag().toString();
You may do something like this:
for(Field field : R.id.class.getFields()) {
try {
if(field.getInt(null) == R.id.helloTV) {
Toast.makeText(this, field.getName(), Toast.LENGTH_LONG).show();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
Pros:
No need for manually setting the tag. So you may use the tag for other purposes.
Cons:
Slower due to reflection.
You can use :
String name = ((view != null) && (view.getId() != View.NO_ID)) ? getResources().getResourceEntryName(view.getId()) : "";
Related
I am new to android studio and kotlin. I need to find a way to check if a string contains a char, which is, in this case, "/"
I want to form a piece of code in the following manner:
if (string input contains a character "/") = true {
<code>
}
else{
<code>
}
Please tell me how to do this, and if possible, give me the code I'll need to specify as the condition.
You can use contains, like this:
val a = "hello/"
val b = a.contains("/")
When the string has the character will return true.
I'm trying to remove all text tagged like this (including the tags)
<tag>TEXT</tag>
from a String.
I have tried
.replaceAll("<tag>.+/(tag)*>", "")
or
.replaceAll("<tag>.*(tag)*>", "")
but neither works correctly and I can't replace the tagged text with ""
I don't know exactly what you want, so here are a few options:
String text = "ab<tag>xyz</tag>cd";
// Between
text.replaceAll("<tag>.+?<\/tag>", "<tag></tag>"); // ab<tag></tag>cd
// Everything
text.replaceAll("<tag>.+?<\/tag>", ""); // abcd
// Only tags
text.replaceAll("<\/?tag>", ""); // abxyzcd
EDIT:
The problem was the missing ? after the .+. The question mark only matches the first occurence, so it works when multiple tags are present which was the case.
Change to this ,
String nn1="<tag>TEXT</tag>";
nn1=nn1.replace("<tag>","");
nn1=nn1.replace("</tag>","");
OR
String nn1="<tag>TEXT</tag>";
nn1=nn1.replaceAll("<tag>","");
nn1=nn1.replaceAll("</tag>","");
Output : TEXT
I hope this helps you.
public static void removeTAG()
{
String str = "<tag>Your Long String</tag>";
for(int i=0;i<str.length();i++)
{
str = str.replace("<tag>", "");
str = str.replace("</tag>", "");
}
System.out.println(str);
}
Here what i did and output was as expected
Output Your Long String
You can use the below regular expression.
.replaceAll("<tag>.+?<\/tag>", "<tag></tag>");
This removes all the tags whether it's an HTML or an XML tag.
Here is my code .This doesn't work
button1.getText() + button2.getText() == button3.getText()
Use equals to compare strings,
Try this,
String button1Text = button1.getText().toString();
String button2Text = button2.getText().toString();
String button3Text = button3.getText().toString();
if((button1Text + button2Text).equals(button3Text)){
// strings are equal
} else {
// strings are not equal
}
button[6].getText().toString().equals(button[0].getText().toString().concat(button[3].getText().toString()));
When you work with Strings in Java the operator == checks if the two objects refer to the same instance of an object.
While equals() checks if the two objects are actually equivalent, even if they are not the same instance.
try:
String button1Text = button1.getText().toString();
String button2Text = button2.getText().toString();
String button3Text = button3.getText().toString();
if (button1Text.equals(button3) && button2Text.equals(button3)) {
// do something...
} else {
// do something...
}
Here's my code that's giving me grief.
TextView questionView = (TextView) findViewById(R.id.questionView);
if(questionView.getText().equals(R.string.begginigStatement){
currentQuestionIndex = -2;
Log.d(TAG, "the TextView's text is equal to R.string.beggingStatement);
}
I'm trying to compare a string w/ an int
but I can't figure out the solution other than perhaps hardcoding the string, though I know that's not a proper convention. What's the solution?
R.string.begginigStatement is just an ID of the string as generated in R.class. To retrieve the value call:
getResources().getString(R.string.begginigStatement)
try to use:
context.getResources().getString(R.string.begginigStatement);
and context can be 'getActivity()' if it's in Fragment or just :
getResources().getString(R.string.begginigStatement)
if it has context
You have to compare this string values:
questionView.getText().toString().equal(getResources().getString(R.string.begginigStatement))
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