0

So i have extended this DrawerActivity to other activities and I can see the Navigation bar in the extended activities but the on item click listener doesnt work. This is the drawer activity

public class DrawerActivity extends AppCompatActivity {


    public DrawerLayout mDrawarlayout;
    public ActionBarDrawerToggle mToggle;
    public NavigationView mNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);
        mDrawarlayout = findViewById(R.id.drawer_layout);
        mToggle = new ActionBarDrawerToggle(this, mDrawarlayout, R.string.open, R.string.close);
        mDrawarlayout.addDrawerListener(mToggle);
        mNavigationView = findViewById(R.id.navigationView);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setupDrawerContent(mNavigationView);


    }

    public void selectItemDrawer(MenuItem menuItem) {

        switch (menuItem.getItemId()) {
            case R.id.settings_drawer:
                Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_LONG).show();
                Intent anIntent = new Intent(getApplicationContext(), PatientSettingsActivity.class);
                startActivity(anIntent);
                //drawerLayout.closeDrawers();
                break;
            case R.id.logout:
                Toast.makeText(getApplicationContext(), "Logout Clicked", Toast.LENGTH_LONG).show();
                break;
            case R.id.chat:
                Toast.makeText(getApplicationContext(), "CHat Clicked", Toast.LENGTH_LONG).show();
                break;
            case R.id.history:
                Toast.makeText(getApplicationContext(), "Hisotry Clicked", Toast.LENGTH_LONG).show();
                break;
            case R.id.db:
                Toast.makeText(getApplicationContext(), "Dashboard Clicked", Toast.LENGTH_LONG).show();
                break;
        }
        menuItem.setChecked(true);
        setTitle(menuItem.getTitle());
        mDrawarlayout.closeDrawers();


    }

    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                selectItemDrawer(item);
                return true;
            }
        });
    }
}

This is the drawer activity xml

<android.support.v4.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    tools:context=".DrawerActivity">


    <android.support.design.widget.NavigationView
        app:headerLayout="@layout/header"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/white"
        app:itemTextColor="@color/primaryTextColor"
        app:menu= "@menu/drawermenu"
        android:id="@+id/navigationView"
        android:layout_gravity = "start">
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

This is how i have extended DrawerActivity

P.S this is what i followed Same Navigation Drawer in different Activities

public class MapActivity extends DrawerActivity{
........}

And i have noticed that the on item click listener works when i run the DrawerActivity only

Shayaan
  • 1
  • 1
  • can you post the Map Activity code, looking at the way you have inflated new layout would help. – adarsh Nov 27 '19 at 17:39
  • The map activity has alot of code but I used this `setContentView(R.layout.activity_map);` to inflate the layout. I can see the navigation drawer in this activity but the item clicks dont seem to work – Shayaan Nov 27 '19 at 17:51
  • If you're calling `setContentView()` again in `MapActivity`, after the `super.onCreate()` call, that's completely replacing the layout you set up in `DrawerActivity`. Given that, your description of the behavior and appearance doesn't really make sense, unless you have redundant ``s and/or ``s. Please [edit] your question to provide a [mcve]. – Mike M. Nov 27 '19 at 18:02
  • What should i do if I extend my Drawer Activity to multiple activites and each of these activites already have their own specific layout that I want to keep – Shayaan Nov 27 '19 at 19:16
  • You have to inflate the layouts yourself, and put them into the `DrawerLayout`'s content. – Mike M. Nov 27 '19 at 20:34

1 Answers1

0

I solved my problem by following this tutorial below

http://mateoj.com/2015/06/21/adding-toolbar-and-navigation-drawer-all-activities-android/

and the github link

https://github.com/j-mateo/MultiActivityToolbar

Shayaan
  • 1
  • 1