22

I have an ImageView in my main.xml file:

<ImageView
            android:id="@+id/pageImage"
            android:layout_width="270dp"
            android:layout_height="45dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="15dp"
            android:scaleType="fitXY"
            android:src="@drawable/pageimage" />

What I only need to do is to set the the layout_height programmatically to another value.

Additionally, I need to set the value in dp. So it is currently 45dp, I'd like to change it to another value in my main activity.

I tried to figure out how to do this using LayoutParams, but I couldn't succeed.

Thanks.

Tobias Reich
  • 4,952
  • 3
  • 47
  • 90
capcom
  • 3,257
  • 12
  • 40
  • 50

2 Answers2

60

I think you want something like this:

ImageView imgView = (ImageView) findViewById(R.id.pageImage);
imgView.getLayoutParams().height = 200;

So first we get the ImageView from the XML. Then getLayoutParams() gets you exactly the params you may edit then. You can set them directly there. The same for the width etc.

About the unit (sp,dp...) I found something here. Hope this helps:

Use DIP, SP metrics programmatically

There they mention, that all units are pixels. In the example he sets the minimum width of 20sp like this:

TextView tv0 = new TextView(this);
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics());
tv0.setMinimumWidth(px);

P.S. Here is the complete code I use. Hopefully this is what you want:

package com.example.testproject2;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imgView = (ImageView) findViewById(R.id.pageImage);
        imgView.getLayoutParams().height = 500;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
Tobias Reich
  • 4,952
  • 3
  • 47
  • 90
  • I think getLayoutParms() can't be used to set a value, only retrieve it, right? – capcom Aug 11 '12 at 01:38
  • No, not at all. I just tried it. It works perfectly fine! :-) – Tobias Reich Aug 11 '12 at 01:40
  • Wait, you are able to set a value using your method? I am unable to. It requires a semicolon after the `height`. – capcom Aug 11 '12 at 01:41
  • Yes, it works. I posted my code in the answer. Hopefully this is what you wanted to do. Is there an exception or something like that for you? I compiled for Android 2.3 Maybe they changed something? Do you use Android 4.x? – Tobias Reich Aug 11 '12 at 01:44
  • BEAUTY! It works! How great, thanks so much. I owe quite a lot to you my friend :-) – capcom Aug 11 '12 at 01:50
  • One thing, the height is being set in px in your example, right? Not dp? I guess I can just convert dp to px though. – capcom Aug 11 '12 at 01:52
  • Well, the value you set are always pixsels. But you can calculate them (like in the example with sp). I don't have a dp converter around but I'm sure you may find one. Sorry. – Tobias Reich Aug 11 '12 at 01:55
  • What about this: http://stackoverflow.com/questions/4605527/converting-pixels-to-dp-in-android – Tobias Reich Aug 11 '12 at 01:56
  • Don't worry, I got it. Thanks again! – capcom Aug 11 '12 at 01:59
12

You can do something like this:

LinearLayout.LayoutParams layoutParams  = new LinearLayout.LayoutParams(width_physical_pixels, height_physical_pixels);
imageView.setLayoutParams(layoutParams);

But in the parameters it takes a pixel, so you would have to use the formula to convert pixels to dp, for example you can use this to convert:

float scale = getBaseContext().getResources().getDisplayMetrics().density;    
px = dp_that_you_want * (scale / 160);

Putting everything together:

//Get screen density to use in the formula.
float scale = getBaseContext().getResources().getDisplayMetrics().density;    
px = dp_that_you_want * (scale / 160);

LinearLayout.LayoutParams layoutParams  = new LinearLayout.LayoutParams(px, px);
imageView.setLayoutParams(layoutParams);

I don't know if this is what you are looking for, or if it will work, so let me know if it does.

0gravity
  • 2,682
  • 4
  • 24
  • 33
  • This works great too! Thank you so much. The answer below came first and is a little simpler. Please forgive me for not choosing your answer as the best. – capcom Aug 11 '12 at 01:51