Pages

Thursday 20 November 2014

SlidingMenu in android

To create a sliding Menu in android:

I have used two library to create this project.
Please reference below link before going to implement your project.

https://github.com/jfeinstein10/SlidingMenu
https://github.com/umano/AndroidSlidingUpPanel

For complete code

MainActivity.java

public class MainActivity extends SlidingFragmentActivity {

private SlidingUpPanelLayout mLayout;
TextView first, second,firstLeft,secondLeft,firstRight,secondRight;

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

// Declaring Left and right SlidingMenu
final SlidingMenu slidingMenu = getSlidingMenu();
slidingMenu.setSecondaryMenu(R.layout.right);
slidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
int height = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 100, getResources()
.getDisplayMetrics());
slidingMenu.setBehindOffset(height);

// For perfect SlidingUpLayout
mLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
mLayout.setOverlayed(true);

// You can write your own code here.
first = (TextView) findViewById(R.id.first_tv);
second = (TextView) findViewById(R.id.second_tv);
firstLeft= (TextView) findViewById(R.id.first_tv_left);
secondLeft= (TextView) findViewById(R.id.second_tv_left);
firstRight= (TextView) findViewById(R.id.first_tv_right);
secondRight= (TextView) findViewById(R.id.second_tv_right);

first.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "first",
Toast.LENGTH_LONG).show();
}
});

second.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "second",
Toast.LENGTH_LONG).show();
}
});

firstLeft.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "firstLeft",
Toast.LENGTH_LONG).show();
}
});

secondLeft.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "secondLeft",
Toast.LENGTH_LONG).show();
}
});

firstRight.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "firstRight",
Toast.LENGTH_LONG).show();
}
});

secondRight.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "secondRight",
Toast.LENGTH_LONG).show();
}
});
}
}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <com.sothree.slidinguppanel.SlidingUpPanelLayout
        xmlns:sothree="http://schemas.android.com/apk/res-auto"
        android:id="@+id/sliding_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="top"
        sothree:dragView="@+id/dragView"
        sothree:panelHeight="25dp"
        sothree:shadowHeight="0dp" >

        <!-- MAIN CONTENT -->

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent" >

            <FrameLayout
                android:id="@+id/transaction"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </RelativeLayout>
        <!-- SLIDING LAYOUT -->

        <RelativeLayout
            android:id="@+id/dragView"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:clickable="true"
            android:focusable="false"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/first"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#ffffff" >

                <TextView
                    android:id="@+id/first_tv"
                    android:layout_width="fill_parent"
                    android:layout_height="30dp"
                    android:layout_marginLeft="60dp"
                    android:layout_marginTop="15dp"
                    android:gravity="center_vertical"
                    android:text="First"
                    android:textColor="#128C79"
                    android:textSize="20sp"
                    android:textStyle="normal" />
            </RelativeLayout>

            <View
                android:id="@+id/line"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@+id/first"
                android:background="#128C79" />

            <RelativeLayout
                android:id="@+id/second"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/line"
                android:background="#ffffff" >

                <TextView
                    android:id="@+id/second_tv"
                    android:layout_width="fill_parent"
                    android:layout_height="30dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="60dp"
                    android:layout_marginTop="15dp"
                    android:gravity="center_vertical"
                    android:text="Second"
                    android:textColor="#128C79"
                    android:textSize="20sp"
                    android:textStyle="normal" />
            </RelativeLayout>

            <View
                android:id="@+id/line1"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@+id/second"
                android:background="#128C79" />

            <RelativeLayout
                android:id="@+id/menu_action"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/line1" >

                <ImageView
                    android:id="@+id/down"
                    android:layout_width="match_parent"
                    android:layout_height="66dp"
                    android:layout_centerHorizontal="true"
                    android:layout_centerInParent="true"
                    android:alpha="0.9"
                    android:src="@drawable/down_menu"
                    android:visibility="visible" />
            </RelativeLayout>
        </RelativeLayout>
    </com.sothree.slidinguppanel.SlidingUpPanelLayout>

</RelativeLayout>

Left.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <RelativeLayout
                android:id="@+id/first"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#ffffff" >

                <TextView
                    android:id="@+id/first_tv_left"
                    android:layout_width="fill_parent"
                    android:layout_height="30dp"
                    android:layout_marginLeft="60dp"
                    android:layout_marginTop="15dp"
                    android:gravity="center_vertical"
                    android:text="First"
                    android:textColor="#128C79"
                    android:textSize="20sp"
                    android:textStyle="normal" />
            </RelativeLayout>

            <View
                android:id="@+id/line"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@+id/first"
                android:background="#128C79" />

            <RelativeLayout
                android:id="@+id/second"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/line"
                android:background="#ffffff" >

                <TextView
                    android:id="@+id/second_tv_left"
                    android:layout_width="fill_parent"
                    android:layout_height="30dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="60dp"
                    android:layout_marginTop="15dp"
                    android:gravity="center_vertical"
                    android:text="Second"
                    android:textColor="#128C79"
                    android:textSize="20sp"
                    android:textStyle="normal" />
            </RelativeLayout>

            <View
                android:id="@+id/line1"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@+id/second"
                android:background="#128C79" />


</RelativeLayout>

Right.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <RelativeLayout
                android:id="@+id/first"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#ffffff" >

                <TextView
                    android:id="@+id/first_tv_right"
                    android:layout_width="fill_parent"
                    android:layout_height="30dp"
                    android:layout_marginLeft="60dp"
                    android:layout_marginTop="15dp"
                    android:gravity="center_vertical"
                    android:text="First"
                    android:textColor="#128C79"
                    android:textSize="20sp"
                    android:textStyle="normal" />
            </RelativeLayout>

            <View
                android:id="@+id/line"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@+id/first"
                android:background="#128C79" />

            <RelativeLayout
                android:id="@+id/second"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/line"
                android:background="#ffffff" >

                <TextView
                    android:id="@+id/second_tv_right"
                    android:layout_width="fill_parent"
                    android:layout_height="30dp"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="60dp"
                    android:layout_marginTop="15dp"
                    android:gravity="center_vertical"
                    android:text="Second"
                    android:textColor="#128C79"
                    android:textSize="20sp"
                    android:textStyle="normal" />
            </RelativeLayout>

            <View
                android:id="@+id/line1"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_below="@+id/second"
                android:background="#128C79" />

</RelativeLayout>

Project Layout: