I would like to set height and width in dp
for ImageView
in android pragmatically.
How can I achieve this?
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With