Set width and height of an imageview programmatically

I am making an imageview and i want to set the width and height to a specific number or wrap_content depending on the image.
At this point I have the following starting code :

ImageView i = new ImageView (this);
i.SetImageBitmap(bm);
i.SetAdjustViewBounds (true);
i.SetX (0);
i.SetY (0);

I can't figure out how to set it. I was thinking of i.width or i.LayoutParameters but I have no idea how.

Best Answer

  • Accepted Answer

    You have to use LayoutParameters to set the width and height of a View. For example:
    LinearLayout layoutBase = FindViewById<LinearLayout> (Resource.Id.layoutBase); ImageView iv= new ImageView (this); iv.LayoutParameters = new LinearLayout.LayoutParams (ViewGroup.LayoutParams.FillParent,ViewGroup.LayoutParams.FillParent); iv.Visibility = ViewStates.Visible; iv.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.Icon)); layoutBase.AddView (iv);

Answers

  • VincentMaverickDuranoVincentMaverickDurano USMember ✭✭
    Accepted Answer

    You have to use LayoutParameters to set the width and height of a View. For example:
    LinearLayout layoutBase = FindViewById<LinearLayout> (Resource.Id.layoutBase); ImageView iv= new ImageView (this); iv.LayoutParameters = new LinearLayout.LayoutParams (ViewGroup.LayoutParams.FillParent,ViewGroup.LayoutParams.FillParent); iv.Visibility = ViewStates.Visible; iv.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.Icon)); layoutBase.AddView (iv);

  • MartijnvandenBergMartijnvandenBerg USMember ✭✭

    thanks

Sign In or Register to comment.