1

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.

1 Answer 1

1

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");
            }
4
  • not sure if this is the cleanest solution, but it works for me ;)
    – Jamie-505
    May 6, 2016 at 16:00
  • if you have more items, you can use switch case instead of if-else. @Jamie-505
    – Bob
    May 6, 2016 at 16:18
  • I only use 2 items, so I think if and else if should be ok, right?
    – Jamie-505
    May 6, 2016 at 16:38
  • is it possible to change the text of the menuitem selected in this method onMenuItemSelected ? I don't succeed to do it :(
    – deveLost
    Sep 7, 2017 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.