How do I display the Floatingactionbutton?

Asked

Viewed 145 times

2

Could you help me? I’m trying to make the Floatingactionbutton appear on my screen but it doesn’t want to appear I don’t know what I do anymore...I’m a long time trying to solve this problem but I can’t solve it :(

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_container"
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:background="#FFFFFF"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="br.com.gruporecursos.noamobile.AlertaFragment"
android:orientation="vertical">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lstAlerta"
    android:dividerHeight="2dp"
    android:divider="#ECECEC"/>

    <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="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_menu_edit"
        />



    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
  • 1

    Instead of app:srcCompat="@android:drawable/ic_menu_edit" use android:src="@android:drawable/ic_menu_edit". Let me know if that was the problem.

  • Since you are using Floatingactionbutton inside a Linearlayout and have a Listview above it, you must assign android:layout_weight="1" Listview so that this does not "push" the Floatingactionbutton off the screen. You can use a Coordinatorlayout as Ack Lay explains in your reply, however, in a simple layout like this, the android:layout_weight="1" solves the problem.

1 answer

2


Use the class in your layout CoordinatorLayout(public class), so that you can use anchorage "bottom|end" in his FloatingActionButton.

The CoordinatorLayout provides an additional layer of control over the touch events between your Views daughters. This is used by many components of the support library.

The CoordinatorLayout also provides its Views the attributes layout_anchor and layout_anchorGravity, that can be used to put some view "floating" relative to another. For example the FloatingActionButton anchored in the lower right corner. See how it should look:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <ListView
            android:id="@+id/lstAlerta"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="#ECECEC"
            android:dividerHeight="2dp" />

    </android.support.v4.widget.SwipeRefreshLayout>

    <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="20dp"
        android:src="@android:drawable/ic_menu_edit" />
</android.support.design.widget.CoordinatorLayout>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.