Set image width and height in ImageView

30,230

Solution 1

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

Solution 2

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?

Solution 3

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.

Share:
30,230
Joeblackdev
Author by

Joeblackdev

Updated on July 10, 2022

Comments

  • Joeblackdev
    Joeblackdev almost 2 years

    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.

    • IgorGanapolsky
      IgorGanapolsky over 10 years
      image.getLayoutParams() returns read-only values (getters) that have already been set on this view. They are not setters to be used at runtime.
  • Joeblackdev
    Joeblackdev almost 13 years
    Hi. This didn't work unfortunately. My imageview is within a linear layout. I have updated my original post with the /res/layout/main.xml
  • Joeblackdev
    Joeblackdev almost 13 years
    I don't think my linear layout has an id (see my main.xml above). So do I need to set an id in my linear layout so that I can retrieve it? Thanks
  • Nikola Despotoski
    Nikola Despotoski almost 13 years
    Correct! Set up id for your LL
  • Joeblackdev
    Joeblackdev almost 13 years
    Hhmm, I set the id on my linear layout and have the following LinearLayout ll = (LinearLayout) findViewById(R.id.llid); System.out.println("***** LINEAR LAYOUT: " + ll); ll.addView(image); but, 'll' is null for some reason. I have updated my /res/layout/main.xml in my post above.
  • Nikola Despotoski
    Nikola Despotoski almost 13 years
    Why System.out.println("***** LINEAR LAYOUT: " + ll); ?
  • Joeblackdev
    Joeblackdev almost 13 years
    The reason I was doing that was because I was getting a null pointer exception. When I'm calling LinearLayout ll = (LinearLayout)findViewById(R.layout.llid); I get null for some reason, even though I have defined an id for my 'LinearLayout' in the main.xml
  • Joeblackdev
    Joeblackdev almost 13 years
    It's ok, I cleaned the project and the exception is gone. Now I have an 'IllegalStateException' because of this line of code setContentView(image); It says, 'The specified child already has a parent. You must call 'removeView()' on the child's parent first'.
  • Nikola Despotoski
    Nikola Despotoski almost 13 years
    Delete these lines image.getLayoutParams().height = 100; image.getLayoutParams().width = 100; setContentView(image);`
  • Joeblackdev
    Joeblackdev almost 13 years
    Aha! That worked. Thank you very much for this. Can you explain why this worked after removing those 3 statements?
  • Nikola Despotoski
    Nikola Despotoski almost 13 years
    Because setContentView() is to set explicit view for the activity you once did (settig the main.xml) and with that you set the parent fr your ImageView and with blabla.height = 100 you just assigned the value which you never use. I hope I was clear and simple enough :D