0

I'm trying to make a navigation drawer that opens a new activity when the user taps an item.

There is 3 items in my drawer and I want each of them to open a different activity but when i click on item nothing happen.

 public class MarkerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_markers);
        getWindow().getDecorView().setBackgroundColor(Color.WHITE);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().hide();

        mDrawerLayout = findViewById(R.id.drawer);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
                R.string.open_drawer, R.string.close_drawer);
        mDrawerLayout.addDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.map_button:
                Intent intent = new Intent(MarkerActivity.this, MapActivity.class);
                startActivity(intent);
                break;
            case R.id.comingsoon:
                Toast.makeText(this, "Coming soon", Toast.LENGTH_SHORT).show();
                break;
            case R.id.comingsoon2:
                Toast.makeText(this, "Coming soon", Toast.LENGTH_SHORT).show();
                break;
        }
        mDrawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }
}

My activity_markers.xml

<android.support.v4.widget.DrawerLayout 
            android:id="@+id/activity_markers"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

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

drawermenu.xml

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/map_button"
        android:title="Map"/>
    <item android:id="@+id/comingsoon"
        android:title="Coming Soon"/>
    <item android:id="@+id/comingsoon2"
        android:title="Coming Soon"/>
</menu>
  • That doesn't appear to be the complete `activity_markers` layout. It sounds like you've got some `View`s out of order. https://stackoverflow.com/questions/31397792, https://stackoverflow.com/questions/22105074 – Mike M. May 17 '18 at 00:54
  • Ohh man thanks i found the answer – Max Yablonskyi May 17 '18 at 08:48
  • No, don't do that. Just fix your layout. You shouldn't have to reorder `View`s from your code. – Mike M. May 17 '18 at 08:50
  • Thanks. Also i have another question. Maybe you know answer https://stackoverflow.com/questions/50162428/instagram-sign-in-webview – Max Yablonskyi May 17 '18 at 09:21

3 Answers3

0

You probably put each item individually in the navigation drawer layout. If so, you need to define these elements in onCreate() method and setOnClickListener for them. Can you please share the navigation drawer layout xml.

Otherwise, you defined a ListView, but haven't initialised it.

ashazar
  • 714
  • 5
  • 11
0

The id of the navigationview and drawer are incorrect

Try this:

    mDrawerLayout = findViewById(R.id.activity_markers);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
            R.string.open_drawer, R.string.close_drawer);
    mDrawerLayout.addDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = findViewById(R.id.drawer);
    navigationView.setNavigationItemSelectedListener(this);
DennisVA
  • 2,068
  • 1
  • 25
  • 35
0

I found the answer. You just need to paste this line navigationView.bringToFront(); after NavigationView navigationView = findViewById(R.id.your_id);