[android] Android: Align button to bottom-right of screen using FrameLayout?

I am trying to put the zoom controls of the map on the bottom right corner of screen. I could do it with RelativeLayout using both alignParentBottom="true" and alignParentRight="true", but with Framelayout I did not find any such attributes. How do I align it to the bottom-right of screen?

This question is related to android layout

The answer is


There are many methods to do this using constraint widget of the activity.xml page of android studio.

Two most common methods are:

  1. Select your button view and adjust it's margins.
  2. Go to "All attributes",then to "layout_constraints",then select
    • layout_constraintBottom_toBottomOf_parent
    • layout_constraintRight_toRightOf_parent

It can be achieved using RelativeLayout

<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

   <ImageView 
      android:src="@drawable/icon"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_alignParentBottom="true" />

</RelativeLayout>

Setting android:layout_gravity="bottom|right" worked for me


If you want to try with java code. Here you go -

    final LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, 
                                             LayoutParams.WRAP_CONTENT);
    yourView.setLayoutParams(params);
    params.gravity = Gravity.BOTTOM; // set gravity

I also ran into this situation and figured out how to do it using FrameLayout. The following output is produced by the code given below.

Some right-bottom aligned text showing up over an image using FrameLayout.

              <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/contactbook_icon"
                        android:layout_gravity="center" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="140"
                        android:textSize="12dp"
                        android:textColor="#FFFFFF"
                        android:layout_gravity="bottom|right"
                        android:layout_margin="15dp" />
                </FrameLayout>

Change the margin value to adjust the text position over the image. Removing margin might make the text to go out of the view sometimes.


You can't do it with FrameLayout.

From spec:

http://developer.android.com/reference/android/widget/FrameLayout.html

"FrameLayout is designed to block out an area on the screen to display a single item. You can add multiple children to a FrameLayout, but all children are pegged to the top left of the screen."

Why not to use RelativeLayout?


android:layout_gravity="bottom"

Two ways to do this:

1) Using a Frame Layout

android:layout_gravity="bottom|right"

2) Using a Relative Layout

android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" 

you can add an invisible TextView to the FrameLayout.

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:visibility="invisible"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/daily_delete_btn"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:text="DELETE"
            android:layout_gravity="center"/>
        <Button
            android:id="@+id/daily_save_btn"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:text="SAVE"
            android:layout_gravity="center"/>
    </LinearLayout>