Button in action bar to open navigation Drawer

Asked

Viewed 575 times

1

Good, I’m making an application that uses the navigation Drawer. From the android 5.1 the button "hamburguer" in the bar action works, and when you click on it the navigation drawer Drawer opens. But I tested now on mobile with android 4.2 and emulator with the same android, and neither accept the click to open the drawer. Just open by sliding. For older versions you need to implement some method?

public class MenuPrincipal extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    ImageView img_Principal;
    DocumentView documentView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_principal);
    Toolbar 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);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

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

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_principal, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

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

    if (id == R.id.nav_carteira_profissional) {
        Intent i = new Intent(getApplicationContext(), CarteiraProfissional.class);
        startActivity(i);
    } else if (id == R.id.nav_autenticar_carteira) {
        Intent i = new Intent(getApplicationContext(), AutenticarCarteiraProfissional.class);
        startActivity(i);
    } else if (id == R.id.nav_processo_em_andamento) {
        Intent i = new Intent(getApplicationContext(), Processo_em_Andamento.class);
        startActivity(i);
    } else if (id == R.id.nav_financeiro) {
        Intent i = new Intent(getApplicationContext(), Financeiro.class);
        startActivity(i);
    } else if (id == R.id.nav_share) {

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

    } else if (id == R.id.nav_denuncia) {
        Intent i = new Intent(getApplicationContext(), Denuncia.class);
        startActivity(i);
    } else if (id == R.id.nav_pessoa_fisica) {
        Intent i = new Intent(getApplicationContext(), SolicitarInscricaoPessoaFisica.class);
        startActivity(i);
    } else if (id == R.id.nav_beneficios) {
        Intent i = new Intent(getApplicationContext(), MapsActivity.class);
        startActivity(i);
    }


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

Remembering that the android 5.1 up is working the click on the action bar, no 4 that is not.

  • Insert into your question the most relevant part of your code, which you are using to create the Drawer navigation.

No answers

Browser other questions tagged

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