Change the size of an Imageview in android


Ask by : Ruwan Dissanayaka December 01, 2014 22:20

I set the default size of an imageview as width=511 and height=385 . Source of the imageview is from drawable. Based on a dynamic value I need to change the size of the image view or the image fit in to it. For example I want to change the size of the imageview or image inside it to width=140 and height=100

ImageView scale=(ImageView) findViewById(R.id.scaleimage);
View original question

Answer by : BoogerDecember 01, 2014 22:20

If you set the width and height attributes of your ImageView to specific values, the system will automatically scale you images to fit.

Unless I don't understand your question, I think it is as simple as:

<ImageView
    android:layout_width="140dp"
    android:layout_height="100dp"
    android:scale_type="fitXY"
/>

To set this in code, try:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(140,100);
yourView.setLayoutParams(layoutParams);
View original answer