Reputation: 1136
For a strange reason my floating action button wont go down to the lower right portion of my android activity which is suppose to be its default behavior.
What did I do wrong in my layout.xml ?
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:clickable="true"
android:src="@android:drawable/ic_menu_myplaces" />
BELOW IS MY WHOLE LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="100dp"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main"
tools:context=".MainActivity"
android:elevation=".5dp"
android:background="@drawable/trizbg"
android:focusable="true">
<ImageView
android:id="@+id/imgLogo"
android:layout_width="250dp"
android:layout_height="150dp"
android:src="@drawable/h_biglogo"
android:paddingTop="8.5dp"
android:paddingBottom="8.5dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/hiaTextField"
android:layout_alignEnd="@+id/hiaTextField"
android:layout_above="@+id/hiaTextField" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/hiaTextField"
android:selectAllOnFocus="true"
android:textColorHint="#ffffffff"
android:hint="Tell us where you're going"
android:drawableLeft="@android:drawable/ic_search_category_default"
android:text=""
android:autoText="false"
android:layout_gravity="center_horizontal|bottom"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEARCH"
android:id="@+id/hiaButton"
android:onClick="hiaSubmitClick"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:layout_below="@+id/hiaTextField"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="18sp"
android:textColor="@color/colorWhite"
android:alpha="20"
android:layout_alignRight="@+id/hiaTextField"
android:layout_alignEnd="@+id/hiaTextField"
android:background="@drawable/buttonround"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="16dp"
android:clickable="true"
android:src="@android:drawable/ic_menu_myplaces" />
</RelativeLayout>
Upvotes: 0
Views: 2702
Reputation: 192023
General template for getting the FAB at the bottom right is to include
your layout inside of a CoordinatorLayout
, then have the button to aligned to the buttom-right of that.
I think your problem is that the layout_gravity
attribute doesn't apply to RelativeLayout
children.
You could do this, but that isn't really the documented way it should be done.
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
What you should have is like so, of course, replacing that include line with the name of your layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.androidstack.app.NavDrawerActivity2">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/your_layout"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_menu_myplaces"/>
</android.support.design.widget.CoordinatorLayout>
Upvotes: 1
Reputation: 1169
Try to add this attribute for floating action button
app:layout_anchor="@id/layout" //id of the relativelayout
app:layout_anchorGravity="bottom|right|end"
Upvotes: 1