Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

How can I achieve this?

like image 636
Karthik Avatar asked Jan 06 '14 11:01

Karthik


People also ask

Which attribute is used to set an image in ImageView in Android?

src: src is an attribute used to set a source file or you can say image in your imageview to make your layout attractive. Below is the example code in which we set the source of a imageview lion which is saved in drawable folder.

How do I change the image size on Kotlin?

This example demonstrates how to resize Image in an Android App using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

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>  
like image 164
Andros Avatar answered Sep 28 '22 23:09

Andros