I am very new to programming. I created a NavigationDrawer with the help of a tutorial. It looks like this: main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_quote_of_the_dat"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hp.myapp.QuoteOfTheDat"
android:background="@color/colorAccent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/randomQuote" android:layout_marginTop="147dp"
android:textSize="24sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:textColor="@android:color/background_light" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
android:layout_gravity="start"
android:id="@+id/drawerLayout"
app:headerLayout="@layout/navigation_header">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
and MainActivity.java looks like this:
package com.example.hp.myapp;
import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import java.util.Random;
import android.view.View.OnClickListener;
import android.content.res.Configuration;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;
import android.view.View;
public class QuoteOfTheDat extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
NavigationView navigationView = (NavigationView) findViewById(R.id.drawerLayout);
// navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener());
//NavigationView.OnNavigationItemSelectedListener listener);
//System.out.println(quotes[random.nextInt(3)]);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote_of_the_dat);
mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_quote_of_the_dat);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
and menu.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/home"
android:icon="@mipmap/ic_home_black_24dp"
android:title="Home"/>
<item android:id="@+id/glossary"
android:icon="@mipmap/ic_book_black_24dp"
android:title="Glossary"/>
<item android:id="@+id/anxiety"
android:icon="@mipmap/ic_lock_outline_black_24dp"
android:title="Anxiety"/>
<item android:id="@+id/physicality"
android:icon="@mipmap/ic_pan_tool_black_24dp"
android:title="Physicality"/>
<item android:id="@+id/date"
android:icon="@mipmap/ic_favorite_black_24dp"
android:title="Date"/>
</menu>
But now I do not know what to do, to make NavigationDrawer items clickable. I spent hours researching and experimenting and errors always appear. I already created Activity for the "glossary" option but I can not connect the activity to the navigation drawer. Please help! Thanks:)