Mark Cortejo
Mark Cortejo

Reputation: 109

Align a button to the bottom right of screen?

I have a submit button here on the right of "Action" Button:

Submit Button

I've tried to use the code:

android:gravity="bottom|right"

but it doesn't work, and it still stays on the same place.

My full code is this:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="bottom"
    android:keepScreenOn="true"
    android:textSize="25dp"
    android:padding="30dp"
    android:textStyle="bold"
    android:textAlignment="center"
    android:text="@string/select_action"
    android:id="@+id/btnSelectPhoto"
    android:layout_alignParentBottom="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/btnSelectPhoto"
    android:keepScreenOn="true"
    android:gravity="bottom|right"
    android:padding="30dp"
    android:text="@string/submit"
    android:textSize="25dp"
    android:textStyle="bold"
    android:id="@+id/btnSubmit"
    android:layout_alignParentBottom="true"
    />

Even with the code above, the layout is still the same as the image. How can I fix this?

Fixed: Added android:layout_alignParentRight="true", Thanks to Egor N.

Upvotes: 1

Views: 3196

Answers (3)

Simin Ghasemi
Simin Ghasemi

Reputation: 91

You can use android:layout_gravity="end|bottom" to place your component on the right bottom corner of its parent.

<LinearLayout
    android:id="@+id/layout_mapFloatingButtons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:orientation="vertical">
 
    <ImageButton
        android:id="@+id/mapicon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/gf_map_selected"></ImageButton>
   
</LinearLayout>

The android:layout_alignParentEnd="true" and android:layout_alignParentTop="true" do not work for me.

Upvotes: 0

Khalil Meg
Khalil Meg

Reputation: 781

You can use a simlpe View between the two bottons

for example:

<LinearLayout
    android:orientation="horizontal"
    android:weightSum="3"
    android:layout_width="match_parent"
    android:layout_height="80dp"
>
    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
</LinearLayout>

Upvotes: 0

Egor Neliuba
Egor Neliuba

Reputation: 15054

android:layout_alignParentRight="true" should do the job. But, you should get rid of android:layout_toRightOf="@id/btnSelectPhoto".

android:gravity didn't work, because this value is applied to content inside the view (text inside a button).

Upvotes: 2

Related Questions