How can I close an android DrawerLayout when touched outside of it?
Asked Answered
W

1

5

I have created a DrawerLayout and it works fine. But I want it to close when the user touches the background. This can be implemented with a DrawerLayout with listView but here i'm using a NavigationView. So is there a way to accomplish this?

Here is the menu layout for the NavigationView

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/home" android:title="Home Parent" android:icon="@drawable/ic_home_black"/>
    <item android:id="@+id/send" android:title="Send" android:icon="@drawable/ic_send_black"/>
    <item android:id="@+id/add" android:title="Add" android:icon="@drawable/ic_add_black"/>
</menu>

Here is the java code

public class ParentActivity extends AppCompatActivity {

    private NavigationView mNavigationView;
    private User mCurrentUser;
    private UserLocalStore mUserLocalStore;
    private CircleImageView mProfilePic;
    private TextView mProfileName;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;

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

        mUserLocalStore = new UserLocalStore(this);
        mCurrentUser = mUserLocalStore.getUserDetails();

        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer);
        mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mNavigationView = (NavigationView)findViewById(R.id.navigationView);
        setNavigationViewMenu(mCurrentUser.userType);
        mProfileName = (TextView) mNavigationView.getHeaderView(0).findViewById(R.id.profileName);
        mProfileName.setText(mCurrentUser.getName());
        mProfilePic = (CircleImageView) mNavigationView.getHeaderView(0).findViewById(R.id.circleImageProfile);
        Picasso.with(this).load("https://www.sonypark360.net/wp-content/uploads/2017/08/profile-pictures.png").into(mProfilePic);

        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                mDrawerLayout.closeDrawers();
                return false;
            }
        });

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(mToggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



    private void setNavigationViewMenu(String userType) {
        switch (userType){
            case "s":
                mNavigationView.inflateMenu(R.menu.menu_student_navigation_drawer);
                break;
            case "pa":
                mNavigationView.inflateMenu(R.menu.menu_parent_navigation_drawer);
                break;
            case "pr":
                mNavigationView.inflateMenu(R.menu.menu_principal_navigation_drawer);
                break;
            case "t":
                mNavigationView.inflateMenu(R.menu.menu_teacher_navigation_drawer);
                break;
        }
    }
}

Here is the DrawerLayout code

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mlpj.www.morascorpions.ParentActivity">

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header_layout"

        >
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

I have also looked into this question, but it does not solve my problem

Westley answered 6/12, 2017 at 8:17 Comment(4)
DrawerLayout already does that, regardless of what you use for the drawer, if you have it setup correctly. If yours is not, then it's likely a problem with the layout.Carport
Mike is right if there is a drawer in your activity then its the default behavior. Check the drawer setup.Fader
Show your layout, problem may be in your layout where you define your drawerCabrera
Hi jacky, I have adde the layout code, can you please take a look at itWestley
O
13

Try this

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    Rect viewRect = new Rect();
    mNavigationView.getGlobalVisibleRect(viewRect);


    if (!viewRect.contains((int) ev.getRawX(), (int) ev.getRawY())) {

        //hide your navigation view here. 

    }
    return super.dispatchTouchEvent(ev);;
}
Ovaritis answered 6/12, 2017 at 9:24 Comment(1)
But still looking for that idea of Mike.M, ADM and Jacky of default behaviourWestley

© 2022 - 2024 — McMap. All rights reserved.