nav drawer design support

211 views Asked by At

navigation drawer not responding to clicks and snack is also not showing up. how to make the clicks responsive and add fragments for data loading. their are two items in the drawer and only one gets checked at the time of selection.

package com.example.jasaran.nav;

import com.example.jasaran.nav.R;
import android.content.Context;    
import android.content.SharedPreferences;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;


public class MainActivity extends AppCompatActivity implements 
NavigationView.OnNavigationItemSelectedListener {

private Toolbar mToolbar;
private DrawerLayout mDrawerLayout;
private NavigationView mNavigationView;
private FrameLayout mContentFrame;
private int mCurrentSelectedPosition;
private static final String PREFERENCES_FILE = "nav_settings";
private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
private boolean mUserLearnedDrawer;
int mselected;

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

    setContentView(R.layout.activity_main);

    setUpToolbar();

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

    mUserLearnedDrawer = Boolean.valueOf(readSharedSetting(this, PREF_USER_LEARNED_DRAWER, "false"));

    if (savedInstanceState != null) {
        mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
        boolean mFromSavedInstanceState = true;
    }

    setUpNavDrawer();

    mNavigationView = (NavigationView) findViewById(R.id.nav_view);
    mContentFrame = (FrameLayout) findViewById(R.id.nav_contentframe);

    mNavigationView.setNavigationItemSelectedListener(this);

    }


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION, 0);
    Menu menu = mNavigationView.getMenu();
    menu.getItem(mCurrentSelectedPosition).setChecked(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
    }
    return super.onOptionsItemSelected(item);
}

private void setUpToolbar() {
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    if (mToolbar != null) {
        setSupportActionBar(mToolbar);
    }
}

private void setUpNavDrawer() {
    if (mToolbar != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationIcon(R.drawable.ic_launcher);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(GravityCompat.START);
            }
        });
    }

    if (!mUserLearnedDrawer) {
        mDrawerLayout.openDrawer(GravityCompat.START);
        mUserLearnedDrawer = true;
        saveSharedSetting(this, PREF_USER_LEARNED_DRAWER, "true");
    }

}

public static void saveSharedSetting(Context ctx, String settingName, String settingValue) {
    SharedPreferences sharedPref = ctx.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(settingName, settingValue);
    editor.apply();
}

public static String readSharedSetting(Context ctx, String settingName, String defaultValue) {
    SharedPreferences sharedPref = ctx.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
    return sharedPref.getString(settingName, defaultValue);
}

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
    menuItem.setChecked(true);
   mselected = menuItem.getItemId();
    switch (menuItem.getItemId()) {

        case R.id.navigation_item_1:
            Snackbar.make(mContentFrame, "Item One", Snackbar.LENGTH_SHORT).show();
            mCurrentSelectedPosition = 0;
            return true;

        case R.id.navigation_item_2:
            Snackbar.make(mContentFrame, "Item Two", Snackbar.LENGTH_SHORT).show();
            mCurrentSelectedPosition = 1;
            return true;

        default:
            return true;
    }
}
}
1

There are 1 answers

1
Suleiman19 On

I see you've implemented the listener directly from the Activity. Try attaching it to the NavigationView like this:

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            // Handle menu clicks here

        }
    });

Check this Activity code on GitHub.