How can we set height and width dp for imageview in android?

Karthik picture Karthik · Jan 6, 2014 · Viewed 51.8k times · Source

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

How can I achieve this?

Answer

Andros picture Andros · Jan 6, 2014

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>