Create Intent for the layout buttons of Drawer , Drawer button , when clicking looks just a text

Asked

Viewed 304 times

1

How do I make the Drawer layout buttons take me to another screen? I tried this way , but it didn’t work Another thing, when I click on the Drawer menu, it looks like it’s just a text, don’t give 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 Inteiro

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;
}

}

2 answers

1


No need to create a button inside Drawer, just change the file suaactivity_drawer.xml 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 this way.

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

The most advisable really is to use Ragments currently, but I believe that for your case this can solve.

I hope I helped :D

0

Missing add the Actionbardrawertoggle at the Drawerlayout and indicate which is the Navigationitemselectedlistener that the Navigationview must use:

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 must change the method onNavigationItemSelected() thus:

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 standard Navigationdrawer implementation in Adroid Studio.

  • Actually I rushed, but it was just a suggested navigation with Fragment. I will do this in the comments.

  • 1

    I know that your intention was to help, but just as the questions must be objective, the answers must also be so and you must answer the question. :)

Browser other questions tagged

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