How do I show a message after a send action?

1

I'm trying to get the Android app to display a "successfully sent email" message after an email, which is linked to the send button action, to be sent correctly. I tried to show a Toast, but it shows before the email is sent.

Follow the code

void enviar_Click(object sender, EventArgs e)
{
    try
    {
        RadioButton rdbgrupo1 = FindViewById<RadioButton>(rdgconquiste.CheckedRadioButtonId);
        RadioButton rdbgrupo2 = FindViewById<RadioButton>(rdgcrie.CheckedRadioButtonId);
        RadioButton rdbgrupo3 = FindViewById<RadioButton>(rdgviva.CheckedRadioButtonId);
        RadioButton rdbgrupo4 = FindViewById<RadioButton>(rdgentregue.CheckedRadioButtonId);
        int RadioGroupIsChecked(RadioGroup radioGroup)
        {
            //-1 means empty selection
            return radioGroup.CheckedRadioButtonId;
        }

        //When user doesn't check a radio button, show a Toast
        if (RadioGroupIsChecked(rdgconquiste) == -1 || RadioGroupIsChecked(rdgcrie) == -1 || RadioGroupIsChecked(rdgviva) == -1 || RadioGroupIsChecked(rdgentregue) == -1)
        {
            string excecao = "Ao menos um botão de cada campo deve ser selecionado e o comentário deve ser preenchido";
            Toast.MakeText(this, excecao, ToastLength.Long).Show();
        }
        else
        {
            String emailescolhido = spinner.SelectedItem.ToString();
            String campocomentario = comentário.Text;

            var email = new Intent(Android.Content.Intent.ActionSend);
            //send to
            email.PutExtra(Android.Content.Intent.ExtraEmail,
            new string[] { "" + emailescolhido });
            //cc to
            email.PutExtra(Android.Content.Intent.ExtraCc,
            new string[] { "" });
            //subject
            email.PutExtra(Android.Content.Intent.ExtraSubject, "SABIA QUE VOCÊ FOI RECONHECIDO?");
            //content
            email.PutExtra(Android.Content.Intent.ExtraText,
            "Você foi reconhecido pelo(s) valor(es) de: " + rdbgrupo1.Text + " , " + rdbgrupo2.Text + " , " + rdbgrupo3.Text + " e " + rdbgrupo4.Text + "                                                                      " + " " + campocomentario);
            email.SetType("message/rfc822");
            StartActivity(email);

        }

        string enviado = "Email enviado com sucesso";
        RunOnUiThread(() => Toast.MakeText(ApplicationContext, enviado, ToastLength.Long).Show());
    }
}
    
asked by anonymous 19.10.2017 / 11:58

1 answer

0

Well, I believe that your error is in logic, in your "IF" you have a deal of exception, and in your "ELSE" would be where you actually do the sending of the email, ie Toast should be at the end of ELSE, like this:

//When user doesn't check a radio button, show a Toast
if (RadioGroupIsChecked(rdgconquiste) == -1 || RadioGroupIsChecked(rdgcrie) == -1 || RadioGroupIsChecked(rdgviva) == -1 || RadioGroupIsChecked(rdgentregue) == -1)
{
    string excecao = "Ao menos um botão de cada campo deve ser selecionado e o comentário deve ser preenchido";
    Toast.MakeText(this, excecao, ToastLength.Long).Show();
}
else
{
    String emailescolhido = spinner.SelectedItem.ToString();
    String campocomentario = comentário.Text;

    var email = new Intent(Android.Content.Intent.ActionSend);
    //send to
    email.PutExtra(Android.Content.Intent.ExtraEmail,
    new string[] { "" + emailescolhido });
    //cc to
    email.PutExtra(Android.Content.Intent.ExtraCc,
    new string[] { "" });
    //subject
    email.PutExtra(Android.Content.Intent.ExtraSubject, "SABIA QUE VOCÊ FOI RECONHECIDO?");
    //content
    email.PutExtra(Android.Content.Intent.ExtraText,
    "Você foi reconhecido pelo(s) valor(es) de: " + rdbgrupo1.Text + " , " + rdbgrupo2.Text + " , " + rdbgrupo3.Text + " e " + rdbgrupo4.Text + "                                                                      " + " " + campocomentario);
    email.SetType("message/rfc822");
    StartActivity(email);

    string enviado = "Email enviado com sucesso";
    RunOnUiThread(() => Toast.MakeText(ApplicationContext, enviado, ToastLength.Long).Show()); 
}

Another point, but that would only be for code organization would be to modify this method a little to make it clearer by putting the whole initial part and also the IF inside a method of validations, if it is valid you execute the code inside of the else. But that's just an idea.

I hope I have helped.

    
19.10.2017 / 13:29