How to show a message after a send action?

Asked

Viewed 105 times

1

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

Follows 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());
    }
}
  • https://forums.xamarin.com/discussion/81278/how-to-handle-the-result-of-startactivityforresult-in-forms

1 answer

0


Well, I believe that your mistake is in logic, in your "IF" you have an exception, and in your "ELSE" would be where you actually send the email, IE, Toast should be at the end of ELSE, so:

//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 serve to code organization would be to modify a little this your method to be clearer placing the whole initial part and also the IF within a validation method, if it is valid you execute the code within Else. But that’s just an idea.

I hope I’ve helped.

  • Thanks, your tip worked! A question, you know how to separate the email into paragraphs? For example: I wanted the Radiogroup values to be sent in a paragraph and the Edittext field comment to be in a paragraph below the first paragraph

  • If you do the separation in your hand, don’t you? and this Intent email library (Android.Content.Intent.Actionsend) don’t you have something to say about that? sorry but I can’t say.

Browser other questions tagged

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