Cannot open navigationbar - android

I click the FloatingActionButton to open my navigation menu, But not working
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawer.isDrawerOpen(GravityCompat.START);
}
});
No errors in my logcat.
Any one help.

Your isDrawerOpen method checks if drawer open or not , its not doing anything only checks. You can use this in onClick method:
if(drawer.isDrawerOpen(GravityCompat.START)) {
closeNavDrawer();
}else {
drawer.openDrawer(GravityCompat.START);
}
closeNavDrawer method:
protected void closeNavDrawer() {
if (drawer != null) {
drawer.closeDrawer(GravityCompat.START);
}
}

Related

Drawer with navigation controller back button

I have one activity and some fragments and try to use navigation controller. I choose menu item, fragment changed. But when I press back button only drawer opens.
Here is my code:
public class MainActivity extends AppCompatActivity implements HomeFragment.OnFragmentInteractionListener,
LibraryFragment.OnFragmentInteractionListener, UserProfileFragment.OnFragmentInteractionListener {
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private Toolbar toolbar;
public NavController navController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
drawerLayout = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
initDrawer();
View header = navigationView.getHeaderView(0);
View userPicture = header.findViewById(R.id.user_pic);
userPicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showUserProfile();
}
});
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
switch (item.getItemId()){
case android.R.id.home:
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (navController.popBackStack()){
} else {
drawer.openDrawer(GravityCompat.START);
}
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onFragmentInteraction(Uri uri) {
}
private void initDrawer(){
Toolbar toolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolBar);
ActionBar actionBar = getSupportActionBar();
NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout);
NavigationUI.setupWithNavController(navigationView, navController);
}
private void showUserProfile(){
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}
navController.navigate(R.id.userProfileFragment);
}
}
What do I do wrong?
Thanks in advance

default back button in navigation drawer doesn't work

I have created an app which uses one activity (Navigation Drawer) and a fragment. But I'm unable to use toolbar back button to navigate back from the fragment to the main activity. Hardware back button works perfectly. I know that I need to override onOptionsItemSelected, catch android.R.id.home, check if there is something in the back stack and then pop it. After changing the fragment, "burger" button changes to "back arrow", but when I click on it the overridden method "onOptionsItemSelected" is never called, rather on click of the back button NavigationDrawer menu opens.
NOTE: I have referred many answers in the StackOverflow including THIS which is the same as that of my problem but that did not work for me. I have been working on this for a week any help is greatly appreciated. Please do not down vote. 1
Here is my code
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
public void init() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
Log.d("Backstack Count", String.valueOf(getSupportFragmentManager().getBackStackEntryCount()));
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
showUpButton(false);
} else {
showUpButton(true);
}
}
});
}
private void showUpButton(boolean show) {
// To keep states of ActionBar and ActionBarDrawerToggle synchronized,
// when you enable on one, you disable on the other.
// And as you may notice, the order for this operation is disabled first, then enable - VERY VERY IMPORTANT.
if (show) {
// Remove hamburger
mDrawerToggle.setDrawerIndicatorEnabled(false);
// Show back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// when DrawerToggle is disabled i.e. setDrawerIndicatorEnabled(false), navigation icon
// clicks are disabled i.e. the UP button will not work.
// We need to add a listener, as in below, so DrawerToggle will forward
// click events to this listener.
} else {
// Show hamburger
mDrawerToggle.setDrawerIndicatorEnabled(true);
invalidateOptionsMenu();
}
// So, one may think "Hmm why not simplify to:
// .....
// getSupportActionBar().setDisplayHomeAsUpEnabled(enable);
// mDrawer.setDrawerIndicatorEnabled(!enable);
// ......
// To re-iterate, the order in which you enable and disable views IS important #dontSimplify.
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// super.onCreateOptionsMenu(menu);
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// onBackPressed();
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
}
return true;
}
// return false;
// return super.onOptionsItemSelected(item);
return true;
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
displayImportFrag();
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void displayImportFrag() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Log.d("called", "new frag");
ImportFragment importFragment = new ImportFragment();
ft.add(R.id.fragment_frame, importFragment).addToBackStack(null).commit();
}
}
ImportFragment.java
public class ImportFragment extends Fragment {
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.import_fragment, container, false);
return rootView;
}
}

OnBackPressed() quits application

My onBackPressed() sends me out of my app. How can I avoid this? Either it should send me back to last shown fragment or just send me to a specific fragment.
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
else{
super.onBackPressed();
}
}
Try this,If you are using Fragment android.support.v4.app.Fragment.
private boolean allowExit = false;
Then
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
else if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
// handling fragment backbutton navigation
getSupportFragmentManager().popBackStack();
} else {
if (allowExit) {
super.onBackPressed();
} else {
Toast.makeText(this, getString(R.string.app_exit_message), Toast.LENGTH_SHORT).show();
allowExit = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
allowExit = false;
}
}, 3000);
}
}
}
If you are using not support fragment you have to replace
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
else if (getFragmentManager().getBackStackEntryCount() > 0) {
// handling fragment backbutton navigation
getFragmentManager().popBackStack();
} else {
if (allowExit) {
super.onBackPressed();
} else {
Toast.makeText(this, getString(R.string.app_exit_message), Toast.LENGTH_SHORT).show();
allowExit = true;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
allowExit = false;
}
}, 3000);
}
}
}
And add message to the string.xml
<string name="app_exit_message">Press again to exit</string>
Try the below code.
1. If your drawer is opened it will close the drawer.
2. If Any fragment is in backstack, it will navigate you to the previous fragment.
3. If No Fragment is in backstack, it will close the application.
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
oneStepBack();
}
}
/**
* function to go back to previous fragment
*/
private void oneStepBack() {
FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
FragmentManager fragmentManager = getSupportFragmentManager();
showOutput("Back Stack Count>>>>>>>>>>" + fragmentManager.getBackStackEntryCount());
if (fragmentManager.getBackStackEntryCount() >= 2) {
fragmentManager.popBackStackImmediate();
fts.commit();
} else {
finish();
}
}
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
else{
write your code here what activity you want to open on back press and remove below line
//super.onBackPressed();
}
}
DrawerLayout should be initialised in onCreate() method ,so you are facing the problem.
For your reference check this , it may help you.
http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

How to open NavigationDrawer with Fab?

I want while click on FAB button it Open Navigation Drawer.
How to do?
You can take a look at this DrawerLayout
mDrawerLayout = (DrawerLayout) getView().findViewById(R.id.drawer_layout);
FloatingActionButton myFab = (FloatingActionButton) myView.findViewById(R.id.myFAB);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//if you need open the slide:
mDrawerLayout.openDrawer(Gravity.LEFT);
//if you need close the slide
mDrawerLayout.closeDrawer(Gravity.LEFT);
}
});
mDrawerLayout = (DrawerLayout) getView().findViewById(R.id.drawer_layout);
and add following line in Fab Onclick
mDrawerLayout.openDrawer(Gravity.LEFT);

Lock one of two fragments in DrawerLayout

I have two navigation fragments in the DrawerLayout. One navigation fragment in on the left and second is on the right.
In some cases I need to open left navigation fragment and lock it.
When I had only left navigation fragment I used drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
But when I have added second navigation fragment on the right, this method started to open and lock both panels.
How to open and lock only left navigation view in this case?
Change you code like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_layout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mLeftDrawerList = (ListView) findViewById(R.id.left_drawer);
mRightDrawerList = (ListView) findViewById(R.id.right_drawer);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close) {
public void onDrawerOpened(View view){
if(view.equals(mRightDrawerList)) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mLeftDrawerList);
} else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mRightDrawerList);
}
invalidateOptionsMenu();
}
public void onDrawerClosed(View view) {
if(view.equals(mRightDrawerList)) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, mLeftDrawerList);
} else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, mRightDrawerList);
}
invalidateOptionsMenu();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
This should work.

Categories

Resources