5
<RelativeLayout>

    ...

    <ImageView
      android:id="@+id/imageView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_marginLeft="10dp"
      android:layout_marginTop=”11dp”
      android:background=”@drawable/image”  />

</RelativeLayout>

Then I need to set the Margin programmatically:

ImageView imageView = (ImageView) findViewRoot(R.id.imageView1);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
params.width = 100;
params.height = 100;
params.setMargins(0,0,0,0); //NOT WORK
imageView.setLayoutParams(params);

The ImageView still has marginLeft 10 and marginTop 11;

What is wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3240604
  • 407
  • 1
  • 8
  • 22

4 Answers4

0

Try using ViewGroup.MarginLayoutParams to set margins to your ImageView. Here is a method from @Hiren Patel answer that works perfectly for me.

private void setMargins (View view, int left, int top, int right, int bottom) {
    if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        p.setMargins(left, top, right, bottom);
        view.requestLayout();
    }
}

Sample of how to use in your code:

public void testMargin(View view){
    ImageView imageView = (ImageView) findViewById(R.id.imageView1);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)   imageView.getLayoutParams();
    params.width = 100;
    params.height = 100;
    setMargins(imageView , 0, 0, 0, 0);
    imageView.setLayoutParams(params);
}

My test layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_light"
    tools:context="com.g2o.test.TestActivity">

    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:background="@android:color/holo_red_dark" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="100dp"
        android:adjustViewBounds="true"
        android:background="@drawable/cast_ic_notification_0" />

    <Button
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:onClick="testMargin"
        android:text="Test" />" />

</RelativeLayout>

Hope this helps!!

Community
  • 1
  • 1
Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
0

Have you try to make a new LayoutParams?

ImageView imageView = (ImageView) findViewRoot(R.id.imageView1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT,      
    RelativeLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(0,0,0,0);
imageView.setLayoutParams(params);
ARP
  • 531
  • 3
  • 8
0

Hope this help put your ImageView in RelativeLayout and set it's width and height to Match-Parent

 <RelativeLayout
                android:id="@+id/lay_expandable"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                >

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@mipmap/ic_expansion" />

            </RelativeLayout>

and in your activity use LinearLayout.LayoutParams for example

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                    (LinearLayout.LayoutParams.MATCH_PARENT, 400);
            lay_layout.setLayoutParams(params);

// note expand the relative layout, not the image itself

Ahmed Karam
  • 386
  • 1
  • 7
-1

This can be useful for someone: In my case I was dealing with the same problem, that margins was not being recognized on android api 19, so what I did, it was to change the size of the image (increase a bit), and then adjust padding, like follows:

file: dimens.xml
<dimen name="image_padding_right">5dp</dimen>

then on code:

 val imageView1 = ImageView(this.activity)
 imageView1.setPadding(0,0,
 resources.getDimension(R.dimen.image_padding_right).roundToInt(), 0)
 imageView1.cropToPadding = true

the opcion cropToPadding allows you to do the workaround, in case margins dont work.

I hope it helps.

ZLNK
  • 811
  • 14
  • 17