How to Make ImageView to Wrap its Contents? - android

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.

Related

Can I force a TextView to not set his dimension in order to fill the background?

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());

Resize ImageView in run time using a button - Android

I am displaying an image using ImageView container. the image needs to be resized when a user enters a specific value and clicks update
resize is just for display, the original resource remains as it is. NO editing, save, etc
xml layout
<ImageView
android:background="#drawable/picture01"
android:id="#+id/picture01holder"
android:layout_below="#+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
</ImageView>
main.java
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
now what do i do to dynamically assign this pic01 a size of my choice. just the function, i can implement it inside a button myself. i believe i have to use something like pic01.setLayoutParams but i dont know how to use it.
basically i want to overwrite layout_height="wrap_content" and layout_width="wrap_content" in .java
change the Imageview size by taking LayoutParams
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
pic01.setLayoutParams(layoutParams);
First of all, you have to set the scale type of the image to CENTER_INSIDE or CENTER_CROP depending on your preferences. You should do this in your onCreate() method.
myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Afterwords you just have to resize the image view in your layout. This will depend on the type of layout you're using. An example for LinearLayout would be:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100,200);
myImageView.setLayoutParams(params);
Or you can set the components minimal size:
myImageView.setMinimumWidth(200);
myImageView.setMinimumHeight(200);
In both cases make sure that the size can be achieved inside the layout. If you have multiple components with smaller weights, the image view may be unable to take up the space you need.
RelativeLayout.LayoutParams myParams = new RelativeLayout.LayoutParams(yourWidthHere, yourHeightHere);
pic01.setLayoutParams(myParams)

How to center a Imageview on the screen? (with java code)

atm i'm using this code, but this code doesn't works for me, because the code is stretching the Image on the screen, i dont want that, i need that the image uses his real size (200x210)
FrameLayout fl = new FrameLayout(this.getApplicationContext());
splash = new ImageView(getApplicationContext());
splash.setImageResource(R.drawable.logo2);
fl.addView(splash);
fl.setForegroundGravity(Gravity.CENTER);
fl.setBackgroundColor(Color.BLACK);
setContentView(fl);
How to do it without making a giant image that it is using all the screen?
You have to specify scalType if you don't want imageview to stretch your image.
splash.setScaleType(ScaleType.CENTER);
use this properties of ImageView as shown here:
splash.setAdjustViewBounds(true);
and tell me if it is working.

ImageView image not appearing in ViewGroup

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.

set extended ImageView width and height programatically - android

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.

Categories

Resources