Tutorial details

Add a sliding menu with animation to an Activity | App Code for Sale | Preview

Add a sliding menu with animation to an Activity | Android Tutorial

How to Add a sliding menu

Overview PAGE TOP

I was playing around with animation, layers and so on and i have coded this simple sliding menu. The idea is very simple when user touches the screen and moves up his finger the app shows a menu, when user touches the screen and moves down his finger this menu disappear. As you can see the idea is very simple but it can be useful when we want to add some menu to our applications.


The first step is creating our sliding layer that will contain our menu. To keep things simple i will just show a TextView but you can modify the code to show your menu. Let’s suppose that activity_main.xml is the xml layout of our Activity, so we have:

01  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
02      xmlns:tools="http://schemas.android.com/tools"
03      android:layout_width="match_parent"
04      android:layout_height="match_parent"
05      android:background="@color/green">
07      <LinearLayout
08      android:id="@+id/slider"
09      android:layout_width="match_parent"
10      android:layout_height="60dp"
11      android:layout_alignParentBottom="true"
12      android:layout_alignParentLeft="false"
13      android:layout_alignParentTop="false"
14      android:layout_centerInParent="true"
15      android:background="@color/gray"
16      android:content="@+id/content"
17      android:gravity="bottom|center_horizontal"
18      android:orientation="vertical" >
20      <TextView android:id="@id/content"
21                android:layout_width="match_parent"
22                android:layout_height="wrap_content"
23                android:text="Surviving with android"/>
25     </LinearLayout>
26  </RelativeLayout>

This is the base layer that will be moved up and down following user finger direction. If you want to add a menu replace the TextView with your menu (maybe with Buttons and so on). Now let’s look at the Activity that has to manage this menu. At the start up this layer should be invisible so we have to add:

1   @Override
2    public void onCreate(Bundle savedInstanceState) {
3        super.onCreate(savedInstanceState);
4        setContentView(R.layout.activity_main);
6        ll = (LinearLayout) findViewById(R.id.slider);
7        ll.setVisibility(View.GONE);
8         ....
9   }

We simply set the layout visibility to View.GONE. Now we have to track the user finger movements to react to this movements. To do it we need simply override the method public boolean onTouchEvent(MotionEvent event) If we want to have more information on how to handle touchscreen events give a look here. To know if the user is moving his finger up or down we simply have to store the Y position when user touch the screen for the first time ( MotionEvent.ACTIONDOWN event) and compare its value to the Y position when user move up his finger ( MotionEvent.ACTIONUP event) . So we have:

01  @Override
02  public boolean onTouchEvent(MotionEvent event) {
03      switch (event.getAction()) {
04          case MotionEvent.ACTION_DOWN : {
05              startY = event.getY();
06              break ;           
07          }
08          case MotionEvent.ACTION_UP: {
09              float endY = event.getY();
11              if (endY < startY) {
12                  System.out.println("Move UP");
13                  ll.setVisibility(View.VISIBLE);
15              }
16              else {
17                  ll.setVisibility(View.GONE);
18              }
19          }
21      }
22      return true;
23  }

In this way we have created a simple ON/OFF menu that gets visible when user moves up his finger and gets invisibile when user moves down his finger. We could add some control for example a threshold that triggers the menu.

Add some animation

To make this menu more attractive and make the layer moving up or down we can add some animation. To do it under the directory res add a directory called anim (if not present). We want to create two animations one that shows the layout moving up and the other one moving it down. We create two files one called anim_up.xml with this content:

1   <?xml version="1.0" encoding="utf-8"?>
2   <set xmlns:android="http://schemas.android.com/apk/res/android">
3       <translate android:fromYDelta="100%"
4                   android:toYDelta="40%"
5                   android:duration="400"/>
7   </set>

and a another one called anim_down.xml :

1   <?xml version="1.0" encoding="utf-8"?>
2   <set xmlns:android="http://schemas.android.com/apk/res/android">
3       <translate android:fromYDelta="0%"
4                   android:toYDelta="100%"
5                   android:duration="400"/>
7   </set>

Now we have simply load this animation in our Activity and start them as user move finger up and down. So the code of our Activity looks like this:

01  private Animation animUp;
02  private Animation animDown;
04  @Override
05  public void onCreate(Bundle savedInstanceState) {
06      super.onCreate(savedInstanceState);
07      setContentView(R.layout.activity_main);
09      ll = (LinearLayout) findViewById(R.id.slider);
10      ll.setVisibility(View.GONE);
12      animUp = AnimationUtils.loadAnimation(this, R.anim.anim_up);
13      animDown = AnimationUtils.loadAnimation(this, R.anim.anim_down);
14  }
16  @Override
17  public boolean onTouchEvent(MotionEvent event) {
18      switch (event.getAction()) {
19      case MotionEvent.ACTION_DOWN : {
20          startY = event.getY();
21          break ;           
22      }
23      case MotionEvent.ACTION_UP: {
24          float endY = event.getY();
26          if (endY < startY) {
27              System.out.println("Move UP");
28              ll.setVisibility(View.VISIBLE);
29              ll.startAnimation(animUp);
30          }
31          else {
33              ll.startAnimation(animDown);
34              ll.setVisibility(View.GONE);
35          }
36      }
38      }
39      return true;
40  }
  • Source code for up-down menu here
  • Source code for left-right menu here

Reference PAGE TOP


0 Comments Leave a comment

Please login in order to leave a comment.

Newest first

to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.