0

I tried everything I could possibly find to right align this EditText element - but it won't.

What am I missing here? As you can see I tried both android:layout_alignParentRight = "true" and android:layout_gravity="right" without any luck.

ps. the linearLayout is just one element in a parent with RelativeLayout, but I guess that doesn't matter.

<LinearLayout android:id="@+id/block1"
  android:layout_width="fill_parent"
  android:paddingLeft = "4sp"
  android:padding = "4sp"
  android:layout_height="25sp"
  android:layout_marginTop = "4sp"
  android:layout_below="@+id/Settings_phoneNumbersLbl" 
  android:orientation="horizontal">

    <TextView android:id="@+id/Settings_phoneNumbersT1"
        android:textColor="#000000"
        android:textSize="14sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Settings_phoneNumbersT1"
    />

    <EditText android:id="@+id/myRightAlignedText"
        android:layout_width="170sp"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_alignParentRight = "true"
        android:layout_gravity="right"
    />

</LinearLayout>
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
user387184
  • 10,953
  • 12
  • 77
  • 147
  • 1
    you are using the orientation as horizontal. which appends the child views one after another. so i advice you to use RelativeLayout and use layout_alignParentRight, it will work. or add a empty view in the middle with weight 1. – Yashwanth Kumar Oct 05 '11 at 12:58
  • you want to align the edittext to right or the contents of edittext to right????? – Aju Oct 05 '11 at 12:59

4 Answers4

5

try to use RelativeLayout instead of LinearLayout.

<RelativeLayout android:id="@+id/block1"
  android:layout_width="fill_parent"
  android:paddingLeft = "4sp"
  android:padding = "4sp"
  android:layout_height="25sp"
  android:layout_marginTop = "4sp"
  android:layout_below="@+id/Settings_phoneNumbersLbl" 
  android:orientation="horizontal">

    <TextView android:id="@+id/Settings_phoneNumbersT1"
        android:textColor="#000000"
        android:textSize="14sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Settings_phoneNumbersT1"
    />

    <EditText android:id="@+id/myRightAlignedText"
        android:layout_width="170sp"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_alignParentRight = "true"
        android:layout_gravity="right"
    />

</RelativeLayout>
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • ..but then the height of the EditText shrinks. So I have to set the height to a fixed size - wrap_content will make it very thin... – user387184 Oct 05 '11 at 13:16
  • you set the height into the RelativeLayout, just set wrap_content/fill_parent instead of fixed size for height – Pratik Oct 05 '11 at 13:18
2

Try

android:gravity="right"

This is different from

android:layout_gravity="right"

The former affects positioning of content within the View, i.e. the text. The latter affects the View's position inside its parent. See Gravity and layout_gravity on Android for a full explanation.

EDIT

To align the EditText to the right of its parent, try changing the parent to be a RelativeLayout rather than a LinearLayout, so the layout_alignParentRight attribute can take effect.

Community
  • 1
  • 1
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • I should have mentioned it... this right aligns the text within the EditText, but I need to right align the complete box. – user387184 Oct 05 '11 at 12:57
2

You should use

android:layout_gravity="right"

because

android:gravity ="right"

means you are setting the children of the view (EditText here - that means text alignment) as right-aligned. To align the EditText itself, use the first one.

dragonfly
  • 411
  • 3
  • 11
1

As per LinearLayout property the child elements are always align one after another either its horizontally or vertically,

If you want to put the child element here in right side of parent then I think You should use FrameLayout.

or just use RelativeLayout and put the both child as per the respected to one-another.

Thanks.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
user370305
  • 108,599
  • 23
  • 164
  • 151