Skip to content
Advertisement

my intent in navigation drawer is not working

When I tap on any item of in navigation drawer ,instead of going to new activity , the drawer toggles.

MainActivity :

  public class MainActivity extends AppCompatActivity {

    LinearLayoutManager mLayoutManager; //for sorting
    SharedPreferences mSharedPreferences; //for saving sort setting

    DrawerLayout drawerLayout;
    ActionBarDrawerToggle toggle;
    NavigationView navigationView;
    Toolbar toolbar;
    RecyclerView mRecyclerView;
    List<FoodData> myFoodList;
    FoodData mFoodData;
    MyAdapter myAdapter;
    SearchView searchView;
    private boolean ascending = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // setUpToolbar();
       // getSupportActionBar().hide();

        toolbar =(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //menu hooks
        drawerLayout = findViewById(R.id.drawerLayout);
        navigationView = findViewById(R.id.navigation_menu);

        toggle = new ActionBarDrawerToggle(this, drawerLayout,toolbar,R.string.open,R.string.close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();






        mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        searchView = findViewById(R.id.srch);

        mSharedPreferences= getSharedPreferences("SortSettings", MODE_PRIVATE);
        String mSorting =mSharedPreferences.getString("Sort","newest"); // where if no setting is selected newest will be default

        if(mSorting.equals("newest")){
            mLayoutManager = new LinearLayoutManager(this);
            //this will load the items from bottom means newest first
            mLayoutManager.setReverseLayout(true);
            mLayoutManager.setStackFromEnd(true);

        }else if(mSorting.equals("oldest")){
            mLayoutManager = new LinearLayoutManager(this);
            //this will load the items from bottom means oldest first
            mLayoutManager.setReverseLayout(false);
            mLayoutManager.setStackFromEnd(false);

        }

        //for setting company name in the navigation drawer
      //  navigationView=(NavigationView)findViewById(R.id.navigation_menu);
        //navigationView.setNavigationItemSelectedListener(this);
       // View header = navigationView.getHeaderView(0);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId())
                {
                    case  R.id.nav_favourite:
                        Intent intent = new Intent(MainActivity.this, favourite.class);
                        startActivity(intent);
                       // drawerLayout.closeDrawer(GravityCompat.START);
                        break;

                    case R.id.nav_about_us:
                        Intent intent1 = new Intent(MainActivity.this , About_us.class);

                        startActivity(intent1);
                       // drawerLayout.closeDrawer(GravityCompat.START);
                        break;
                    case R.id.nav_rate_us:
                        Intent intent2 = new Intent(MainActivity.this , RateUs.class);

                        startActivity(intent2);
                       // drawerLayout.closeDrawer(GravityCompat.START);
                        break;
                    case  R.id.nav_share:

                        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                        sharingIntent.setType("text/plain");
                        String shareBody =  "http://play.google.com/store/apps/detail?id=" + getPackageName();
                        String shareSub = "Try now";
                        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
                        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                        startActivity(Intent.createChooser(sharingIntent, "Share using"));

                    //drawerLayout.closeDrawer(GravityCompat.START);
                        break;
                    case R.id.nav_setting:
                        Intent intent3 = new Intent(MainActivity.this , SettingsActivity.class);

                        startActivity(intent3);
                       // drawerLayout.closeDrawer(GravityCompat.START);
                        break;

                }
                return true;
            }
        });



        GridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this,1);
        mRecyclerView.setLayoutManager(mLayoutManager);

        myFoodList = new ArrayList<>();

        mFoodData = new FoodData("Rawa Kesar",R.drawable.rawa,"INGREDIENTS"); 
        mFoodData = new FoodData("Jalebi",R.drawable.jalebi,"INGREDIENTS"); 
        mFoodData = new FoodData("Rasmalai",R.drawable.rasmlai,"INGREDIENTS"); 
        mFoodData = new FoodData("Kaju Katli",R.drawable.kajuktli,"INGREDIENTS"); 


         myAdapter = new MyAdapter(MainActivity.this,myFoodList);
        mRecyclerView.setAdapter(myAdapter);


        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                myAdapter.getFilter().filter(s.toString());
                return false;

            }
        });


    }


//    public void btn_uploadActivity(View view) {
//
//       startActivity(new Intent(this,Upload_Recipe));
//    }

//    public void setUpToolbar() {
//        drawerLayout = findViewById(R.id.drawerLayout);
//        Toolbar toolbar = findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
//        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
//        drawerLayout.addDrawerListener(actionBarDrawerToggle);
//        actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.black));
//        actionBarDrawerToggle.syncState();
//
//    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id=item.getItemId();

        if(id==R.id.sort){
            //display alert dialogue to choose sorting
            showSortDialogue();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showSortDialogue() {
        //options to display in dialogue
        String[] sortOptions = {"Newest","Oldest"};
        //create alert dislogue
        AlertDialog.Builder builder= new AlertDialog.Builder(this);
        builder.setTitle("Sort By").setIcon(R.drawable.sort_2).setItems(sortOptions,new  DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(which==0){
                            SharedPreferences.Editor editor=mSharedPreferences.edit();
                            editor.putString("Sort","newest");
                            editor.apply();
                            recreate();
                        }else if(which==1){
                            SharedPreferences.Editor editor=mSharedPreferences.edit();
                            editor.putString("Sort","oldest");
                            editor.apply();
                            recreate();
                        }
                    }
                }
        );
        builder.show();
    }


//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu,menu);
//        MenuItem menuItem=menu.findItem(R.id.search);
//        //SearchView searchView=(SearchView) menuItem.getActionView();
//        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
//            @Override
//            public boolean onQueryTextSubmit(String s) {
//                return false;
//            }
//
//            @Override
//            public boolean onQueryTextChange(String s) {
//                myAdapter.getFilter().filter(s.toString());
//                return false;
//            }
//        });
//        return super.onCreateOptionsMenu(menu);
//    }

    @Override
    public void onBackPressed() {

        Intent a =new Intent(Intent.ACTION_MAIN);
        a.addCategory(Intent.CATEGORY_HOME);
        a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(a);
    }

}

this is my mainActiviy.xml code where i have usedNavigation View

I have checked my code many times but not able to find the mistake…it would be great help if someone helps me in this Thanks in advance.

Advertisement

Answer

add navigation view below linear layout, it will work. In drawer layout , content should be first then navigation view.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement