Button in action bar to open navigation drawer

1

Well, I'm making an application that uses the navigation drawer. From android 5.1 the "hamburguer" button in the action bar works, and when you click it the navigation drawer opens. But I tested now on the mobile with android 4.2 and the emulator with the same android, and in neither of them accepts the click to open the drawer. Just open it by sliding. For older versions do 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;
    }
}

Remember that from android 5.1 up is working the click on the action bar, on the 4 that is not.

    
asked by anonymous 05.06.2017 / 19:01

0 answers