Sliding Drawer Example

Sliding Drawer in which we display a Sliding Drawer by using its different attributes. In this we also implement setOnDrawerOpenListener and setOnDrawerCloseListener events for changing the text of handle button.

actvity_main.xml

This xml shows display one Button for the handle of SlidingDrawer and a ListView for the content of the SlidingDrawer.






MainActivity.java

Add the code for initiate the ListView, SlidingDrawer and a Button. Firstly we create String Array and then Array Adapter is used to fill the data in the list.

package com.example.cowboysmediaexample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SlidingDrawer;

public class MainActivity extends AppCompatActivity {

    String[] stringArray = {"Aestro", "Blender", "Cupcake", "Donut", "Eclair", "Froyo", "GingerBread", "HoneyComb", "IceCream Sandwich", "JelliBean", "KitKat", "Lollipop", "MarshMallow"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SlidingDrawer slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer); 
        final Button handleButton = (Button) findViewById(R.id.open); 
        ListView simpleListView = (ListView) findViewById(R.id.listView); 

        ArrayAdapter arrayAdapter = new ArrayAdapter(getApplicationContext(), R.layout.list_item, R.id.name, stringArray);
        simpleListView.setAdapter(arrayAdapter);
        simpleSlidingDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
            @Override
            public void onDrawerOpened() {
                handleButton.setText("Close");
            }
        });
        
        simpleSlidingDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
            @Override
            public void onDrawerClosed() {
                handleButton.setText("Open");
            }
        });
    }
}