Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align Floating Action Button to center

I have one FloatingActionButton. I want it to be middle of 2 LinearLayouts and center of screen like this. enter image description here

Currently My design is like this

enter image description here

I want it to be at the exact center of the screen. How to do that?

This is my whole xml code

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/viewA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.6"
        android:orientation="horizontal"/>
    <LinearLayout
        android:id="@+id/viewB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:orientation="horizontal"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:src="@drawable/ic_plus"
    app:layout_anchor="@id/viewA"
    android:layout_centerHorizontal="true"
    app:layout_anchorGravity="bottom"
    android:layout_centerInParent="true" />

like image 364
dev Avatar asked Aug 20 '15 11:08

dev


People also ask

How do I change the position of my floating action button?

To change the location of the floating action button on the screen, use floatingActionButtonLocation property of Scaffold() widget.

Is Floating Action button docked?

You can dock a FloatingActionButton by specifying floatingActionButtonLocation in a Scaffold and using BottomAppBar .

Can you have two floating action buttons?

When you wish to add two floating buttons, you can opt for the stack, row, or column widget. Developers allot stack to the floating action button and present it as a two-position widget as children and make two floating actions easily.


1 Answers

Above solutions did not work for me somehow. I changed the android:layout_gravity to

android:layout_gravity="bottom|center"

Hope it helps :-)

like image 164
Mustafa Yousef Avatar answered Oct 11 '22 04:10

Mustafa Yousef