Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Newbie: Trying to toggle background color via button

$
0
0

So I have the main background color set, and I want to toggle the color by clicking a button. I can get the color to change on first click, but it won't return to the original color on second click:

See code:

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set your view layout layout resource file
        SetContentView (Resource.Layout.Main);

        // Initilize button from the layout resource
        Button button = FindViewById<Button> (Resource.Id.myButton);

        button.Click += delegate {
            //display toast message
            if (BackgroundIsNormal()) {
                SetNewBackgroundColor();

            } else { 
                BackgroundIsNormal();
            }
        };
    }


    private bool BackgroundIsNormal()
    {
        LinearLayout mainLayout = FindViewById<LinearLayout> (Resource.Id.linearlayout2);
        mainLayout.SetBackgroundColor (Color.Aqua);
        return true;
    }

    private bool SetNewBackgroundColor()
    {
        LinearLayout mainLayout = FindViewById<LinearLayout> (Resource.Id.linearlayout2);
        mainLayout.SetBackgroundColor (Color.Green);
        return true;
    } 

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>