Umair Iqbal
Umair Iqbal

Reputation: 2069

drawer menu not doing anything on Click event

Iam Implementing navigation drawer in my application but its menu not working on click. I dont know whats wrong but on click event is not doing anything or toasting a message.Click anything from drawer menu dont do anything.Firstly other button were also not working but adding them in a seprate layout solved the issue.but drawer menu are still not working

Here is my xml :

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/drawer_menu" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <include layout="@layout/app_bar_main" />

    </LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>

Here in java Iam using it :

public class AuthorMainScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    DrawerLayout drawerLayout;
    NavigationView navigationView;
    private ActionBarDrawerToggle actionBarDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_author_navigation);

        viewDeclaration();
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (actionBarDrawerToggle.onOptionsItemSelected(item))
            return true;

        return super.onOptionsItemSelected(item);
    }


    private void viewDeclaration() {
        newSurveyBtn = findViewById(R.id.new_surveys_button);
        surveyWithRef = findViewById(R.id.get_survey_button);
        surveyResult = findViewById(R.id.analyze_survey);
        drawerLayout = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.navigation_view);

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.menu_share:
                System.out.println("ss" + "coming");
                Toast.makeText(getApplicationContext(), "Share", Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_survey_count:
                Toast.makeText(getApplicationContext(), "Surveys", Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_logout:
                Toast.makeText(getApplicationContext(), "logout", Toast.LENGTH_SHORT).show();
                break;

        }
        return true;
    }

Upvotes: 0

Views: 289

Answers (3)

Brahma Datta
Brahma Datta

Reputation: 1112

When you are using AndroidX try to use the methods related to it.

Add the host fragment when you want to implement the fragments in that particular activity:

xml:

<androidx.drawerlayout.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">


       <fragment
        android:id="@+id/host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navigation_graph"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />



<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include layout="@layout/app_bar_main" />

</LinearLayout>

  <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/drawer_menu" />

Now use the navigation graph where you need to implement the fragments you are about to use in the activity :

navigation_graph.xml:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navigation_graph"
app:startDestination="@id/profile">
  <fragment
    android:id="@+id/profile"
    android:name="com.example.jetpacknavigationexample.Fragments.ProfileFragment"
    android:label="@string/profile"
    tools:layout="@layout/fragment_profile" />
  <fragment
    android:id="@+id/features"
    android:name="com.example.jetpacknavigationexample.Fragments.FeaturesFragment"
    android:label="@string/features"
    tools:layout="@layout/fragment_features" />
  <fragment
    android:id="@+id/signout"
    android:name="com.example.jetpacknavigationexample.Fragments.ProfileFragment"
    android:label="@string/signout"
    tools:layout="@layout/fragment_signout" />
  </navigation>

In your java class add the implement the Navigation component code :

public class AuthorMainScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
NavigationView navigationView;
private ActionBarDrawerToggle actionBarDrawerToggle;

//Add this
 NavController mNavController;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_author_navigation);

   //add this
    mNavController = Navigation.findNavController(this,R.id.host_fragment);







 NavigationUI.setupActionBarWithNavController(this, mNavController, drawerLayout);

    NavigationUI.setupWithNavController(navigationView,mNavController);

    mNavigationView.setNavigationItemSelectedListener(this);

    viewDeclaration();
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (actionBarDrawerToggle.onOptionsItemSelected(item))
        return true;

    return super.onOptionsItemSelected(item);
}


private void viewDeclaration() {
    newSurveyBtn = findViewById(R.id.new_surveys_button);
    surveyWithRef = findViewById(R.id.get_survey_button);
    surveyResult = findViewById(R.id.analyze_survey);
    drawerLayout = findViewById(R.id.drawer_layout);
    navigationView = findViewById(R.id.navigation_view);

}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case R.id.menu_share:
            System.out.println("ss" + "coming");
            Toast.makeText(getApplicationContext(), "Share", 
         Toast.LENGTH_SHORT).show();
            break;
        case R.id.menu_survey_count:
            Toast.makeText(getApplicationContext(), "Surveys", 
              Toast.LENGTH_SHORT).show();
            break;
        case R.id.menu_logout:
            Toast.makeText(getApplicationContext(), "logout", 
                 Toast.LENGTH_SHORT).show();
            break;

    }
    return true;
}

You can test your app now. let me know after you implement this.

Upvotes: 0

Umair Iqbal
Umair Iqbal

Reputation: 2069

Solution for this is in the XML file bring navigation view below the include tag and it will work

Change this

 <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/drawer_menu" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include layout="@layout/app_bar_main" />

</LinearLayout>

To this

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <include layout="@layout/app_bar_main" />
    </LinearLayout>

<com.google.android.material.navigation.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/drawer_menu" />

Upvotes: 1

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15433

You don't initialize actionBarDrawerToggle. You have to do it.

Upvotes: 0

Related Questions