Joeblackdev
Joeblackdev

Reputation: 7327

Set image width and height in ImageView

No matter what I try, I cannot set the width and height of my image that is passed from a soap service to my android emulator. I'm using an ImageView as follows:

            byte[] bloc = Base64.decode(result, Base64.DEFAULT);         
            Bitmap bmp = BitmapFactory.decodeByteArray(bloc,0,bloc.length);    
            ImageView image = new ImageView(this);
            image.setImageBitmap(bmp);
            image.setLayoutParams(
                    new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT, 
                            LinearLayout.LayoutParams.WRAP_CONTENT)); 
            image.getLayoutParams().height = 100;
            image.getLayoutParams().width = 100;
            setContentView(image);

In the above code, I am attempting to set the width and height manually, of a jpeg image that has a 259px width and 194px height.

The /res/layout/main.xml looks like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1" android:id="@+id/llid">
</LinearLayout>

After trying some of the approaches in below's answers, I just see the following on my emulator

enter image description here

I'm not even sure if the approach I am taking is correct. Most other solutions I have found from searching the forums don't work for me. Any suggestions would be great.

Upvotes: 6

Views: 30299

Answers (4)

jeet parmar
jeet parmar

Reputation: 868

try this

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
imageView.setLayoutParams(params);

Upvotes: -1

C Nick
C Nick

Reputation: 2520

I'm not sure in what scope you're manually setting your image view, but if it is before the onMeasure gets called for that given ImageView, then that's probobly why it is respecting the XML layout params.

You can try overloading your onMeasure method for your ImageView, and call setMeasuredDimension(100,100) to manually set it there.

Upvotes: 1

Nikola Despotoski
Nikola Despotoski

Reputation: 50538

Try like this. Make a use of yourBitmap.getWidth() and .getHeight()

  image.setLayoutParams(
              new LinearLayout.LayoutParams(
                     bmp.getWidth(), 
                     bmp.getHeight())); 

Edit:

Now you have set the LP for the ImgView, the first thing you have to do is to add it to the layout

LinearLayout ll = (LinearLayout)findViewById(R.layout.llid);

Now you can either add your view straight or you can do it like separating the parameters and adding it with .addView(view, params) or .addView(View); Your call.

so you do

ll.addView(image);

Hope it works

Upvotes: 10

Kaj
Kaj

Reputation: 10949

What happens if you change this:

new LinearLayout.LayoutParams(
   LinearLayout.LayoutParams.WRAP_CONTENT, 
   LinearLayout.LayoutParams.WRAP_CONTENT)); 

To:

new LinearLayout.LayoutParams(width, height);

.. and is the object placed in a LinearLayout?

Upvotes: 3

Related Questions