Skip to content

Instantly share code, notes, and snippets.

@joeyjiron06
Last active April 13, 2016 02:56
Show Gist options
  • Save joeyjiron06/6604690 to your computer and use it in GitHub Desktop.
Save joeyjiron06/6604690 to your computer and use it in GitHub Desktop.
Here is a very simple slideout menu that uses animations to show and hide a slide out menu. Slides from the left. Feel free to customize it, add to it, fork it, or whatever.
<!--activity_main.xml -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/showmenu"
android:text="Menu"
android:textColor="#ffffff"
android:background="#999999"
/>
<com.example.listexample.SlideoutMenu
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/slideout_menu"
android:layout_centerInParent="true"
android:background="#41DB00"
android:visibility="gone"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu"
android:textSize="120sp"
android:textColor="#ffffff"
android:layout_gravity="center_horizontal"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hidemenu"
android:text="hide menu"
android:layout_gravity="center_horizontal"
/>
</com.example.listexample.SlideoutMenu>
</RelativeLayout>
//MainActvity.java
//shows how to use SlideoutMenu
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity implements View.OnClickListener
{
private SlideoutMenu slideoutMenu;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get width and height of window
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
slideoutMenu = (SlideoutMenu) findViewById(R.id.slideout_menu);
slideoutMenu.height(metrics.heightPixels);
slideoutMenu.width(metrics.widthPixels);
((Button) findViewById(R.id.showmenu)).setOnClickListener(this);
((Button) findViewById(R.id.hidemenu)).setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View view)
{
switch (view.getId()) {
case R.id.showmenu:
slideoutMenu.toggle();
break;
case R.id.hidemenu:
slideoutMenu.toggle();
break;
}
}
}
//SlideoutMenu.java
//very simple slide out menu that slides from the left
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
public class SlideoutMenu extends LinearLayout implements Animation.AnimationListener
{
private boolean isOpen = false;
private int windowWidth = 0;
private int windowHeight = 0;
public SlideoutMenu(final Context ctxt, AttributeSet attrs)
{
super(ctxt, attrs);
setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
public void width(int w) { windowWidth = w;}
public void height(int w) { windowHeight = w;}
public int width() { return windowWidth;}
public int height() { return windowHeight;}
public void toggle()
{
TranslateAnimation anim;
isOpen = !isOpen;
if (isOpen) {
setVisibility(View.VISIBLE);
anim = new TranslateAnimation(width()*-1, 0.0f, 0.0f,0.0f);
}
else {
anim=new TranslateAnimation(0.0f, width()*-1, 0.0f, 0.0f);
anim.setAnimationListener(this);
}
anim.setDuration(300);
anim.setInterpolator(new AccelerateInterpolator(1.0f));
startAnimation(anim);
}
@Override
public void onAnimationStart(Animation animation)
{
setVisibility(View.GONE);
}
@Override
public void onAnimationEnd(Animation animation) { }
@Override
public void onAnimationRepeat(Animation animation) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment