android – How to open navigation drawer on button click in main fragment?-ThrowExceptions

Exception or error:

I have made an app with one activity which uses a navigation drawer to open a number of different fragments. I have the actionbar drawertoggle, but it is not very visible.
If I place a button in the onCreateView in my main fragment(the fragment that appears when my app first starts up), how can I get it to open the navigation drawer controlled by my activity?


This seems to work. The answer is much simpler than I thought it would be.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View fragView = inflater.inflate(R.layout.mainmenu, container, false);
    button1 = (Button) fragView.findViewById(R.id.button1);         
    mDrawerLayout = (DrawerLayout)getActivity().findViewById(R.id.drawer_layout);
    mDrawerList = (ListView)getActivity().findViewById(R.id.left_drawer);
    button1.setOnClickListener(this);
    return fragView;
}


@Override
public void onClick(View v) {       

        mDrawerLayout.openDrawer(mDrawerList);          

    }

Thank you for your answers.

How to solve:

if you need open the slide:

mDrawerLayout.openDrawer(Gravity.LEFT); //Edit Gravity.START need API 14

if you need close the slide

mDrawerLayout.closeDrawer(Gravity.LEFT); //Edit Gravity.START need API 14

EXAMPLE

my mDrawerLayout is instanced here:

mDrawerLayout = (DrawerLayout)findViewById(R.id.my_drawer_layout);

my slide state:

mSlideState=false;

if you need to know the slide menu state (closed, opened). Use this code:

mDrawerLayout.setDrawerListener(new ActionBarDrawerToggle(this, 
    mDrawerLayout, 
    R.drawable.ic_menu_slide,
    0,
    0){
@Override
public void onDrawerClosed(View drawerView) {                       
    super.onDrawerClosed(drawerView);
    mSlideState=false;//is Closed
}
@Override
public void onDrawerOpened(View drawerView) {                       
    super.onDrawerOpened(drawerView);
    mSlideState=true;//is Opened
}});

finally. You can use your click event like this:

public void clickEventSlide(){
if(mSlideState){                
    mDrawerLayout.closeDrawer(Gravity.END);
}else{              
    mDrawerLayout.openDrawer(Gravity.END);
}}

In my case, my slide menu is at the right (Gravity.END), but if you need it on the left, try with Gravity.START

###

I have a much simpler solution using isDrawerOpen().

This automatically closes or opens the navigation drawer based on the drawer’s current state (Opened/Closed)

    Button hamMenu = findViewById(R.id.ham_menu);

    hamMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DrawerLayout navDrawer = findViewById(R.id.drawer_layout);
            // If the navigation drawer is not open then open it, if its already open then close it.
            if(!navDrawer.isDrawerOpen(Gravity.START)) navDrawer.openDrawer(Gravity.START);
            else navDrawer.closeDrawer(Gravity.END);
        }
    });

###

if you are using from default navigation activity in android you just have to
add this code in click listener of button —>

mDrawerLayout.openDrawer(Gravity.START);

for closing you do not have to do something.

###

To apply the toolbar as the app bar, first make sure your activity extends from AppCompatActivity. Then call setSupportActionBar() and pass the Toolbar object from your layout:

    toolbar=(Toolbar) findViewById(R.id.toolbar_main);
    setSupportActionBar(toolbar);
    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);

    drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.navigation_drawer_open,
            R.string.navigation_drawer_close);
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            drawerLayout.openDrawer(GravityCompat.START);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    if (drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    }
    else{
        super.onBackPressed();
    }
}

}

###

Use these lines to open and close the drawer on a certain event:

Code snippet for opening drawer:

drawerLayout.openDrawer(Gravity.START);

Code snippet for closing drawer:

drawerLayout.closeDrawer(Gravity.LEFT);

openDrawer(gravity_of_navigation_view_to_be_shown)

in openDrawer(“gravity”), in “gravity” section, you have to input the gravity of the Navigation View like given above:

Gravity.LEFT
Gravity.RIGHT
Gravity.START
Gravity.END

I think thats the best answer.

Leave a Reply

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