0

In my app I have a FloatingActionButton in a ListFragment.

Here is the layout of the ListFragment:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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">

    <include layout="@android:layout/list_content"/>

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/no_contacts"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:src="@drawable/ic_add"
        app:layout_anchor="@id/android:list"
        app:layout_anchorGravity="bottom|end"
        android:layout_alignParentEnd="true"/>
</android.support.design.widget.CoordinatorLayout>

In Android Studio the FAB is at the bottom-right position but when I run the app it's at the top-left.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98

3 Answers3

0

Try using this

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_add"
app:layout_anchor="@id/android:list"
app:layout_anchorGravity="bottom|end"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>

Anurag
  • 1,162
  • 6
  • 20
0

Add the listview and textview in the layout that you add to this layout using the include tag.

Arjun Issar
  • 672
  • 4
  • 13
  • 32
0

Here's my working layout.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@android:layout/list_content" />

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="no contacts" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:src="@android:drawable/ic_input_add"
        app:layout_anchor="@id/android:list"
        app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>

And showing the layout in device pretty well.

enter image description here

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • In my app the ListFragment is in a ViewPager (Tabs) and this is what I get:[link](http://i.imgur.com/F4hskOe.png) – Olivier Stihl Oct 03 '16 at 22:00
  • By searching a little bit more if found that the fab must be put in the main activity layout (according to material design's guidelines [here](https://material.google.com/components/buttons-floating-action-button.html#buttons-floating-action-button-behavior) and how to do it [here](http://stackoverflow.com/questions/31596640/fab-animation-with-viewpager-tabslider/31663686#31663686) – Olivier Stihl Oct 07 '16 at 15:26