-1

I have two identical, equally-sized RelativeLayouts with some text and an image that I want to be aligned to the right side. However, there seems to be some sort of padding that doesn't let me put it on the right, even though I never specified this.

enter image description here

Here is my code:

<LinearLayout
    android:baselineAligned="false"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RelativeLayout
        android:id="@+id/my_relativelayout"
        android:background="@drawable/my_background"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="right">
        <TextView
            android:id="@+id/my_textview"
            android:textAppearance="?android:attr/textAppearanceButton"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
        <ImageView
            android:id="@+id/my_imageview"
            android:layout_centerVertical="true"
            android:scaleType="center"
            android:src="@drawable/my_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/my_textview">
        </ImageView>
    </RelativeLayout>

    <RelativeLayout
        ... same thing here ...>
    </RelativeLayout>

</LinearLayout>

Edit: The empty space IS NOT the other RelativeLayout. The other RelativeLayout is another "blue rectangle" with text and an image, and is also experiencing the issue of the space on the right side. Like so:

enter image description here

Kalina
  • 5,504
  • 16
  • 64
  • 101
  • 1
    This space is because of your other . What is in there? – iTurki Aug 30 '12 at 22:02
  • @iturki, no, there are two identical RelativeLayouts in the LinearLayout, each with a weight of 1. i.e., so each one takes up half of the screen space. Both of these RelativeLayouts have the space on the right side. – Kalina Aug 31 '12 at 15:08

5 Answers5

0

It's because you're using the android:layout_toRightOf attribute. This will align the left edge of your current view with the right edge of the view that you're referencing (my_textview). If you want the right side of your view to match the right side of your parent, try android:layout_alignParentRight="true" instead.

MattDavis
  • 5,158
  • 2
  • 23
  • 35
  • Firstly, that did not change anything :( Secondly, I don't just want the image to be on the right side; I want the text and the image to be together, with no space between them. – Kalina Aug 31 '12 at 15:11
0

try this layout instead:

    <ImageView
        android:id="@+id/my_imageview"
        android:layout_centerVertical="true"
        android:scaleType="center"
        android:src="@drawable/my_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:Layout_alignParentRight="true">
    <TextView
        android:id="@+id/my_textview"
        android:textAppearance="?android:attr/textAppearanceButton"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/my_imageview">
    </TextView>
iTurki
  • 16,292
  • 20
  • 87
  • 132
  • This won't work, you can't put a view `toLeftOf` itself. I think you meant to make it `toLeftOf="my_imageview"`, in which case I believe you'd need declare the ImageView before the TextView. You'll crash or the compiler will complain if you reference an ID that hasn't been declared yet in that file. – MattDavis Aug 30 '12 at 22:18
  • 1
    Edited. although I think the order isn't important in the referencing. – iTurki Aug 30 '12 at 22:28
  • 1
    You're right! http://developer.android.com/guide/topics/ui/layout/relative.html Last paragraph in the Positioning Views section. I stand corrected, although I could have sworn I've had issues with that in the past. – MattDavis Aug 31 '12 at 12:31
  • @MattDavis Thanks for sharing that. However, you should avoid the confusion and stick to the order. – iTurki Aug 31 '12 at 12:35
0

Check the bounds of the image(in an image editor) you are using in the image view. Looks like there is some transparent area in the image that is causing the confusion/problem.

if that is not the problem : try this add the attribute alignParentRight="true" for the ImageView instead of android:gravity="right" which does not work for RelativeLayouts

Vinay W
  • 9,912
  • 8
  • 41
  • 47
0

The solution that ended up working was to put

android:paddingRight="0dip"

in each of the RelativeLayouts, strangely enough, though I never said that there should be any padding. I'd been trying to figure this out for DAYS; thanks everyone for your input!

Kalina
  • 5,504
  • 16
  • 64
  • 101
-1

Because your LinearLayout has a horizontal layout, android:orientation="horizontal", that space will be taken by whatever object is next to the first RelativeLayout, which in your case looks like a second RelativeLayout.

If that doesn't help I would try tweaking the layout_weight and layout_width attributes of those two Relative Layouts.

Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141
  • No, there are two identical RelativeLayouts in the LinearLayout, each with a weight of 1. i.e., so each one takes up half of the screen space. Both of these RelativeLayouts have a TextView and ImageView, and the space on the right side. – Kalina Aug 31 '12 at 15:12
  • Why the downvote? Your question was unclear enough that several of us thought the second relative layout was the space to the right. Glad you edited the post to make it more clear. – Kyle Clegg Aug 31 '12 at 19:15