Android - How to make Navigationdrawer items clickable?

Asked

Viewed 125 times

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.

1 answer

0

I am currently creating an application with Navigation Drawer. Mine is customized, I don’t know if it will be the same as yours, but here it goes:

ACTIVITY

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

    emailUserLogado = getIntent().getStringExtra("email");


//Instanciar o Navigation Drawer existente num layout
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        TextView q = (TextView) findViewById(R.id.txf);

    //  q.setText(emailUserLogado);




    drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
/* TextView clicaveis, só falta o setClickListener e método OnClick, mas isso já deve ser do seu conhecimento.*/
    txtviewtoTitleofBolos = (TextView) drawer.findViewById(R.id.txttoBolosCriados);
    txtviewtoHome = (TextView) drawer.findViewById(R.id.txt_nv_Home);
    txtviewtoCriar = (TextView) drawer.findViewById(R.id.txt_nv_Criar);

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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
   >
        <include
            layout="@layout/activity_menu">
        </include>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorPrimary"
        android:fitsSystemWindows="true"
        app:itemTextColor="@color/colorSecondBackGround">

        <LinearLayout
            android:id="@+id/linearexemplo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical">
            <!-- Include para o latyout onde estão as textviews que em cima instanciei -->
            <include layout="@layout/layout_to_navigate" />

        </LinearLayout>


    </android.support.design.widget.NavigationView>
    <!--app:menu="@menu/activity_main_proto_drawer"-->

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

I hope it works! In my case I wanted a Navigation Drawer with the layout customizable by me, I do not know if it is your case!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.