How can I send an activity according to the option in my menu drawer layout?

1

I have already prepared my side menu, but now I need to select an option to send me the corresponding activity. Then I leave my code:

public class cliente_inicio extends AppCompatActivity {


    private DrawerLayout drawerLayout;
    private LinearLayout mainLayout;
    private ListView menuLateral;

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

        ActionBar aBar = getSupportActionBar();
        aBar.setHomeButtonEnabled(true);
        aBar.setDisplayHomeAsUpEnabled(true);

        drawerLayout= (DrawerLayout)findViewById(R.id.drawerlayaout);
        mainLayout=(LinearLayout)findViewById(R.id.mainLayout);
        menuLateral=(ListView)findViewById(R.id.menuLateral);

        String[] opciones={"Catalogo","Paquetes","Galeria","Contacto","Iniciar Sesion"};
        ArrayAdapter<String> adp=new ArrayAdapter<String>(cliente_inicio.this, android.R.layout.simple_list_item_1,opciones);
        menuLateral.setAdapter(adp);

        menuLateral.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String opcSeleccionado=(String) menuLateral.getAdapter().getItem(position);
                Toast.makeText(cliente_inicio.this, opcSeleccionado, Toast.LENGTH_SHORT).show();
            }
        });
    }
    public boolean onCreateOptionsMenu (Menu menu){
        getMenuInflater().inflate(R.menu.cliente_inicio,menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item){
        int id = item.getItemId();

        if (id==android.R.id.home){
            if (drawerLayout.isDrawerOpen(menuLateral)){
                drawerLayout.closeDrawer(menuLateral);
            }else {
                drawerLayout.openDrawer(menuLateral);
            }
        }
        if (id==R.id.action_settings){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
    
asked by Jessica 23.02.2018 в 04:26
source

1 answer

0

I usually do it through Intent , in the onOptionsItemSelected method.

For example:

public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            Intent i = new Intent(this, ActivityQueSeLLamara.class);
            startActivity(i);
        }

        if (id == R.id.nav_otra) {
            Intent i = new Intent(this, ActivityOtra.class);
            startActivity(i);
        }

        return super.onOptionsItemSelected(item);
    }

Here,

  • In the if you have to ask for the id of the option that has been selected. In the example I used action_settings and another hypothetical id nav_otra which you will have to change for some valid option of your menu.
  • In Intent the value this will represent the current activity. You can also put it that way Intent i = new Intent(MainActivity.this, ActivityOtra.class); , assuming that the activity in which you have the drawer is MainActivity .
  • ActivityOtra.class and ActivityQueSeLLamara.class are fictitious names that you must change for the actual activitys you want to call in each menu option that you press.
  • I have not put this if in my answer: if (id==android.R.id.home){... because it does not concern the solution of the problem. I say this because it does not mean that you should delete that part in your method because I did not put it in the answer.
answered by 23.02.2018 / 11:56
source