2

How to align view to right of the screen in Android ? I have used this but not worked please help ?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/widget0"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#FFFFFF" android:paddingLeft="10px" android:paddingTop="10px"
    android:paddingBottom="10px" android:paddingRight="10px">

    <LinearLayout android:id="@+id/heading" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:orientation="horizontal">

        <ImageView android:id="@+id/widget30" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:src="@drawable/logo_cpw">
        </ImageView>

            <EditText android:id="@+id/widget28" android:layout_width="134px"
                android:layout_height="35px" android:text="EditText"
                android:textSize="18sp" android:background="@drawable/rounded_edit_text_effects"
                android:gravity="right" android:layout_alignParentRight="true" android:paddingRight="-10px">
            </EditText>

    </LinearLayout>

</RelativeLayout>

I have used absoluteLayout instead of relativelayout, but that too not worked. Please help ?

oldergod
  • 15,033
  • 7
  • 62
  • 88
ramsbh
  • 656
  • 4
  • 14
  • 26
  • I think this anwser well help too : [http://stackoverflow.com/questions/4305564/android-layout-right-align](http://stackoverflow.com/questions/4305564/android-layout-right-align) – Soufiane Jun 08 '16 at 15:17

1 Answers1

17

You can use the android:layout_gravity attribute on your views - android:gravity is just for the content.

But I really recommend using RelativeLayout because it usually fixes most of the problems right away. In fact you already have an attribute android:layout_alignParentRight="true" defined which is available only in a RelativeLayout.

Edit: Oh, and do not use px in any of your layouts - use density-independent pixels (dp) instead! You should really read the User Interface chapter from the documentation.

mreichelt
  • 12,359
  • 6
  • 56
  • 70