How to Display a Message Box or Alert Message in C# Xamarin Android

Hi, I'm still new in Xamarin Android platform using C#. I was wondering whether I can show an alert message or a message box based on what I checked on radio button or check boxes? I've tried this method, implement it in my code but alas, it's not working. By the way, here's my code:
Button chk = FindViewById<Button>(Resource.Id.button1); Button nxt = FindViewById<Button>(Resource.Id.button2); CheckBox chk1 = FindViewById<CheckBox>(Resource.Id.checkBox1); CheckBox chk2 = FindViewById<CheckBox>(Resource.Id.checkBox2); CheckBox chk3 = FindViewById<CheckBox>(Resource.Id.checkBox3); CheckBox chk4 = FindViewById<CheckBox>(Resource.Id.checkBox4); CheckBox chk5 = FindViewById<CheckBox>(Resource.Id.checkBox5); CheckBox chk6 = FindViewById<CheckBox>(Resource.Id.checkBox6); CheckBox chk7 = FindViewById<CheckBox>(Resource.Id.checkBox7); CheckBox chk8 = FindViewById<CheckBox>(Resource.Id.checkBox8); chk.Click += delegate { if (chk1.Checked == true && chk5.Checked == true) { Toast.MakeText(this, "Your blood type is A", ToastLength.Short).Show(); } else { Toast.MakeText(this, "Your blood type is not A", ToastLength.Short).Show(); }; };

Thank you.

Answers

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    Hello @ellalaelasari ,
    i'm not an expert but here is is a little sample who works for me

    `
    CheckBox chk2;
    CheckBox chk3;
    protected override void OnCreate(Bundle bundle)
    {
    base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
            //
            CheckBox chkMain = FindViewById<CheckBox>(Resource.Id.checkBox1);
            chkMain.Click += ChkMain_Click;
    
            //
            chk2 = FindViewById<CheckBox>(Resource.Id.checkBox2);
            chk3 = FindViewById<CheckBox>(Resource.Id.checkBox3);
        }
    
    
    //<del></del>
        private void ChkMain_Click(object sender, EventArgs e)
        {
            //
            string message = string.Empty;
    
            //
            if (chk2.Checked == true)
                message = "check box 2 is checked";
            else
                message = "check box 2 is unchecked";
    
            //
            Toast.MakeText(ApplicationContext, message, ToastLength.Long).Show();
    
        }
    

    `

    hope this help

  • ellalaelasariellalaelasari USMember

    @ChristopheBERNARD said:
    Hello @ellalaelasari ,
    i'm not an expert but here is is a little sample who works for me

    `
    CheckBox chk2;
    CheckBox chk3;
    protected override void OnCreate(Bundle bundle)
    {
    base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
    
            //
            CheckBox chkMain = FindViewById<CheckBox>(Resource.Id.checkBox1);
            chkMain.Click += ChkMain_Click;
    
            //
            chk2 = FindViewById<CheckBox>(Resource.Id.checkBox2);
            chk3 = FindViewById<CheckBox>(Resource.Id.checkBox3);
        }
    

    //
    private void ChkMain_Click(object sender, EventArgs e)
    {
    //
    string message = string.Empty;

            //
            if (chk2.Checked == true)
                message = "check box 2 is checked";
            else
                message = "check box 2 is unchecked";
            
            //
            Toast.MakeText(ApplicationContext, message, ToastLength.Long).Show();
    
        }
    

    `

    hope this help

    i have to choose two option (ex : chk1 and chk2) and i got stuck on it. can you help me?
    this is my layout :)
    if i check A and AB, than it will show a message dialog. (ex : your are AB)

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    hello again @ellalaelasari
    sorry but my english is not very high and i can't understand what you want exactly ...

    copy paste my code and try to adapt perhpas with your task ...
    in my layout i have just three checkbox ...

    don't forget when you type a reply to indicate my name as this i receive a notification :)

    have a nice coding day

  • BernardoSalgueiro.0082BernardoSalgueiro.0082 BRMember ✭✭
    edited July 2016

    @ellalaelasari,
    I don't know what is not working in your app, but I would suggest you to use the following code

    Toast.MakeText(this.ApplicationContext, "Your blood type is not A", ToastLength.Short).Show();

    instead of

    Toast.MakeText(this, "Your blood type is not A", ToastLength.Short).Show();

  • ariolarielariolariel USMember

    Ty guys! I was trying to do something like that.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    Hello all,
    @MohammadRezaMoradi => thanks a lot guy you save my week you know ...
    great sample guy ...thanks a lot for sharing our knowledge ...

    have a nice day

  • SagarPanwalaSagarPanwala USMember ✭✭✭

    @MohammadRezaMoradi : Thanks for fantastic answer. But I need enhancement, how can I dismiss the dialog ? Suppose I show dialog with this way, now I have running some service in Timer. Now if timer service show the alert, hide the previous visible alert.

  • rodrigodzirodrigodzi Member ✭✭
    edited February 13
  • zzzzzchzzzzzch Member ✭✭
Sign In or Register to comment.