Create intent for drawer layout buttons, drawer button, clicking looks like text only

1

How do I make drawer layout buttons take me to another screen? I tried this way but it did not work  Something else, when I click on the drawer's menu, it looks like it's just a text, not that click effect, I'll leave my entire MainActivity below

    @SuppressWarnings("StatementWithEmptyBody")
    public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
        if (id == R.id.testando) {
        Button button = (Button) findViewById(R.id.testando);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it= new Intent(MainActivity.this, 
               Ingredientes.class);
            }
        });

MainActivity Integer

import android.content.Intent;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
Intent intent;

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

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    toggle.syncState();
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }

}

@SuppressWarnings("StatementWithEmptyBody")

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.testando) {
        Button button = (Button) findViewById(R.id.testando);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it= new Intent(MainActivity.this, Ingredientes.class);
            }
        });


    } else if (id == R.id.passo1) {


    } else if (id == R.id.passo2) {


    } else if (id == R.id.passo3) {


    } else if (id == R.id.passo4) {


    } else if (id == R.id.passo5) {


    } else if (id == R.id.passo6) {


    } else if (id == R.id.passo7) {


    } else if (id == R.id.passo8) {


    } else if (id == R.id.donate) {



    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

    
asked by anonymous 25.03.2017 / 05:15

2 answers

1

You do not need to create a button inside the drawer, just change the suaactivity_drawer.xml file inside the menu folder.

In your case it would look something like

 <item
        android:id="@+id/testando"
        android:icon="@drawable/ic_home"
        android:title="@string/testando" />

And then in your java class would just put it like this.

 if (id == R.id.testando) {
    Intent it= new Intent(MainActivity.this, Ingredientes.class);
    startActivity(it);
 }

The most advisable really is to use fragments these days, but I believe that for you it can solve.

I hope I have helped: D

    
28.03.2017 / 15:00
0

The ActionView > / p>

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

You should change the method onNavigationItemSelected() like this:

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.testando) {
        Intent it = new Intent(MainActivity.this, Ingredientes.class);
        startActivity(it);
    }

    ....
    ....

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

Note:
I assumed that you are using the default NavigationDrawer implementation in Adroid Studio.

    
25.03.2017 / 14:57