How to Open Navigation Drawer with No Actionbar, Open with Just a Button

How to open Navigation Drawer with no actionbar, open with just a button

It's giving you a null pointer because you are trying to find the drawer layout in the fragment's view, when it is actually in the activities view.

A quick hack to do what you want is to find the view like:

getActivity().findViewById(R.id.drawer_layout)

That should work. A better way is to have a method on the activity for opening the drawer

public void openDrawer(){
mDrawerLayout.openDrawer(mDrawerLayout);
}

In the activity onCreate run your findViewById:

mDrawerLayout = (DrawerLayout) getView().findViewById(R.id.drawer_layout);

mDrawerLayout should be a member variable of your activity.

Then in your fragment you can call:

//cast activity to MyActivity so compiler doesn't complain
((MyActivity)getActivity()).openDrawer();

An even better way to do it is to create a listener in the fragment and set the activity as a listener to the fragment. Then you can call a method on the activity, similar to above. I'll let you do some research on how to do that.

Android: How to disable the ActionBar button that opens navigation drawer

Try This i think it Will help you

actionBar.setDisplayHomeAsUpEnabled(false);

or you can use this

getActionBar().setDisplayHomeAsUpEnabled(false);

Android ActionBar Up button won't open navigation drawer

Take a look at my codes first:

public class HomeActivity extends ActionBarActivity implements
DrawerCloseListener {
private Toolbar toolbar;
private DrawerLayout drawer;
private ActionBarDrawerToggle drawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.home_toolbar);
toolbar.setNavigationIcon(R.drawable.icon_nav);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.app_name, R.string.app_name);
drawerToggle.setHomeAsUpIndicator(R.drawable.icon_nav);
drawer.setDrawerListener(drawerToggle);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}

@Override
public void onBackPressed() {
// TODO Auto-generated method stub
if (drawer.isDrawerOpen(Gravity.LEFT | Gravity.START)) {
drawer.closeDrawers();
return;
}
super.onBackPressed();
}

@Override
public void onDrawerClose() {
// TODO Auto-generated method stub
if (drawer.isDrawerOpen(Gravity.LEFT | Gravity.START)) {
drawer.closeDrawers();
}
}
}

And among codes above, I replaced ActionBar by ToolBar, but you still can use ActionBar where there is a ToolBar. Did you miss something?

How to open Drawer Layout only with button?

Just write

drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

to prevent menu from listening to gesture

and use openDrawer and closeDrawer to change menu visibility



Related Topics



Leave a reply



Submit