This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can i get the resource id of an image if I know its name?
hi i want to get the id of an image,i could do this,
int id = R.layout.imagename
then i could do this
imageView.setImageResource(id)
but my question is how to get the id if i know the imagename changes at run time, so on this case how can i make R.layout."imagename" ?
I just copied Francesco's answer, I hope you will up vote his answer
String mDrawableName = "myappicon";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
Related
This question already has answers here:
Android getResources().getDrawable() deprecated API 22
(16 answers)
Closed 18 days ago.
I want to get the drawable id which is shown in the screen.
I used
if(getResources().getDrwable(binding.image1)==R.drawable.image){ //my stuff}
But this is depreciated.
You can use alternative of getResources().getDrawable(int id) as mentioned below
ContextCompat.getDrawable(Context context,int drawableId)
This question already has answers here:
I can only get part of a url (link) and put into string in android?
(2 answers)
Closed 6 years ago.
Ok I wasn't really sure how to word this question, but basically what I want to do is, I got a url from a RSS feed in android, and I need to put part of that url into a string, the url will look something like this: http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821and I only want the part after id=, ONLY the number, is that possible? Please help me, Thanks
You can achieve this by using two ways:
// One Way
String url = "http://www.prsn.uprm.edu/Spanish/Informe_Sismo/myinfoGeneral.php?id=20161206012821";
String substr = "=";
String after = url.substring(url.indexOf(substr) + substr.length());
// Second Way
String[] parts = url.split(substr);
String afterTwo = parts[1];
This question already has answers here:
How to remove the " .0" in a whole number when using double in java? [duplicate]
(3 answers)
Closed 4 years ago.
So, I have two numbers as strings - for example, 123.00 and 123.50. How do I remove the decimal point and all that follows. I had a method that worked for the 123.00, but would not work correctly on the 123.50.
Here is what I have so far:
String balance = getString("balance");
BigDecimal number = new BigDecimal(balance);
String formattedBalance = number.stripTrailingZeros().toPlainString();
int roundedBalance = Integer.parseInt(formattedBalance);
int roundedBalance = Integer.parseInt(getString("balance").split(".")[0]);
Do you want to remove the following points or numbers?
If only the points why not:
balance = balance.replace(".","");
This question already has answers here:
How to get a resource id with a known resource name?
(10 answers)
Closed 8 years ago.
Lets say i have a few views and I want to get the id name of their IDs
view.getId()
returns an int value but instead i want to get the name of the id
android:id="#+id/valueFrance"
So is there any method on java that can return the name/value of the id ( "valueFrance" ) ?
Thank you very much.
try this
getResources().getResourceEntryName(int resid);
This question already has answers here:
How to get the image from drawable folder in android?
(4 answers)
Closed 9 years ago.
Is there a way to generate R.drawable.... name on the fly? I want to set a background image with a dynamically generated name ( which is already usable as R.drawable.example_graphic)
So i want to find a way to assign a String to $ABC -> btn.setBackgroundImage(R.drawable.$ABC);
I don't want to create a new drawable, i want to use the existing one.
Yes. Drawables are constants at compile time: you know exactly what exists. So, in your Application object, create a map:
public static final Map<String, Integer> NAMED_RESOURCES;
static {
Map<String, Integer> m = new HashMap<String, Integer>();
m.put(KEY1, R.drawable.ABC);
m.put(KEY2, R.drawable.DEF);
// ...
NAMED_RESOURCES = Collections.unmodifiableMap(m);
}
now you can:
btn.setBackgroundImage(Application.NAMED_RESOURCES.get($ABC));
i have found the solution, as #CommonsWare has shared.
getContext().getResources().getIdentifier("flags_"+country, "drawable","mypackage")
However, it may require cacheing as it uses reflection.
So, creating a static HashMap to keep the ResourceName and it's result from getIdentifier function (integer) seems to be a good idea; further usages for the same ResourceName will just get the value from HashMap instead of using the reflection again.