androidandroid-layoutandroid-listviewnavigation-drawerslidingpanelayout

How to close navigation drawer through touch on the Screen in android


I have success fully open 4 different navigation drawer from left side when I click 4 different background button. Now when I click 1st button the 1st navigation drawer opening and closing as well. But problem is that when I click 2nd button drawer is opening but not closing onclick of list item and touch outside of the drawer. See below image want. this is image 1st this is another one

see code  public class MainActivity extends Activity {
    private ListView mDrawerList;
    private CharSequence mDrawerTitle;
    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;
    private CharSequence mTitle;
    // slide menu items
        private  String[] navMenuTitles;
        private  TypedArray navMenuIcons;
        private  RelativeLayout leftRL;
        private  RelativeLayout rightRL;
        private  RelativeLayout byleft;
        private  RelativeLayout toleft;
        private  DrawerLayout drawerLayout;
        final String[] data ={"one","two","three"};    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//      I'm removing the ActionBar.
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);

        mTitle = mDrawerTitle = getTitle();
     navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);
    final ListView navList = (ListView) findViewById(R.id.drawer);

    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

    navDrawerItems = new ArrayList<NavDrawerItem>();

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
    // Communities, Will add a counter here
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
    // Recycle the typed array
    navMenuIcons.recycle();


    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);

    leftRL = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
    rightRL = (RelativeLayout)findViewById(R.id.YouWantInRightDrawer);
    byleft = (RelativeLayout)findViewById(R.id.WantInRightDrawer);
    toleft = (RelativeLayout)findViewById(R.id.InRightDrawer);
    drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); 
    }

    public  void onLeft(View view)
    {
    drawerLayout.openDrawer(leftRL);
    }

    public  void ToLeft(View view)
    {
    drawerLayout.openDrawer(rightRL);
    drawerLayout.setFocusableInTouchMode(true);
    }

    public  void byLeft(View view)
    {
    drawerLayout.openDrawer(byleft);
    }
    public  void throughLeft(View view)
    {
    drawerLayout.openDrawer(toleft);
    }


}


<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="100dp" 
        android:background="#0F0FF0"
        >

        <Button
            android:id="@+id/home_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:background="@drawable/ic_home"
            android:onClick="onLeft" />

        <Button
            android:id="@+id/profile_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/home_btn"
            android:background="@drawable/ic_people"
            android:onClick="ToLeft"
            android:clickable="true"
             />

         <Button
             android:id="@+id/chat_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/profile_btn"
            android:background="@drawable/ic_photos"
            android:onClick="byLeft"
            android:clickable="true"
             />

         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/chat_btn"
            android:background="@drawable/ic_whats_hot"
            android:onClick="throughLeft"
            android:clickable="true"
             />

    </RelativeLayout>

    <!-- Left Drawrer -->

    <RelativeLayout
        android:id="@+id/YouWantInRightDrawer"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left" >

        <ListView
            android:id="@+id/firsr_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_green_light" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/WantInRightDrawer"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left" >

        <ListView
            android:id="@+id/second_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light" />

    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/InRightDrawer"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left" >

        <ListView
              android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_green_dark" />

    </RelativeLayout>
       <RelativeLayout
        android:id="@+id/whatYouWantInLeftDrawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start">
        <ListView
            android:id="@+id/list_slidermenu"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/list_background"
            android:choiceMode="singleChoice"
            android:divider="@color/list_divider"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/list_selector" />

    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

Solution

  • Hi friends i got the solution see code below

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff0000" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
    
            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Open Drawer1" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Open Drawer2" />
    
            <Button
                android:id="@+id/button3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Open Drawer3" />
    
            <Button
                android:id="@+id/button4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Open Drawer4" />
        </LinearLayout>
    
        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white"
            android:cacheColorHint="#00000000"
            android:choiceMode="singleChoice"
            android:divider="@android:color/black" >
        </ListView>
    
    </android.support.v4.widget.DrawerLayout>
    

    Java:

    public class MainActivity extends Activity {
    
        private Button btn1, btn2, btn3, btn4;
        private DrawerLayout mDrawerLayout;
        private ListView mDrawerList;
    
        String[] string = { "one", "two", "three", "four", "five" };
        String[] string2 = { "one", "two", "three" };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_drawer_layout);
    
            btn1 = (Button) findViewById(R.id.button1);
            btn2 = (Button) findViewById(R.id.button2);
            btn3 = (Button) findViewById(R.id.button3);
            btn4 = (Button) findViewById(R.id.button4);
            mDrawerList = (ListView) findViewById(R.id.left_drawer);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    
            btn1.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                            MainActivity.this, android.R.layout.simple_list_item_1,
                            string);
    
                    if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                        mDrawerLayout.closeDrawer(mDrawerList);
                        mDrawerList.setVisibility(View.INVISIBLE);
    
                    } else {
                        mDrawerList.setAdapter(arrayAdapter);
                        mDrawerLayout.openDrawer(mDrawerList);
                        mDrawerList.invalidate();
                    }
                }
            });
    
    
    
            btn2.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(
                            MainActivity.this, android.R.layout.simple_list_item_1,
                            string2);
    
                    if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                        mDrawerLayout.closeDrawer(mDrawerList);
                        mDrawerList.setVisibility(View.INVISIBLE);
    
                    } else {
                        mDrawerList.setAdapter(arrayAdapter1);
                        mDrawerLayout.openDrawer(mDrawerList);
                        mDrawerList.invalidate();
                    }
                }
            });
    
        }
    
    }
    

    Happy coding!!!!