Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Adjust Image view width dynamically according to the screen size in android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 5.81k
    Comment on it

    It is very difficult to re-size the image-view width according to the screen without losing the quality of the image or you can say increase the width in proportionate to height of the image view.

    I will show you the trick how you can do that, you just have to set this class from the xml.

    public class ResizableImageView extends ImageView {
    
    public ResizableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
         Drawable d = getDrawable();
    
         if(d!=null){
                 // ceil not round - avoid thin vertical gaps along the left/right edges
                 int width = MeasureSpec.getSize(widthMeasureSpec);
                 int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
                 setMeasuredDimension(width, height);
         }else{
                 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         }
    }
    }
    

    like this

    <com.xml.ResizableImageView
                android:id="@+id/option_icon"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:adjustViewBounds="true"
    

    Enjoy:- android:scaleType="fitXY" />

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: