3

I want to set relative layout to wrap content , and align all of the children to the right , here the xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout18"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000">


    <TextView
        android:id="@+id/textViewMsgDate"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_gravity="right"
        android:layout_marginRight="8dp"
        android:gravity="center"
        android:textColor="#ffff"
        android:text="simple"
        />

</RelativeLayout>

enter image description here

but as you can see in the picture i dont succeed ? although if i align to the left wrap_content work as expected .

Onik
  • 19,396
  • 14
  • 68
  • 91
david
  • 3,310
  • 7
  • 36
  • 59

1 Answers1

2

The problem here is that you are aligning the child of the RelativeLayout to its parent's(RelativeLayout) right, since RelativeLayout's width is set to wrap_content this won't move the child.

If you wan't to align the child to right of the parent, parent should have greater width than it's child. So either give RelativeLayout some width or make it "match_parent" or align the RelativeLayout itself to right, so that its child(TextView) moves along with it.

Note : if you align to left and make the RelativeLayout width "wrap_content", it behaves as you expected because, RelativeLayout is already aligned to left, thus it the child as well.

EDITED

To achieve what you want, simply do this :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout18"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RelativeLayout
    android:id="@+id/textview_wrapper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="#000">

    <TextView
       android:id="@+id/textViewMsgDate"
       android:layout_width="wrap_content"
       android:layout_height="50dp"
       android:layout_marginRight="8dp"
       android:textColor="#ffff"
       android:text="simple"
    />
</RelativeLayout></RelativeLayout>

The result should be something like this :

Output

Varun Verma
  • 692
  • 7
  • 13
  • thank you for your answer. but i don't want to set it to match parent as i don't want the background black color fill all the widht. – david Sep 09 '15 at 17:38
  • I have edited my answer, and added solution for your case. It should work :) – Varun Verma Sep 09 '15 at 17:47
  • thank you again. but it it still not working. the text isn't aligned to the right ! – david Sep 09 '15 at 17:56
  • you mean the textview is not aligned to the right, or the text itself ? If you are talking about text then it is probably because you have given it a margin of 8dp towards right. – Varun Verma Sep 09 '15 at 18:08
  • i mean the textview . one minute , does relativeLayout.layoutParams has layout_gravity . as i don't found it in the documentation http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html – david Sep 09 '15 at 18:11
  • Check out the, edited answer. Added output screenshot. – Varun Verma Sep 09 '15 at 18:13
  • yes if you remove the layout_gravity='right' from the text view. nothing will be changed ! right ? – david Sep 09 '15 at 18:19
  • yeah, sorry! I just copied your code. It shouldn't be there. – Varun Verma Sep 09 '15 at 18:21
  • what i want is to align the textview to the right . the same as your picture but it should start from the right ? – david Sep 09 '15 at 18:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89192/discussion-between-varun-verma-and-david). – Varun Verma Sep 09 '15 at 18:22