Skip to content

A custom layout for Android that reveals a menu lying underneath another view

Notifications You must be signed in to change notification settings

arendjr/SlidingMenuLayout

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

SlidingMenuLayout

A custom layout for Android that reveals a menu lying underneath another view.

Description

This project is aimed to provide a very basic implementation of a layout to implement sliding menu's like those found in the Facebook, Spotify or Firefox apps, for example.

Objectives

  • Simplicity - This solution only requires adding a single class to your own project, and can be used with a minimal amount of glue code.
  • Flexibility - This solution only provides the bare minimum to implement a sliding menu. You will have maximum flexibility to go from there (in fact, the bottom view that is revealed doesn't even really have to be a menu).
  • Compatibility - The solution works on old Android versions. Personally I make sure it works at least on Android 2.2. Also, I make sure this solution can be used as a drop-in solution with PhoneGap / Cordova projects.

Usage

First, copy the file SlidingMenuLayout.java into your project.

Second, use the SlidingMenuLayout as root view for your Activity. To accomplish this with the least amount of modification to existing code, you can override your Activity's setContentView() as follows:

    @Override
    public void setContentView(View view) {

        SlidingMenuLayout layout = new SlidingMenuLayout(this);
        layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT, 0.0F));

        layout.addView(new MenuView(this));

        layout.addView(view);

        super.setContentView(layout);
    }

In this example, MenuView is the view that will actually show the menu. It is up to you to implement this view.

Finally, you can add a button (typically in the top left corner of your main view), that calls openMenu() or closeMenu() on the layout as appropriate. This also is left as an exercise to the reader. You may also want to implement swipe movements for opening/closing the menu.

Update

I made the following modifications on the original code:

1- Fixed a bug that occurs if you frequently open and close the menu quickly. The fix was to cancel the timer, if exists, before scheduling a new one.

2- Add a toggleMenu() function

3- Convert all hard-coded values to constants

4- Adjust the menu width differently according to the phone orientation (see onMeasure).

5- Add support for right side menu (see onLayout)

About

A custom layout for Android that reveals a menu lying underneath another view

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages