0
I’m 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 was 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 the.xml menu looked 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 don’t know what to do to make Navigationdrawer items clickable. I spent hours searching and experimenting and errors always appear. I’ve already created Activity for the "Glossary" option but I can’t connect Activity to the Drawer navigation. Please help! Thank you :)
I have this example that takes from the book Google Android by Ricardo Lecheta (http://ricardolecheta.com.br/) https://github.com/icaronunes/Carro/blob/master/app/src/main/java/br/livroandroid/carros/activity/BaseActivity.java Check the link, watch the method onNavDrawerItemSelected. Should help you.
– Icaro