I have looked everywhere but cannot find an example of programmatically setting a background resource from a string value?
As an example:
Drawable a = getResources().getDrawable( R.drawable.a );
Drawable b = getResources().getDrawable( R.drawable.b );
Drawable c = getResources().getDrawable( R.drawable.c );
abc.setBackgroundResource("b");
Is this possible or would I have to do it as a big switch statement?
you have getResources().getIdentifier for this purpose. It returns the id of the resources from its name.
E.g.:
int resId = getResources().getIdentifier("b", "drawable", getPackageName());
Here you can find the documentation.
Related
I'm testing my android app with espresso and want to test the ZoomControls like this:
onView(withId(R.id.zoomIn)).perform(click());
But I don't get the zoomIn/zoomOut id, from the Zoom_Controls. In the Layout Component Tree, the zoomIn and zoomOut are visible.
Can someone help me?
Thanks in advance.
Resources res = Resources.getSystem();
int id = res.getIdentifier("btn_zoom_down", "drawable", "android");
Didn't work.
Got it working with:
Resources res = Resources.getSystem();
int id = res.getIdentifier("#id/zoomOut", "drawable", "android");
onView(withId(id)).perform(click());
ImageView test = (ImageView) findViewById(R.id.A1);
gets me an ImageView
instead of A1 I would like to use a string variable to fetch a series of IDs
I have tried
Method Pmethod = Class.getDeclaredMethod("(ImageView) findViewById(Id.R." + dest.toString());
as suggusted in this question : how to call a java method using a variable name?
However I get the error
non-static method getDeclaredMethod cannot be referenced from a static context
Any way to fix this?
My latest attempt :
String ps = "P"+ dest.toString();
String ss = "S"+ dest.toString();
int piecelayertier = getResources().getIdentifier(ps,"id","com.donthaveawebsite.mhy.ocfix");
int selectorlayertier = getResources().getIdentifier(ss,"id","com.donthaveawebsite.mhy.ocfix");
ImageView test =(ImageView)findViewById(piecelayertier);
spot.TieAppearance((ImageView)findViewById(piecelayertier));
spot.TieSelector((ImageView)findViewById(selectorlayertier));
I think this is not a correct way to get id.
You can try following to get id using a string variable :
int id = getResources().getIdentifier("<String id>","id","<package name>");
ImageView test =(ImageView)findViewById(id);
You might also need to use a this reference like so :
int id = this.getResources().getIdentifier("<String id>","id", getPackageName());
I'm trying to put inline images in a TextView. Here goes my code:
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = ctx.getResources().getDrawable(Integer.parseInt(source));
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
CharSequence votesString="124 votes";
votesString=Html.fromHtml(votesString
+"(<img src='" + R.drawable.icon_upvotes + "'/>)", imageGetter, null);
labelVotes.setText(votesString);
It works (see the image below) but I would need the image to be vertically centered. How can I do that?
Thanks
Instead of use HTML, you can use
labelVotes.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.icon_upvotes);
and playing a little bit with padding, using setCompoundDrawablePadding
Why you don't use android:drawableRight ?
I have an imageView that i want to display a little icon of the country that you are currently in. I can get the country code, but problem is i can't dynamically change the imageView resource. My image files are all lowercase
String lowerCountryCode = countryCode.toLowerCase();
String resource = "R.drawable." + lowerCountryCode;
img.setImageResource(resource);
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
setImageResource(id);
abve is my code.
Now, of course this will not work because set Image Resource wants an int, so how can i do this? Thanks in advance
I mean you know the name of files(Drawable) which you wanna use for one and other country.
ImageView imgView=(ImageView)findViewById(R.id.imageView1);
int id = getResources().getIdentifier(lowerCountryCode.toLowerCase(), "drawable", getPackageName());
imgView.setImageDrawable(res.getDrawable(id));
The following code should work well (assuming that you really have a file with name lowerCountryCode in your res/drawable/ folder)
String lowerCountryCode = countryCode.toLowerCase();
int id = getResources().getIdentifier(lowerCountryCode, "drawable", getPackageName());
ivCard[0].setImageResource(id);
i use that code....
ImageView[] ivCard = new ImageView[1];
#override
protected void onCreate(Bundle savedInstanceState)
ivCard[0]=(ImageView)findViewById(R.id.imageView1);
Scaling with ScaleDrawable is not working for me.
The drawable is remained in the same size.
LayerDrawable layerDrawable = new LayerDrawable(layers);
Drawable d = layerDrawable.getCurrent();
ScaleDrawable sd = new ScaleDrawable(d, 0, 0.01f, 0.01f);
return sd.getDrawable();
What i need to do to fix it?
Thanks.
You need to set the level:
LayerDrawable layerDrawable = new LayerDrawable(layers);
Drawable d = layerDrawable.getCurrent();
ScaleDrawable sd = new ScaleDrawable(d, 0, 0.01f, 0.01f);
sd.setLevel(8000);
return sd;
The level ranges from 0 to 10000: at 10000 it is full size, at 0 it does not appear at all.
If you check the reference for ScaleDrawable, you will see that the getDrawable method returns the base drawable. That is, it returns d in your case. You should just return sd as it is already a Drawable.