Alert in action of the button

Asked

Viewed 47 times

0

I researched several days, nights.. Anyway, just basic examples: my problem is to add an alert to the user after the click of the button, "Do you want to Renew?" or "Do you want to Return? How? my attempt was to make no mistakes and yet nothing does!

 btnDevolvido.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder alertDevolv = new 
 AlertDialog.Builder(ListarEmprestimosActivity.this);
            alertDevolv.setTitle("Confirme a ação!");
            //define a mensagem
            alertDevolv.setMessage("Deseja Devolver o Livro?");
            alertDevolv.setPositiveButton("Devolver", new 
 DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    // ações esperadas se clicado em "DEVOLVER"
 databaseHelper.devolverLivro(renovacaoModelo.getId());
                    Toast.makeText(ListarEmprestimosActivity.this, 
 "Devolvido com Sucesso" , Toast.LENGTH_SHORT).show();
                    Intent intent = new 
 Intent(ListarEmprestimosActivity.this, MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | 
 Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            });
 //define um botão como negativo.
            alertDevolv.setNegativeButton("Cancelar", new 
 DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
             // ações esperadas se clicado em "CANCELAR"
                    Toast.makeText(ListarEmprestimosActivity.this, 
 "Retorna A tela Principal" , Toast.LENGTH_SHORT).show();
                }
            });

            AlertDialog teste = alertDevolv.create();
            teste.show();// qse deu certo


        }
    });

I would like to add an action alert on each button of my Android application, but I can’t find anything but the basics.

Trying to answer the question itself, simply put the expected actions inside each block of Alert dabaseHelper.xxx Toast.xxx.

  • It does the action on the buttons without returning the Toast?

  • Does not show Alertdialog as intended.

  • Can you send us how the alertdialog is being shown? And if possible, the error of what you were trying to implement.

  • the implementation code is above as you can observe

  • String s = Alert("The book must be delivered to complete the action!!! nThe system will release when it completes!"); private void Alert(String s) { Toast.makeText(Listremprestimosactivity.this, s, Toast.LENGTH_SHORT). show(); }

  • To clarify, when you click on Positive, it makes a Toast and starts a new Activity, and when you click on Negative it only makes the Toast? The part at the end that you’re creating a new Intent, is that just comment? If it’s not, there’s no sense of it existing

  • neither one nor the other: .. my attempt was to make no mistakes and yet nothing does!

  • does not run Alert by clicking the button.

  • Let’s discuss in the chat I put up.

  • Murilo, I didn’t see any comment about you on the Alert code, I don’t see the need to extend the conversation to chat. Thank you for your attention!

  • 1

    So, the code for me is right, only thing that doesn’t make sense is after the test.show(); you create a new information, and starta it, does not make sense, By the logic that you are doing, when triggering the alertdialog, you already send to Mainactivty

  • 1

    Yes, I noticed that, this rereading has already helped! Now it seems to go the right way!

  • 1

    Thanks for the comment, through it I saw that there were repeated codes returning to the same function, when moving the pieces of these codes referring the actions of the form to the correct location within the action of the Alert button the rest worked perfectly.

Show 9 more comments
No answers

Browser other questions tagged

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