I have an ImageView as a child of a ViewGroup that is part of a GridView. The drawable of the ImageView does not appear, however I can see the background color, and if I use the drawable in setBackgroundDrawable instead, it is displayed. I've tried changing every property and calling invalidate() but none have made the drawable appear.
The Log.i prints
android.graphics.drawable.BitmapDrawable#407bd4c0
setImageResource(int) doesn't work either.
I'm using Honeycomb.
public MyViewGroup(Context context) {
super(context);
myImageView = new ImageView(context);
myImageView.setBackgroundColor(Color.RED);
myImageView.setScaleType(ImageView.ScaleType.CENTER);
Drawable drawable = this.getResources().getDrawable(R.drawable.myimage);
myImageView.setImageDrawable(drawable);
Log.i("",drawable+"");
//myImageView.setImageResource(R.drawable.myimage);
addView(myImageView);
}
I omitted the onLayout code where I call setLeft() setTop() setRight() and setBottom().
Any help is much appreciated. Thanks.
Update:
Also doesn't work with android.R.drawable images. I'm stumped.
Maybe the problem is, that you don't set the layout parameters. Try:
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, WRAP_CONTENT);
myImageView .setLayoutParams(params);
Try setting getting the image as bitmap, then setting it as drawable.
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.myimage)
Drawable drawable = new BitmapDrawable(bmp);"
myImageView.setImageDrawable(drawable);
Just out of curiosity: Why you do all of this in your constructor?
If the parent layout has width/height of fill_parent, changing it to wrap_content might do the trick.
I was having this problem in Honeycomb using an ImageView with a drawable in a layout - the image was there judging from the width of the dialog I had it appearing in, but I couldn't see the image itself. The ImageView's parent was a LinearLayout with fill_parent. Switching to wrap_content fixed it right up.
Related
I want to set a bitmap as a view background without stretching and i dnt want to change the view width and height as wrap-content. I want to keep the width and height of view as match-parent. I have set background of view like below, where mDrawingView is a custom view which extends View.
Bitmap tempBitmap = BitmapFactory.decodeFile(copy.getAbsolutePath());
mDrawingView.setBackground(new BitmapDrawable(getResources(), tempBitmap));
and my layout file looks like below,
<MyPackageName.DrawingView
android:id="#+id/drawing_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"/>
And please note I'm not using imageview.
Please suggest me any idea?
I have changed my DrawingView which extends Imageview instead of View and then set the bitmap like below
Bitmap bitmap = BitmapFactory.decodeFile(copy.getAbsolutePath());
mDrawingView.setImageBitmap(bitmap);
Now bitmap displays on imageview without stretching and works perfect.
Thanks for your valuable comments and answers.
if your are using image view then you can do like below
ImageView.ScaleType
which can be CenterCrop,fit etc you can read about it here it's android native support
https://developer.android.com/reference/android/widget/ImageView.ScaleType.html
You can do
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(R.drawable.res_id);
where res_id is the resource id in drawable folder.
In Short
I have an ImageView with size set to wrap_content.
The image has a drawable already set, and everything looks great.
Now, I want to change the drawable, while forcing the ImageView to keep it's size.
Explanation
Well, the easy answer is of course to set the width & height to a fixed size.
But in my case, I am copying an ImageView + layout from a Google Map to add my custom map button. It works well, because I have an icon with same size.
Now I want to change the icon for a moment (loading icon btw), but because the Drawable source is larger, it changes the ImageView size accordingly.
ScaleType doesn't work as the width and height are not fixed.
What I've Tried
1. Setting the new Drawable bounds using getBounds on the old one.
2. setRight() on the ImageView with the right bound of the existing map button (the original one which doesn't change).
(left is already fixed, it's inside a RelativeLayout).
Some (working) Code of how I created the new button
ImageView newBtn = new ImageView(context);
newBtn.setAdjustViewBounds(false);
newBtn.setBackgroundResource(R.drawable.map_btn_background);
newBtn.setScaleType(ScaleType.CENTER_INSIDE);
newBtn.setImageResource(imageResId);
newBtn.setClickable(true);
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationBtn.getLayoutParams();
rlp = copyRelativeLayoutParams(rlp);
//add to Google's map button layout
btnContainer.addView(newBtn, rlp);
Some (not working) Code of how I change the drawable.
This is the point were I want the ImageView to keep it's size.
perfectlyMadeNewMapButtonImageView.setImageDrawable(mLoadingDrawable);
//mLoadingDrawable.setBounds(
// perfectlyMadeNewMapButtonImageView.getDrawable().getBounds());
// not working
I need to put an image (downloaded via HTTP request) as a TextView's background programmatically.
On this image I have to change the scaleType (FIT_XY, CENTER, FIT_CENTER, ...). Since that TextView doesn't allow to set the scaleType, my idea was to:
create an ImageView
set the downloaded image as background to the ImageView
set the ImageView's drawable as TextView's background
Unfortunately this approach doesn't work. I think that the TextView try to expand himself to bound the image.
Anyone can confirm my assumption?
Any possible solution?
CODE ADDED
ScaleType scaleType = ImageUtil.parseStretchOption(model.background.backgroundImageStretchOption);
ImageView iv = new ImageView(getContext());
iv.setScaleType(scaleType);
iv.setBackground(new BitmapDrawable(getContext().getResources(), future.get()));
TextView view = new TextView(getContext());
view.setBackground(iv.getBackground());
I'm a starter Android learner and I think that this is a very easy question but I can't find it anywhere. I have added an ImageView like this:
//....
iv = new ImageView(context);
iv.setBackground(getResources().getDrawable(drawable));
iv.setAdjustViewBounds(true);
RelativeLayout.LayoutParams imajpara=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
iv.setMaxWidth(100);
iv.setMaxHeight(100);
iv.setLayoutParams(imajpara);
iv.setScaleType(ScaleType.CENTER_INSIDE);
//....
But the ImageView (a ball picture in this case) fits the entire screen, even though the image (.png) is 100x100 pixels. In the code you see my attempts to fix this but none of them worked. The imageview is in a RelativeLayout.
So how can I make the ImageView resize itself according to the image's dimensions?
Use setImageDrawable() instead of setBackground().
Instead of indicating "WRAP_CONTENT" in the constructor of your Layoutparams, you can set directly the right dimensions.
I have a custom class that extends ImageView that I'm programatically putting on to a RelativeLayout. I want each one of these images to be a specific width and height. I've tried setting the width/height in code with LayoutParams and with setMaxWidth/setMaxHeight...neither seem to be working.
Here is the code from the constructor of the class:
LayoutParams p = new LayoutParams(50,67);
this.setLayoutParams(p);
this.setImageResource(WhichImage(wi));
this.setMaxWidth(50);
this.setMaxHeight(67);
I've moved the call to setImageResource() above and below everything there and it still defaults to the true image size in my drawables...
You probably need to tell the ImageView how to scale. Try using ImageView#setScaleType.