Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking hamburger icon on Toolbar does not open Navigation Drawer

I have this nav drawer which was working perfectly fine. Refactoring my code I removed all onOptionsItemSelecteds in activities and made all activities inherit from a base activity which extends AppComplatActivity and implements all the necessary methods. After this clicking on hamburger icon does not work any more even though I have syncstate() and every thing.

Any clues why this is not working?

One of the activities:

public class MainActivity extends BaseActivity implements SearchFilterFragment.OnFragmentInteractionListener {

NavigationView navigationView;
DrawerLayout drawerLayout;

private Tracker mTracker;

@Override
protected void onResume() {
    super.onResume();
    drawerLayout.openDrawer(GravityCompat.START);
}

@Override
protected void onPostResume() {
    super.onPostResume();
    mTracker.setScreenName("MainActivity" + "-----");
    mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerLayout.openDrawer(GravityCompat.START);
    navigationView = (NavigationView) findViewById(R.id.navigation_view_primary);
    navigationView.setNavigationItemSelectedListener(new NavigationDrawerListener(this));
    setupToolbar();
    Haftdong application = (Haftdong) getApplication();
    mTracker = application.getDefaultTracker();
}

private void setupToolbar() {
    // Show menu icon
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);// will make the icon clickable and add the < at the left of the icon.
    DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();//for hamburger icon
}

@Override
public void onFragmentInteraction(Uri uri) {
}

}

BaseActivity:

public class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // 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.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

like image 834
Tina Avatar asked Apr 12 '16 10:04

Tina


People also ask

How do I open drawer menu?

It can be opened via swipe gesture or by clicking on the menu icon in the app bar. Typically, the navigation drawer opens up from the left side of the screen, but you can also configure it to open from the right side (for the LTR text settings).

Where is the hamburger menu on Android?

Like iOS's legacy back buttons, these hamburger menu buttons appear in the top left corners of their apps, but considering you don't need to use them very often, I'm not so worried about how easy they are to reach.

How do you display the badge on the navigation drawer menu icon?

Use BadgeDrawerArrowDrawable to provide a badge and/or count to the hamburger icon. To use BadgeDrawerArrowDrawable with DrawerLayout following code snippet can help. To display only Badge icon enable the badge and set the text to null.

What is action bar drawer toggle?

public class ActionBarDrawerToggle implements DrawerLayout.DrawerListener. This class provides a handy way to tie together the functionality of DrawerLayout and the framework ActionBar to implement the recommended design for navigation drawers.


1 Answers

You're using the four-parameter constructor for ActionBarDrawerToggle, which means you'll have to call the toggle's onOptionsItemSelected() method in MainActivity's onOptionsItemSelected() override in order to open/close the drawer.

For example:

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

    return super.onOptionsItemSelected(item);
}

If you happen to be providing your own Toolbar – e.g., as the support ActionBar (though it's not necessary to set it as such) – then you can instead pass that Toolbar as the third argument in the ActionBarDrawerToggle constructor call. For example:

Toolbar toolbar = findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
        toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

The drawer opening/closing will then be handled by ActionBarDrawerToggle internally, and you won't need to call through to the toggle in onOptionsItemSelected().

The setDisplayHomeAsUpEnabled() call is also unnecessary for this setup, which is handy if you don't want to set the Toolbar as the ActionBar.

like image 126
Mike M. Avatar answered Oct 26 '22 00:10

Mike M.