30

I would like to set height and width in dp for ImageView in android pragmatically.

How can I achieve this?

Atef Hares
  • 4,715
  • 3
  • 29
  • 61
Karthik
  • 4,943
  • 19
  • 53
  • 86
  • http://stackoverflow.com/questions/5255184/android-and-setting-width-and-height-programmatically-in-dp-units ,http://androidactivity.wordpress.com/2011/10/04/use-dip-sp-metrics-programmatically/try this – Janmejoy Jan 06 '14 at 11:15
  • you accept [this](https://stackoverflow.com/a/23575369/5993410) as an answer! – Atef Hares Sep 10 '17 at 18:29

7 Answers7

73

Set width & height with dp :

imageview.getLayoutParams().height = (int) getResources().getDimension(R.dimen.imageview_height);
imageview.getLayoutParams().width = (int) getResources().getDimension(R.dimen.imageview_width);

In your dimens.xml provide values for the keys :

<dimen name="imageview_width">50dp</dimen> 
<dimen name="imageview_height">50dp</dimen> 
Andros
  • 4,069
  • 1
  • 22
  • 30
29

Use display metrics to get the sale factor and then just some simple maths - example, if I want 200x150dp:

final float scale = getResources().getDisplayMetrics().density;
int dpWidthInPx  = (int) (200 * scale);
int dpHeightInPx = (int) (150 * scale);

Then set the image view size:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(dpWidthInPx, dpHeightInPx);
imageView.setLayoutParams(layoutParams);
sham
  • 1,346
  • 1
  • 20
  • 28
6

This may be simpler and should do the trick:

ImageView im = (ImageView)findViewById(R.id.image1);
LayoutParams params = im.getLayoutParams();
params.height = getActivity().getResources().getDimensionPixelSize(R.dimen.item_height);
params.width = getActivity().getResources().getDimensionPixelSize(R.dimen.item_width);

And in your dimens.xml

<dimen name="item_height">80dp</dimen> 
<dimen name="item_width">80dp</dimen> 
velval
  • 3,072
  • 36
  • 45
1

This may help you...

ImageView im = (ImageView)findViewById(R.id.image1);
LayoutParams params = im.getLayoutParams();
params.height = 100;
params.width = 100;
lalith
  • 55
  • 7
  • DisplayMetrics metrics = getWindowManager().getDefaultDisplay().getMetrics(metrics); float logicalDensity = metrics.density; int px = (int) (dp * logicalDensity + 0.5); this may help u in converting pixels to dp – lalith Jan 06 '14 at 11:48
  • Lalith does the 0.5 stays a common factor for all devices or it will be different.. – Karthik Jan 06 '14 at 11:57
  • please select this answer as correct, if it satisfies ur question – lalith Jan 06 '14 at 12:21
1
  1. at first choose a desirable dip and assign it to a var.

int dipAmount=350;

  1. Then read the height of your ImageView.

float scale = imageview.Resource.DisplayMetrics.Density;

  1. Convert from px to dip.

int converter =(int) (350 * scale + 0.5f);

  1. Set the height to imageview.

imageView.LayoutParameters.Height=converter;

1
 final float scale = getContext().getResources().getDisplayMetrics().density;
int height_ = (int) (250 * scale + 0.5f);
int width_ = (int) (250 * scale + 0.5f);

250 is in dp

ViewGroup.LayoutParams params = ImageView.getLayoutParams();
params.height = height_;
params.width = width_;

ImageView.setLayoutParams(params);
-2

Try this:

image_view.getLayoutParams().height = 20;
image_view.getLayoutParams().width= 20;

Give me your feedback if it's acceptable. It's work for me.

Satyaki Mukherjee
  • 2,857
  • 1
  • 22
  • 26