smish_
smish_

Reputation: 35

Items on NavigationDrawer are not Clicking

I have used Android Studio's inbuilt Navigation Drawer Activity and only the main fragment seems to be working, The other items in the drawer are not clicking or working. Please help.

MainActivity.java

package com.example.duzol;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class MainActivity extends AppCompatActivity implements 
NavigationView.OnNavigationItemSelectedListener {

private AppBarConfiguration mAppBarConfiguration;

private static final int MAIN_FRAGMENT = 0;
private static final int TRACK_FRAGMENT = 1;

private FrameLayout frameLayout;
private NavigationView navigationView;
private DrawerLayout drawerLayout;

private static int currentFragment;

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

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);


    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    DrawerLayout drawer = findViewById(R.id.drawer_layout);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.nav_open_drawer, R.string.nav_close_drawer);
    drawer.addDrawerListener(toggle);
    toggle.syncState();


    navigationView = (NavigationView) findViewById(R.id.nav_view);
    if (navigationView!=null) {
        navigationView.setNavigationItemSelectedListener(this);
    }

    navigationView.getMenu().getItem(0).setChecked(true);

    frameLayout = (FrameLayout) findViewById(R.id.main_frameLayout);
    setFragment(new MainFragment(),MAIN_FRAGMENT);


}

@Override
public void onBackPressed() {
    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    if (currentFragment == MAIN_FRAGMENT){  // menu inflates only if user is in main fragment
        getMenuInflater().inflate(R.menu.main, menu);
    }
    return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_bag) {
        Toast.makeText(this, "Bag clicked", Toast.LENGTH_SHORT).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}


@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    item.setChecked(true);
    drawerLayout.closeDrawers();

    switch (id) {
        case R.id.nav_home:
            setFragment(new MainFragment(),MAIN_FRAGMENT);break;
        case R.id.nav_track:myTrack();break;
        case R.id.nav_appointment:
        case R.id.nav_feedback:
            Intent intent = new Intent(this, feedBackActivity.class);
            startActivity(intent);
            break;
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

private void myTrack(){
    setFragment(new TrackFragment(),TRACK_FRAGMENT);
    navigationView.getMenu().getItem(1).setChecked(true);
}


private void setFragment(Fragment fragment, int fragmentNum){
    currentFragment = fragmentNum;
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(frameLayout.getId(),fragment);
    ft.commit();
}
}

ActivityMain.xml I have used Navigation Drawer before but i used it from scratch (i.e. Empty Activity) but now after using the template the same stuff doesn't work. I don't know what I am doing wrong, I have checked it multiple times.

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</androidx.drawerlayout.widget.DrawerLayout>

Activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_baseline_notes_24"
        android:title="@string/menu_home" />
    <item
        android:id="@+id/nav_track"
        android:icon="@drawable/ic_baseline_pin_drop_24"
        android:title="@string/menu_track" />
    <item
        android:id="@+id/nav_appointment"
        android:icon="@drawable/ic_baseline_timelapse_24"
        android:title="@string/menu_appointment" />
</group>

<item android:title="Support">
    <group android:checkableBehavior="single">
        <item android:id="@+id/nav_feedback"
            android:icon="@drawable/ic_baseline_mail_24"
            android:title="@string/feedback"/>
    </group>
</item>
</menu>

Upvotes: 0

Views: 478

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363895

The issue is in your layout.

Change

<androidx.drawerlayout.widget.DrawerLayout>

   <com.google.android.material.navigation.NavigationView/>

   <include
     layout="@layout/app_bar_main"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />

</androidx.drawerlayout.widget.DrawerLayout>

to:

<androidx.drawerlayout.widget.DrawerLayout>

   <include
     layout="@layout/app_bar_main"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />

   <com.google.android.material.navigation.NavigationView/>

</androidx.drawerlayout.widget.DrawerLayout>

Upvotes: 1

Related Questions