Android – marginRight property of imageview not working

androidimageview

I have an image view whose background is a xml shape drawable i.e. a rectangle shape drawable.My app's orientation is fixed to landscape view.

The imageview is in relative layout.

I am trying to move it to the right of the screen by setting the appropriate value of layout_marginRight but this does not work .The imageView always stays in its's original position.

I have tried the following other options also but none helped.

The other options which I tried are:

  1. Creating a new relative layout params and setting the right margin
  2. Creating new margin layout params and setting the position
  3. Trying padding option
  4. Setting the imageview to right position relative to another imageview…
  5. Using display metrics to get width of screen and accordingly setting the margin….

I am stuck since a week setting the position of this imageview…

I was thinking the best approach is to set this imageview in between two imageview as I am not able to move it by setting margin but that does not work either…

Here is the current xml of my imageview in main.xml:-

<ImageView
            android:id="@+id/rect1"
            android:background="@drawable/rect"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" 
                android:layout_alignParentBottom="true"
                android:layout_marginRight="70dp"
            android:scaleType="fitCenter"/>

Best Answer

I don't see why adding a margin to the right of the image would help move the image to the right, it just extends the right side bounds of the imageview by the margin. As long as the imageview isn't actually on the right side of the parentview, it would be allowed to grow without changing position. I suggest using layout_alignParentRight="true" on the imageview if you want it on the right of your relativelayout, and then you can use the marginRight to control how far off the right side you want it.

Related Topic