Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating Action Button does not responds when it clicked/touched

I am using a floating action button(FAB) in my application to show Dialogs, everything is worked just fine when I tested my app in Xperia Z with Lopllipop 5.1.1.
However, the problem is when tested my app in ASUS Zenfone 6 with KitKat 4.4.2 and in Xperia C with Jelly Bean 4.2.2, the FAB loaded perfectly but the FAB does not show the Dialogs, seems like it does not responds when I touched it.
For the record, my min sdk version is 16.

What i want to ask is why this is happening? Is there anything wrong with my code, or maybe with the android version or API level?

Please take a look at my code. Here the XML code:

    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="@drawable/ic_add_account"
    android:visibility="invisible"
    android:clickable="true"/>

and here how I declare and set the listener for the FAB:

FloatingActionButton fab;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

   fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);

   ...
   fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
            }
   });
   ...
}

The thing that made me confused is how it is worked in Xperia Z but it does not worked in ASUS Zenfone 6?

like image 529
Bullockta Bodax Avatar asked Mar 09 '16 09:03

Bullockta Bodax


People also ask

What is true about floating action buttons?

A floating action button (FAB) is a circular button that triggers the primary action in your app's UI. This page shows you how to add the FAB to your layout, customize some of its appearance, and respond to button taps.

How do I keep my floating action button from blocking other components?

Add a blank View with the same height as floating button in footer of list. So when you scroll your list, last item will come up and floating button will not hide last item of list. This is a simple and quick solution.

Can you have two floating action buttons?

When you only need a single floating button, using the FloatingActionButton widget is elegant and neat. If you need multiple floating buttons, using a Stack, Column, or Row widget is a good choice.


2 Answers

The problem in here is that I declared the FAB before the declaration of listView. Somehow by relocating the declaration of FAB after the the declaration of listView it will solved the problem.
From:

<android.support.design.widget.FloatingActionButton
    ...
    ...
    .../>
<ListView
    ...
    ...
    .../>

To:

<ListView
    ...
    ...
    .../>
<android.support.design.widget.FloatingActionButton
    ...
    ...
    .../>

Just as simple as that.

like image 125
Bullockta Bodax Avatar answered Oct 08 '22 18:10

Bullockta Bodax


Programmatically bringing the FAB to front should also work:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.bringToFront();
fab.setOnClickListener... //etc
like image 24
Maciej Beimcik Avatar answered Oct 08 '22 18:10

Maciej Beimcik