Drawable drawable = res.getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
This work but i would all dinamically.
Drawable drawable = res.getDrawable(R.drawable.path);
button.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
where path is a string that contains a name of image..
thanks to all!
If I got you right, you are trying to get drawable by the image name. right?
If so,
int imageResource = res.getIdentifier(path, "drawable", getContext().getPackageName());
Drawable drawable = res.getDrawable(imageResource);
then assign the button drawable...
Related
How to get ImageView src programmatically and set it in another ImageView
Drawable drawable = imageViewA....?
imageViewB.setImageDrawable(drawable);
You can do something like this:
Drawable drawable = imageViewA.getDrawable();
if(drawable != null){
imageViewB.setImageDrawable(drawable);
}
You can use setImageResource(int)
imageView.setImageResource(R.drawable.bg_image);
Instead of get drawable you do...
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
and set to new image
newImageView.setImageBitmap(bitmap);
How to add image(left) and text on button?
For illustration:
use android:drawableLeft="#drawable/image" in your layout xml
you can also do this from code
Drawable icon= getContext().getResources().getDrawable( R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds( icon, null, null, null );
android:text="Documentos"
android:drawableLeft= "#drawable/ic_document"
We can also manage the size of drawable while doing programmatically
Drawable dr = getResources().getDrawable(R.drawable.mag_green);
dr.setBounds(0, 0, 43, 40); //Left,Top,Right,Bottom
search_bar.setCompoundDrawables(dr, null , null , null);
I have a RelativeLayout object and want to dynamically change the background image with a dynamically created Bitmap object (it changes its color dynamically).
I saw that when I wanted to update the background image of the RelativeLayout object that I can only choose setBackgroundDrawable() which requires a Drawable object as a parameter.
My question is, how can I convert the dynamically created Bitmap object into a Drawable object?
BitmapDrawable(obj) convert Bitmap object into drawable object.
Try:
RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1);
Drawable dr = new BitmapDrawable(bit);
(view).setBackgroundDrawable(drawable);
I hope this will help you.
You can do it by this way
Drawable drawable = new BitmapDrawable(bitmap);
RelativeLayout r;
r = (RelativeLayout) findViewById(R.id.relativelayout1);
ll.setBackgroundDrawable(drawable);
Try this,
Drawable drawable = new BitmapDrawable(bitmap);
RelativeLayout rl=(RelativeLayout) findViewById(R.id.main1);
Bitmap myImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Drawable dr = new BitmapDrawable(myImage);
rl.setBackgroundDrawable(dr);
Drawable d = new BitmapDrawable(bitmap);
//Create Reference for RelativeLayout
RelativeLayout layout = (RelativeLayout) findViewById(R.id.rl);
//set Background to layout reference using the following method
Drawable drawable = getResources().getDrawable(R.drawable.bg);
layout.setBackground(drawable);
Note: R.id.rl is id of RelativeLayout
R.drawable.bg id the Image in drawable folder
I'm not able to show an image which is saved in res/drawable folder.
I use ImageGetter to do this. The code is below:
ImageGetter imageGetter3 = new ImageGetter() {
public Drawable getDrawable(String source) {
int id=0;
if (source.equals("smiley")) {
id = R.drawable.smiley;
}
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
directions += "Je bent bij lokaal " + vertrek.getNaam() + "\n"
+ "Sta met je rug voor de deur\n"
+ Html.fromHtml("<img src=\"smiley\">", imageGetter3, null) + " Draai naar links\n";
What I see on the screen when running is a little square with "obj" text on it.
So what is wrong? The image cannot be read or something else?
How to show images?
Honestly I have Googled a lot and tried other methods of ImageGetter as well, but none of them seems to work, I tried these too, they don't work:
ImageGetter imageGetter2 = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = null;
d = Drawable.createFromPath(source);
d.setBounds(0,0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawFromPath;
int path = Route.this.getResources().getIdentifier(source, "drawable","com.prj56.tracingapp");
drawFromPath = (Drawable) Route.this.getResources().getDrawable(path);
drawFromPath.setBounds(0, 0, drawFromPath.getIntrinsicWidth(), drawFromPath.getIntrinsicHeight());
return drawFromPath;
}
};
=========================================================
if (....) {
ImageView iv1 = new ImageView(this);
iv1.setImageResource(R.drawable.smiley);
directions += "Je bent bij lokaal " + vertrek.getNaam() + "\n"
+ "Sta met je rug voor de deur\n";
HERE COMES THE IMAGE! BUT HOW TO DO THIS? It's within directions textview...
directions += " Draai naar links\n";
}
Update 12 Dec 2016:
getResources().getDrawable() was deprecated in api 22 so you should now be using ContextCompat.getDrawable e.g.
Drawable d = ContextCompat.getDrawable(context, R.drawable.smiley);
In your activity you can call this to get your Drawable resource as a Drawable object
Drawable d = getResources().getDrawable(R.drawable.smiley);
If you want to show your drawable in an ImageView you can do this:
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.smiley);
or in your xml file
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/smiley"/>
You can get the image with the help of pImg.Based on the adapter position get the string that is the image name.The image should be present in the drawable folder.
ImageView prodImg=(ImageView)view.findViewById(R.id.img_proj_name);
String pImg=prod.get(position);
int resID = view.getResources().getIdentifier("#drawable/"+pImg , "drawable", parentView.getContext().getPackageName());
prodImg.setImageResource(resID);
A super easy way to show it is to write this in your ImageView widget in your xml:
android:background="#drawable/your_file_name"
... and make sure you have put that image file (whether png or jpg, etc.) into your drawable folder. When you write your_file_name, you only need the title, not the file extension.
I prefer this way over programmatically writing in your .java file (as shown with other answers above), because your xml will show you the image in preview, programmatically will not, it will just show the ImageView placeholder.
Easiest way - Can be consider the below code
We can take advantage of Imageview setImageResource , refer below code for the same.
put image in the drawable like the below order
image_1.png, image_2.png, etc.
int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", getPackageName());
imageview.setImageResource(resId);
Drawable drawable = getResources().getDrawable(R.mydrawableID);
Now you can use it as Image, Like:-
ImageView myImage = findViewById(R.id.yourImageView);
Now fire this -
myImage.setImageResource(drawable);
You should use ContextCompat for backward compatibility features on Android.
//Using ButterKnife
#BindView(R.id.DogImg)
ImageView myImage;
//Or
ImageView myImage = findViewById(R.id.MyImage);
...
Drawable MyImageDrw = ContextCompat.getDrawable(context,R.drawable.myImageOnResources);
myImage.setImageDrawable(MyImageDrw);
I have an image res/drawable/test.png (R.drawable.test).
I want to pass this image to a function which accepts Drawable, e.g. mButton.setCompoundDrawables().
So how can I convert an image resource to a Drawable?
Your Activity should have the method getResources. Do:
Drawable myIcon = getResources().getDrawable( R.drawable.icon );
As of API version 21 this method is deprecated and can be replaced with:
Drawable myIcon = AppCompatResources.getDrawable(context, R.drawable.icon);
If you need to specify a custom theme, the following will apply it, but only if API is version 21 or greater:
Drawable myIcon = ResourcesCompat.getDrawable(getResources(), R.drawable.icon, theme);
This code is deprecated:
Drawable drawable = getResources().getDrawable( R.drawable.icon );
Use this instead:
Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),R.drawable.icon);
The getDrawable (int id) method is deprecated as of API 22.
Instead you should use the getDrawable (int id, Resources.Theme theme) for API 21+
Code would look something like this.
Drawable myDrawable;
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
myDrawable = context.getResources().getDrawable(id, context.getTheme());
} else {
myDrawable = context.getResources().getDrawable(id);
}
I would just like to add that if you are getting "deprecated" message when using getDrawable(...) you should use the following method from the support library instead.
ContextCompat.getDrawable(getContext(),R.drawable.[name])
You do not have to use getResources() when using this method.
This is equivalent to doing something like
Drawable mDrawable;
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
mDrawable = ContextCompat.getDrawable(getContext(),R.drawable.[name]);
} else {
mDrawable = getResources().getDrawable(R.id.[name]);
}
This works on both pre and post Lollipop versions.
Get Drawable from vector resource irrespective of, whether its vector or not:
AppCompatResources.getDrawable(context, R.drawable.icon);
Note:
ContextCompat.getDrawable(context, R.drawable.icon); will produce android.content.res.Resources$NotFoundException for vector resource.
If you are trying to get the drawable from the view where the image is set as,
ivshowing.setBackgroundResource(R.drawable.one);
then the drawable will return only null value with the following code...
Drawable drawable = (Drawable) ivshowing.getDrawable();
So, it's better to set the image with the following code, if you wanna retrieve the drawable from a particular view.
ivshowing.setImageResource(R.drawable.one);
only then the drawable will we converted exactly.
If you are inheriting from a fragment you can do:
Drawable drawable = getActivity().getDrawable(R.drawable.icon)
You must get it via compatible way, others are deprecated:
Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.my_drawable, null);
In Kotlin you can do like this:
binding.floatingActionButton.setImageDrawable(
AppCompatResources.getDrawable(this, android.R.drawable.ic_delete))
Where binding is a root View and this refer to Context, You need import
import androidx.appcompat.content.res.AppCompatResources