Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

marginRight property of imageview not working

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"/>
like image 631
Ruchira Avatar asked Aug 18 '11 12:08

Ruchira


People also ask

Which attribute is used to set an image in ImageView?

An ImageView control is used to display images in Android applications. An image can be displayed by assigning it to the ImageView control and including the android:src attribute in the XML definition of the control. Images can also be dynamically assigned to the ImageView control through Java code.

What is ImageView code?

ImageView class is used to display any kind of image resource in the android application either it can be android. graphics. Bitmap or android. graphics.

How padding works in Android studio?

The padding is expressed in pixels for the left, top, right and bottom parts of the view. Padding can be used to offset the content of the view by a specific number of pixels. For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge.


3 Answers

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.

like image 161
MrJre Avatar answered Nov 14 '22 22:11

MrJre


Make the layout_width attribute of the RelativeLayout to fill_parent and see it work :)

like image 32
jagmohan Avatar answered Nov 14 '22 22:11

jagmohan


My issue was that my parent layout's width was set to wrap_content, which doesn't have a defined width during the editor, I think. Thus, I couldn't use marginRight to get an offset. I had to change the width to a fixed size like match_parent or a constant dp size.

like image 44
Daniel Handojo Avatar answered Nov 14 '22 22:11

Daniel Handojo