Jamie-505
Jamie-505

Reputation: 1260

Handle menuitem selection with speed dial floating action buttons

I'm currently developing my first Android app and therefore I'm using fab-speed-dial. I've implemented the floating action button so that the options suit my needs. But now I need to assign actions to the menu items.

E.g. I want to show an input field if one clicks on "add room" in the Screenshot below:

menu items

I've tried so add some listeners but obviously I'm doing something wrong. This is the code I'm currently using in my MainActivity.java

FabSpeedDial fabSpeedDial = (FabSpeedDial) findViewById(R.id.fab_speed_dial);
    fabSpeedDial.setMenuListener(new SimpleMenuListenerAdapter() {
        @Override
        public boolean onMenuItemSelected(MenuItem menuItem) {
            //what do I do here?
            return false;
        }
    });

My activity_main.xml looks like this:

<FrameLayout 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:padding="16dp">

    <io.github.yavski.fabspeeddial.FabSpeedDial
        android:id="@+id/fab_speed_dial"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:fabGravity="bottom_end"
        app:fabMenu="@menu/menu_speeddial"
        app:miniFabBackgroundTint="@android:color/white"
        app:miniFabDrawableTint="?attr/colorPrimaryDark"
        app:miniFabTitleTextColor="?attr/colorPrimaryDark" />

</FrameLayout>

And last but not least the menu entries in menu_speeddial.xml

<?xml version="1.0" encoding="utf-8"?>
  <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
      android:id="@+id/add_room"
      android:icon="@drawable/ic_action_home"
      android:title="@string/menu_item_room"
      android:onClick="gotClicked"/>
  <item
      android:id="@+id/add_furniture"
      android:icon="@drawable/ic_action_armchair"
      android:title="@string/menu_item_furniture"/>
 </menu>

Any help on how to deal with this would be highly appreciated.

Upvotes: 1

Views: 2334

Answers (1)

Bob
Bob

Reputation: 13850

I havent used this library. But seeing your code, I can guess that the issue would be in the MenuListener.

@Override
        public boolean onMenuItemSelected(MenuItem menuItem) {
            if (menuItem.getItemId()==1){
                System.out.print("number 1 clicked");
            }
            return false;
        }

I think, you have to check equals with the resource id. Like this,

if (menuItem.getItemId() == R.id.add_room){
                System.out.print("number 1 clicked");
            }

Upvotes: 1

Related Questions