KaliMa KaliMa - 1 month ago 8
Android Question

FloatingActionButton strange press behavior

I have no idea what suddenly caused this, as I don't think I've changed any code (maybe this is an Android Studio 2.0 bug?).

But now when I press a FloatingActionButton, it stays pressed and doesn't do anything when I first click it. If I start clicking it again, it seems to work (but still looks pressed).

In my OnCreate method:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
assert fab != null;

fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//does stuff here, behavior is the same even if I remove this code
}
});


XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">



<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<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"
android:src="@android:drawable/ic_input_add"
android:baselineAlignBottom="false"
android:focusableInTouchMode="true"
/>

</android.support.design.widget.CoordinatorLayout>

</LinearLayout>

Answer

Remove android:focusableInTouchMode="true" from your FAB .

<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"
    android:src="@android:drawable/ic_input_add"
    android:baselineAlignBottom="false"
    />