Click on the icon of the action bar to open a new entry

Asked

Viewed 168 times

1

I would like to know how to click on the icon/word in the action bar and open a new.

Here’s what I’m doing:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.marcas:
            Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
                    .show();
            break;
        }

    return true;
}

It returns me the message of how I clicked on the icon, but I would like to open a new entry.

  • just call the Internet instead of the Toast

  • @Joannis, I did and it didn’t work out, man. I’ve tried this before. You can put the code in how you did it ?

  • postei, see if it works!

  • @Joannis, I did as you showed me there. Error: Error:(98, 2) error: reached end of file while Parsing

  • try now, we missed Return

1 answer

2


You can do it like this:

public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.menueditar) {

            Intent iedicao = new Intent(MainActivity.this, paginadeedicao.class);

            startActivity(iedicao);
            MainActivity.this.finish();
            return true;
        }
    }
    return super.onOptionsItemSelected(item);
}

where:

                Intent iedicao = new Intent(MainActivity.this, paginadeedicao.class);

You create a new Intent with the name in this case from mainActivity (current activity) to paginated.class (which is another code page)

You can also send parameters with the extra put:

    iedicao.putExtra("pagina", page);

has to come before startActivity.

You’re inflating the menu?

//coloca os ícones no menu
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
            if(menu.getClass().getSimpleName().equals("MenuBuilder")){
                try{
                    Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch(NoSuchMethodException e){
                } catch(Exception e){
                    throw new RuntimeException(e);
                }
            }
        }

        return super.onMenuOpened(featureId, menu);
    }
    //cria todos os menus
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        onMenuOpened(Window.FEATURE_ACTION_BAR, menu);
        return true;
    }
    //seta as opções de função dos itens do menu
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.menueditar) {

            startActivity(new Intent(MainActivity.this, edicao.class));
            MainActivity.this.finish();

            return true;
        }
    }
    return super.onOptionsItemSelected(item);
}

that

startActivity(new Intent(MainActivity.this, edicao.class));

is equal to that

Intent iedicao = new Intent(MainActivity.this, paginadeedicao.class);

startActivity(iedicao);
  • I got it, man. I did what you said, I just got a few stitches. It helped a lot. Thank you very much.

  • put as correct the answer, hahah we help each other, so the world grows

Browser other questions tagged

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