I've tried several different ways but the imageview still show only a gray square. I'm testing on API 17.
Below are the options I've tried unsuccessfully:
Configured the ImageView in XML to:
fixed width and height; width and height to wrap_content; width to match_parent and height to wrap_content
android: scaleType to "fitXY" and "centerCrop"
android: adjustViewBounds to true | false;
Image loading methods tried:
binding.captcha.setImageBitmap (BitmapFactory.decodeFile (imgFile2.getAbsolutePath ()));
Also tried this answer https://stackoverflow.com/a/35830800/1764042
Tried Glide:
Glide.with(this)
.load (Uri.fromFile (imgFile2))
.skipMemoryCache (true)
.diskCacheStrategy (DiskCacheStrategy.NONE)
.override (150, 150)
.centerCrop ()
.into (binding.captcha);
The option below is the only that displays the image (in background), however I would like to know what could be happening to prevent me from displaying the image using default methods ...
Drawable drawableImage = new BitmapDrawable(BitmapFactory.decodeFile(imgFile2.getAbsolutePath()));
binding.captcha.setBackgroundDrawable(drawableImage);
Notes:
imgFile2 is not empty, it is loaded from SD Card.
imgFile2 is showing if I use it as background so the problem is not with the file.
The problem is not simply solved with a lib, I'm already trying Glide... But if you Know what I need to do to make it work with glide...
The imageView is inside a fragment, if it matters.
Current xml:
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:id="#+id/captcha"/>
For those who are still facing this issue, for me this is caused because of android:tint property of ImageView to which I was setting image into using Glide.
I was displaying a dummy placeholder image with android:tint as a shade of grey while the actual image loads from url. And that tint was causing the problem.
So try removing the android:tint property alltogether or set the tint to null programmatically after loading the image using imageView.setImageTintList(null)
Hope it helps
Related
I am trying to set a SVG Icon as the background of an Android ImageView.
The SVG Icon will receive some padding and colors are being changed a little.
<ImageView
android:id="#+id/passwordLabel"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#android:color/black"
android:padding="30px"
android:tint="#android:color/white"
app:srcCompat="#drawable/ic_lock"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="524dp" />
Is there a way to make the ImageView circular now?
I have tried other solutions but am confused, as they demand me to change the background.
You can't make the imageView circular, because it just displayed the image that already set.
Use CircleImageView library instead
Wrap your ImageView with CardView and make the radius half of its width/height
Change your image shape to be circle instead
1- you can use this library
https://github.com/hdodenhof/CircleImageView
2- use cardview
put imageview inside it and make radius half of width or height
(as suitable for your use case) also see this link
The actual image is this:
the red boxex are where i made 1 px black likes. Even the preview shows fine. So no problem with 9-patch
but the image i get for the following layout is:
<ImageView
android:id="#+id/image_landing"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#drawable/loginheader"
android:contentDescription="#string/login_header"
/>
I Expected, the logo to be on the left and black dots on the right and the rest of the space between them is filled with grey color i selected on the top
Thank You
Is your ImageView really bigger than your 9patch?
If not, you need to change scaleType as defaut is FIT_CENTER.
Use android:scaleType="fitXY" or android:adjustViewBounds="true".
It looks like your 9 patch is larger than the ImageView that you are trying to put it in. Try setting the ImageView to wrap_content to see if it fixes the problem. If it does, try making the 9 patch smaller, the ImageView bigger or set the scaleType as pcans mentioned.
I want set a background colour to an ImageView, when the imageview has an image resource, it works and i get my background colour but when the image view don't have an image resource, SetBackgroundColor don't work and do nothing, this is my code :
xml Layout :
<ImageView android:layout_centerHorizontal="true"
android:id="#+id/favor_item_image"
android:layout_alignParentTop="true" android:layout_height="wrap_content"
android:layout_width="match_parent" android:adjustViewBounds="true"/>
java code :
ImageView favorImage = (ImageView) MyView.findViewById(R.id.favor_item_image);
//favorImage.setImageResource(R.drawable.btndelete);
favorImage.setBackgroundColor(Color.argb(255,255,0,0));
So, when i comment this line : //favorImage.setImageResource(R.drawable.btndelete); i cannot set the image view background colour , but when i uncomment this line, setBackgroundColor works fine.
any help please.
Thanks
The reason you are not seeing anything is because you have the layout_height set to wrap_content. When you do not have an image set on the ImageView wrap_content sets the height to 0. If you want the color to display without an image you will have to set the layout_height to an actual value such as 50dp.
Your ImageView has a height of wrap_content, and when you only set the background color, it has no content. Therefore doesn't show up. You should be able to fix this by setting a different height.
Try setting: android:adjustViewBounds=false I may be mistaken but when this is set to true the view might scale to 0 width and 0 height when it doesn't have an image.
I have seen these different approaches in setting images but I don't get the difference.
Why there two methods?
setBackgroundResource is for setting the background of an ImageView.
setImageResource is for setting the src image of the ImageView.
Given:
ImageView iv = new ImageView(this);
Then:
iv.setBackgroundResource(R.drawable.imagedata);
Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.
imageView.setImageResource(R.drawable.imagedata);
Will occupy only the size of the image in ImageView.
For that you want to also set
android:layout_width="wrap_content"
android:layout_height="wrap_content"
for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.
SetBackdroundResource is for a drawable or color you want to set at the background of the imageview and your setImageResource is like to display on it.
so setImageResource is for add any resource to your imageview's front side. try this example and look at the difference. Android Gallery, ImageView Example
. This is a two layer effect,backside (setBackgroundResource) and frontside (setImageResource).
The method setBackgroundResource() belongs to all Views. The method setImageResource() only belongs to ImageView. You can set them both:
imageView.setBackgroundResource(R.drawable.sky);
imageView.setImageResource(R.drawable.balloons);
The setBackgroundResource() method will cause the image's width and height will be stretched to fill the size of the view. The setImageResource() method will let its image keep its aspect ratio.
My fuller answer is here.
setBackgroundResource sets the background image of an ImageView. The XML attribute is: android:background
setImageResource sets the image displayed in an ImageView. The XML attribute is: android:src
I am a puzzled about using src or background for an ImageView.
I know the former means the content of this ImageView and the latter means the background of the ImageView.
But how to decide which one to use? I don't see the difference.
All views can take a background image.
The src to an ImageView has additional features:
different scaling types
adjustViewBounds for setting bounds to match image dimensions
some transformations such as alpha-setting
And more that you may find in the docs.
when you use android:background, image will be set to fit on ImageView area(i.e according to width and height of ImageView). It doesn't matter if the image is smaller or larger than ImageView.
when you use android:src, then image will display in its original size. No
automatic scaling, adjustments will happen.
Note: Using android:src, we can get additional benefit of adjustViewBounds property
If you set an image to be the background of your ImageView, then the image will scale to whatever size the ImageView is. Other than that, src is a foreground image and background is a background image. Pretty much as it implies.
The difference between XML attribute src and background in ImageView:
The background will stretch according to the length given by the ImageView component, and SRC will hold the size of the original image without stretching. SRC is the picture content (foreground), BG is the background, can be used at the same time.
In addition: ScaleType only works on SRC; BG can set transparency, for example, in ImageButton, you can use Android:scaletype to control how the image is scaled, sample code as follows:
<ImageView
android:id="#+id/img"
android:layout_height="60dip"
android:layout_width= "60dip"
android:src="#drawable/logo"
android:scaleType="centerInside"
android:layout_centerVertical= "true"/>
Feel free to ask doubt if you get any.