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

AndroidLayout

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

Actually it's possible, despite what's being said in other answers. If you have a FrameLayout, and want to position a child item to the bottom, you can use android:layout_gravity="bottom" and that is going to align that child to the bottom of the FrameLayout.

I know it works because I'm using it. I know is late, but it might come handy to others since this ranks in the top positions on google

Solution 2 - Android

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.

Solution 3 - Android

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>

Solution 4 - Android

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

Solution 5 - Android

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"

Solution 6 - Android

android:layout_gravity="bottom"

Solution 7 - Android

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

Solution 8 - Android

Use bottom|right as the layout_gravity value of the element which you wish to align in the bottom right position of the parent layout.

android:layout_gravity="bottom|right"

Solution 9 - Android

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>

Solution 10 - Android

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

Solution 11 - Android

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?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRohith NandakumarView Question on Stackoverflow
Solution 1 - AndroidlblasaView Answer on Stackoverflow
Solution 2 - AndroidJaydeepWView Answer on Stackoverflow
Solution 3 - AndroidRohith NandakumarView Answer on Stackoverflow
Solution 4 - AndroidJakobView Answer on Stackoverflow
Solution 5 - AndroidJorgesysView Answer on Stackoverflow
Solution 6 - Androidsuperarts.orgView Answer on Stackoverflow
Solution 7 - AndroidRahulView Answer on Stackoverflow
Solution 8 - AndroidCodemakerView Answer on Stackoverflow
Solution 9 - AndroidximoedView Answer on Stackoverflow
Solution 10 - AndroidPawananjani KumarView Answer on Stackoverflow
Solution 11 - AndroidJuhaniView Answer on Stackoverflow