Basically, I'm trying to add an SVG drawable to an ImageView, but I keep getting strange type errors I can't resolve.
// String var "icon" holds the name of the drawable resource
int drawableId = context.getResources()
.getIdentifier(icon, "drawable", MainActivity.PACKAGE_NAME);
// drawableId shows the error:
// 'getDrawable(android.content.Context, int)' in 'android.support.v4.content.ContextCompat' cannot be applied to '(int)'
// I tried using Context instead of ContextCompat, but I got an error about needing API 21
Drawable channelIcon = ContextCompat.getDrawable(drawableId);
// drawableId here shows no error
imageView.setImageResource(drawableId);
Any idea what I'm doing wrong? Basically this is a workaround for needing to use a variable for R.drawable.something
Thanks.
I'm trying to add an SVG drawable to an ImageView
ImageView does not support SVG.
Any idea what I'm doing wrong?
Beyond the aforementioned issue, replace:
Drawable channelIcon = ContextCompat.getDrawable(drawableId);
with:
Drawable channelIcon = ContextCompat.getDrawable(context, drawableId);
Related
I'm working on an android app using java in AndroidStudio. I have an ImageView pic and I want to set its image as a .png that I have saved in my /drawable folder. I may try
pic.setImageDrawable(R.drawable.image_name);
However, setImageDrawable() requires a Drawable, while R.drawable.image_name returns an Integer (the ID of the image).
How may I resolve this?
You can use setImageResource() for this:
pic.setImageResource(R.drawable.image);
More about it here
Use this for java
ContextCompat.getDrawable(getActivity(), R.drawable.drawable_resource_name);
and then set this drawable to your image view
I have stored image icons in res/mipmap folder of my android project. Each of them have their own image id. I want to check if I have image there by image id. I want to do something like,
String input_id = "blue_image";
ImageView image;
if (image.getImageByID(input_id)){ ## if image with blue_image exists
image.setImageResource(...);
}
If I know that blue_image exists then I usually get it like
image.setImageResource(R.mipmap.blue_image);
There seems to be 2 problems in this question. One is about checking image exists by ID and second one is to find something similar to getattr() python function in java.
If you have any other solutions to similar case, those are also welcome.
Try this in your Activity class:
int imageId = getResources().getIdentifier(inputId, "drawable", getPackageName());
if (imageId > 0) {
image.setImageResource(imageId);
}
The point is that getIdentifier() returns 0 if no resouce was found.
Use:
int imageId = getResources().getIdentifier(inputId, "mipmap", getPackageName());
With "mimap", not "drawable" as the other answer suggests
Sorry for the title, had troubles describing what I'm trying to do. I've always had this question for Java as a whole, but right now, its specific for Android. Let's say that I am parsing an API and get this special String, let's say "clear". Inside my drawables, I have a clear.jpg that I would like to set as an ImageView programically . That's not the hard part. What I was wondering is if there was a quick way where I Could call on the .jpg with just the variable name? I know I can easily make different if statements, such as:
if(string == "clear")
{
//setImageView to drawable/clear
}
but is there anyway I can do something such as
//setImageView to drawable/string
where string would be clear? I know obviously it would look for a string inside drawable, but is there anyway I could do what I'm describing? Just a general question I had; it was something I always wondered about.
Let me know! Thanks!
private void showImage() {
String uri = "drawable/icon";
// int imageResource = R.drawable.icon;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(imageResource);
imageView.setImageDrawable(image);
}
Else I would recommend you to work with R.* references like this:
int imageResource = R.drawable.icon;
Drawable image = getResources().getDrawable(imageResource);
Reference -> Android - Open resource from #drawable String
With what i know, the quickest way to call up that picture is putting the
picture's name in the xml file; For instance if you want to change the background
of an imageview, you can just put in your xml file this code below
android:background="#drawable/clear".
Note the image must have been in the drawable folder. Thanks, i hope this helps.
It looks like Java 8 provides limited support for local variable annotations, but its reflection API can't give you access within method bodies. So you'd need to parse the class file itself. And for that, a library like ASM would do the trick.
if(string.equals("clear")){
int resID = getResources().getIdentifier("clear", "drawable", getPackageName());
imageView.setBackgroundResource(resID);
}
Same as other ones at the point which is using getIdentifier method.
imageView.setImageView(getResources().getIdentifier(string, "drawable",
getPackageName()));
However, by doing this, you can process without if-condition block and just specify a Drawable of the value from string directly (I think it is your intention of this question).
I'm working on an app that allows changing theme. To achieve that, I need to change background in java.
I created imageView and tried to set background as imageView.
I was using this code:
ImageView imgViewBackground =(ImageView) findViewById(R.id.imageViewBackground);
int ID = getResources().getIdentifier("imagebackground", "drawable", getPackageName());
imgViewBackground.setImageResource(ID);
but the app crashes after 20-30 seconds of usage.
I also tried this, but the app crashes on startup:
RelativeLayout layout =(RelativeLayout)findViewById(R.id.imageViewBackground);
layout.setBackgroundResource(R.drawable.imagebackground);
Is there an effective way to change background directly, and not through imageView in java?
Why not simply do this:
ImageView imgViewBackground =(ImageView) findViewById(R.id.imageViewBackground);
imgViewBackground.setImageResource(R.drawable.imagebackground);
P.S: An image by the name imagebackground must be present in the drawable folder.
I'm using this code to change the wallpaper of the android home
WallpaperManager wm = WallpaperManager.getInstance(this);
wm.setBitmap(myBitmap);
I would like to set the background to a drawable. Is this possible?
You'll first have to convert the Drawable to a Bitmap. How to do this I found here. You'll need to use the BitmapFactory class, specifically the decodeResource() method.
Simply pass in the Resources and the Resource ID as parameters, like so:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.my_drawable);
wm.setBitmap(bmp);