Click the button on Toolbar

Asked

Viewed 345 times

0

I have two buttons Save and Record on my Toolbar. I’m not getting the click. If I put the button click in the normal view I can catch the click

private class btGravar implements View.OnClickListener {
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnSalvar:

                String nomeDesc = nome.getText().toString();
                String foneDesc = fone.getText().toString();
                String emailDesc = email.getText().toString();
                String ruaDesc = rua.getText().toString();

I wonder if anyone can help me ?

  • Set "[.. ] I’m unable to click." What happens? Nothing? Please be clearer.

  • Eai @Genos. True dude. I’m sorry. I forgot even. The two buttons I want one save and the other cancel and return to previous screen. I put it in the code. Thanks

1 answer

1

Do it this way to get the clicks from Toolbar:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.btnSalvar) {

        String nomeDesc = nome.getText().toString();
        String foneDesc = fone.getText().toString();
        String emailDesc = email.getText().toString();
        String ruaDesc = rua.getText().toString();

        return true;
    }

    return super.onOptionsItemSelected(item);
}
  • It didn’t work, Leonardo. It’s a mistake. FATAL EXCEPTION: main java.lang.Nullpointerexception

  • How are you putting your menu in the layout? Is it an inflated xml? It’s in the res/menu folder?

  • No. To inflando do res/layout activity_cad_client.

  • Is the menu in the same layout file? It does not have a menu xml?

  • Yes. the menu xml is in res/menu. Ma already inflating it in the layout. It already appears, but the click is not happening yet.

Browser other questions tagged

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