Question

OK, I am very new to Android and C#. Just started today in fact. I normally program in VB, but anyway I was trying MonoDroid out and after a couple of small tutorials I tried something on my own. What I want is that there are two radio buttons on the canvas. And a disabled button, it only gets enabled when you click on one of the radio buttons. Funnily, you have to code the radio buttons to uncheck when the other one is clicked unlike in windows forms, or I'm missing someting. But I managed that. And when you press the button 'Next' it goes to the next page.

This is my code for the above:

    protected override void OnCreate(Bundle bundle)
    {

        base.OnCreate(bundle);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        RadioButton radSilent1 = FindViewById<RadioButton>(Resource.Id.radSilent);
        RadioButton radVibrate1 = FindViewById<RadioButton>(Resource.Id.radVibrate);
        Button button1 = FindViewById<Button>(Resource.Id.btnNext);
        radSilent1.Click += delegate
        {
            button1.Enabled = true;
            if (radSilent1.Checked == true)
                radVibrate1.Checked = false;
            else if (radVibrate1.Checked == true)
                radSilent1.Checked = false;
            {
            }
        };
        radVibrate1.Click += delegate
        {
            button1.Enabled = true;
            if (radVibrate1.Checked == true)
                radSilent1.Checked = false;
            else if (radSilent1.Checked == true)
                radVibrate1.Checked = false;
            {
            }
        };
        // Set our view from the "secondry" layout resource
        button1.Click += delegate { SetContentView(Resource.Layout.Secondry); };
    }

This brings up the second canvas. Where I have another button 'Back'. When I press that button it brings me to the first screen but the above code doesn't work. I press the two radio buttons and both are checked, and the button doesn't enable either. Why does this happen? Remember I am very new to this sorry. lol Any help is much appreciated.

Thanks.

Was it helpful?

Solution

The error is that when you have used new layout (SetContentView(Resource.Layout.Secondry)) you have lost all attached event handlers that was initialized in OnCreate(Bundle bundle). To solve this problem you need to create 2 methods something like InitializeMainView() and InitializeSecondView() where attach handlers to controls events on selected layout. And after changing layout you just call init method for selected view.

But i think that the best solution will be to create new separate activity for second view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top