bookmark_border

Hover: An Android Floating Menu Library

Mohammad Shaddad,

This is a walk through on using Hover; a floating menu implementation for Android. The library provides:

  • An easy-to-use, out-of-the-box floating menu implementation for Android developers
  • Common tools for Android developers to create their own floating menu

Important Note: Hover is still in beta. Expect some changes, potentially breaking ones, as the library undergoes development towards a final release.



Hover menu

Referencing Hover

Hover is available from jCenter and can be added to your Android Studio project by adding the following line to your build.gradle file:

compile 'io.mattcarroll.hover:hover:0.9.6'

Create a HoverMenuService

To get started with Hover, create a subclass of HoverMenuService to host your Hover menu. The only method that you're required to override is createHoverMenuAdapter() which essentially returns the content of your Hover menu.

  1. public class MyHoverMenuService extends HoverMenuService {
  2.  
  3. @Override
  4. protected HoverMenuAdapter createHoverMenuAdapter() {
  5. // Create and configure your content for the HoverMenu.
  6. return myHoverMenuAdapter;
  7. }
  8.  
  9. }



Implement a HoverMenuAdapter

A HoverMenuAdapter acts a lot like a standard Android Adapter. HoverMenuAdapters provide a View for each tab that appears in your Hover menu. It also provides the corresponding NavigatorContent for each tab.


  1. public class MyHoverMenuAdapter extends BaseHoverMenuAdapter {
  2.  
  3. private List<String> mTabs;
  4. private Map<String, NavigatorContent> mContentMap = new HashMap<>();
  5.  
  6. public MyHoverMenuAdapter() {
  7. mTabs = Arrays.asList("first", "second");
  8. mContentMap.put("first", /*...*/);
  9. mContentMap.put("second", /*...*/);
  10. }
  11.  
  12. @Override
  13. public void getTabCount() {
  14. return mTabs.size();
  15. }
  16.  
  17. @Override
  18. public long getTabId(int position) {
  19. return mTabs.get(position).hashCode();
  20. }
  21.  
  22. @Override
  23. public View getTabView(int position) {
  24. String tabName = mTabs.get(position);
  25. if ("first".equals(tabName)) {
  26. // Create and return the tab View for "first".
  27. } else if ("second".equals(tabName)) {
  28. // Create and return the tab View for "second".
  29. }
  30. // etc.
  31. }
  32.  
  33. @Override
  34. public NavigatorContent getNavigatorContent(int position) {
  35. String tabName = mTabs.get(position);
  36. return mContentMap.get(tabName);
  37. }
  38.  
  39. }

Working Directly With A HoverMenu

If you want to create your own Hover menu service from scratch, or if you want to experiment with a HoverMenu directly, you can instantiate one yourself. Use HoverMenuBuilder to configure a HoverMenu for your particular requirements.

  1. // Build a HoverMenu.
  2. HoverMenu hoverMenu = new HoverMenuBuilder(context)
  3. .displayWithinWindow()
  4. .useNavigator(myNavigator)
  5. .startAtLocation(savedLocationMemento)
  6. .useAdapter(adapter)
  7. .build();
  8.  
  9. // When you're ready for your HoverMenu to appear on screen.
  10. hoverMenu.show();
  11.  
  12. // When you want to remove your HoverMenu from the screen.
  13. hoverMenu.hide();
  14.  
  15. // When you want to force your HoverMenu to expand fullscreen.
  16. hoverMenu.expandMenu();
  17.  
  18. // When you want to force your HoverMenu to collapse to a draggable icon.
  19. hoverMenu.collapseMenu();
  20.  
  21. // When you want to change the tabs and content in your HoverMenu.
  22. hoverMenu.setAdapter(otherAdapter);
  23.  
  24. // When you want to save the display state of your HoverMenu.
  25. Parcelable displayState = hoverMenu.createDisplayStateMemento();
  26.  
  27. // When you want to be notified when your HoverMenu is added to/removed from the display.
  28. hoverMenu.addOnVisibilityChangeListener(listener);
  29.  
  30. // When you want to be notified when your HoverMenu expands or collapses.
  31. hoverMenu.addOnCollapseAndExpandListener(listener);



You can find a demo application with source code at the Hover Git repository on GitHub.

androidmaterial-design

What level is this content for?

We've all had our humble beginnings, it's our hard learned experiences that got us to master our code. We will all learn something sharing our knowledge, leave a legacy and write a post now.
create Write post

About the author


Mohammad Shaddad
Mohammad Shaddad

Mohammad is a technology consultant and entrepreneur with big passion for technology and living systems; aka software communities. He enjoys mentoring other programmers and entrepreneurs, as well as creating new things. Mohammad is the founder of @barmijly and @nafaqati. He currently spends his time between Amman.JO and Dubai.AE.