James King
James King

Reputation: 2445

Android - Aligning button to editview slightly off

I am wanting a button to be aligned to the the bottom and right of a EditText box.

My code is below:

<RelativeLayout
    android:id="@+id/global_only_available_home_delivery_postcode_check_layout"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_marginTop="5dp"
    android:layout_gravity="right"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/global_only_available_home_delivery_postcode_check_text_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/delivery_edit_hint"
        android:layout_marginLeft="@dimen/pdp_status_delivery_margin_5"
        android:layout_marginTop="@dimen/pdp_status_delivery_margin_10"
        android:textSize="14sp"
        android:inputType="text" />
     <Button
        android:id="@+id/fragment_pdp_deliverygo"
        style="@style/alternatesmallbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="35dp"
        android:minHeight="35dp"
        android:layout_marginLeft="10dp"
        android:text="@string/pdp_delivery_go"
        android:layout_gravity="right"
        android:layout_margin="5dp"
        android:layout_toRightOf="@+id/global_only_available_home_delivery_postcode_check_text_phone"
        android:layout_alignBottom="@+id/global_only_available_home_delivery_postcode_check_text_phone"
        android:visibility="visible" />
</RelativeLayout>

and here is what I get button alignment

As you can see from the image, the button doesn't seem to be aligned to the bottom of the textview.

Can anyone help?

Upvotes: 0

Views: 62

Answers (3)

Abhishek Shukla
Abhishek Shukla

Reputation: 1242

Try using layout_below instead of layout_alignBottom

Upvotes: 0

Amulya Khare
Amulya Khare

Reputation: 7698

This is because you have android:layout_margin="5dp" for the button. This assigns a margin of 5dp to the bottom of the button and it appears as if it is not aligned to the bottom. You should remove it and assign margins individually to other sides if you want eg:

android:layout_marginRight="5dp"
android:layout_marginTop="5dp" // etc (optional)

Upvotes: 1

Hariharan
Hariharan

Reputation: 24853

Try this..

Use android:layout_alignParentRight="true" and android:layout_centerVertical="true"

<Button
        android:id="@+id/fragment_pdp_deliverygo"
        style="@style/alternatesmallbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="35dp"
        android:minHeight="35dp"
        android:layout_marginLeft="10dp"
        android:text="@string/pdp_delivery_go"
        android:layout_gravity="right"
        android:layout_margin="5dp"              
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/global_only_available_home_delivery_postcode_check_text_phone"
        android:layout_alignBottom="@+id/global_only_available_home_delivery_postcode_check_text_phone"
        android:visibility="visible" />

Upvotes: 0

Related Questions