Question

This is an example of facebook sliding menu.

When sliding, there is 20% space user can see which is similar with facebook. Facebook implements it with clicking anywhere of this 20% and the menu is slide back.

How to implement this?

Was it helpful?

Solution

One way of doing that is as following with OnTouchListener on your activity. You can detect exactly where is touched on the screen.

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.Toast;

public class AndroidTestActivity extends Activity implements OnTouchListener {
  LinearLayout main;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    main = (LinearLayout) findViewById(R.id.main_layout);
    main.setOnTouchListener(this); // you need to set the touch listener for your view. And every element around the detection area.
  }

  public boolean onTouch(View v, MotionEvent e) {
    if(e.getX() <= main.getWidth() / 5) {
      Toast.makeText(this, "In the %20..", Toast.LENGTH_SHORT).show();
      return true;      
    }

    return false;
  }
}

You also need to id your main layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top