[Android] Cara Membuat Slide Menu di Android

Pertama bikin project dulu, misal namanya Slide

slide

siapkan gambar buat icon menunya, gambarnya terserah situ aja, dan copy gambar tersebut ke folder res->drawable-hdpi

lokasi gambar

lalu buat lah class Baru yang dikasih nama SlideAdapter.java isikan code seperti ini

 

package com.slide;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class SlideAdapter extends BaseAdapter {

	private Context context;

	// array of integers for images IDs
	private Integer[] gambarMenu = { R.drawable.index, R.drawable.about,
			R.drawable.help, };

	public SlideAdapter(Context c) {
		context = c;
	}

	@Override
	public int getCount() {
		return gambarMenu.length;
	}

	@Override
	public Object getItem(int i) {
		// TODO Auto-generated method stub
		return i;
	}

	@Override
	public long getItemId(int i) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		ImageView imageView = new ImageView(context);
		imageView.setImageResource(gambarMenu[i]);
		imageView.setLayoutParams(new Gallery.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT));
		return imageView;

	}

}

setelah itu edit activity_main.xml seperti ini

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SlidingMenu" >

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="50dip"
        android:paddingTop="80dp" />

    <LinearLayout
        android:id="@+id/image_count"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#00000000"
        android:gravity="center"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>

untuk MainActivity.java edit seperti ini

package com.slide;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	private static TextView DotsText[];
	private int DotsCount;
	private LinearLayout DotsLayout;
	public static int l;
	public static int idxMenu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final Gallery gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setAdapter(new SlideAdapter(this));
		DotsLayout = (LinearLayout) findViewById(R.id.image_count);

		DotsCount = gallery.getAdapter().getCount();

		DotsText = new TextView[DotsCount];

		for (int i = 0; i < DotsCount; i++) {
			DotsText[i] = new TextView(this);
			DotsText[i].setText(".");
			DotsText[i].setTextSize(45);
			DotsText[i].setTypeface(null, Typeface.BOLD);
			DotsText[i].setTextColor(android.graphics.Color.GRAY);
			DotsLayout.addView(DotsText[i]);

			gallery.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View v,
						int position, long id) {

					switch (position) {
					case 0:
						Intent intent_index = new Intent(MainActivity.this,
								Index.class);
						startActivity(intent_index);
						break;
					case 1:
						Intent intent_help = new Intent(MainActivity.this,
								Help.class);
						startActivity(intent_help);
						break;
					case 2:
						Intent intent_about = new Intent(MainActivity.this,
								About.class);
						startActivity(intent_about);
						break;
					}

				}
			});
			gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
				@SuppressWarnings("rawtypes")
				@Override
				public void onItemSelected(AdapterView adapterView, View view,
						int position, long l) {

					// status.setText(menu_name[position]);
					for (int i = 0; i < DotsCount; i++) {
						MainActivity.DotsText[i].setTextColor(Color.GRAY);
					}

					MainActivity.DotsText[position].setTextColor(Color.WHITE);

				}

				@SuppressWarnings("rawtypes")
				@Override
				public void onNothingSelected(AdapterView adapterView) {

				}
			});
		}

	}
}

seteleah itu buatlah 3 Aciivity Baru yanga di Kasih nama Index,Help,dan About, biarkan kondisi nya sesuai sesuai default,.

Kalo Berhasil Pasti Hasilnya gini

indexslide

Download Projectnya disini

One thought on “[Android] Cara Membuat Slide Menu di Android

Leave a Reply

Your email address will not be published. Required fields are marked *


4 + 3 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>