restart app with theme new

Luis_dmLuis_dm USMember ✭✭✭
edited August 2016 in Xamarin.Android

I want to restart my application after i give to him click to a button (this will have the function to change the theme of the application (color of the toolbar, navigationbar, etc.)), how could it do it?

Answers

  • mubimubi USMember ✭✭✭

    any solution ???

  • Luis_dmLuis_dm USMember ✭✭✭
    edited November 2016

    Yes, first you make a style theme with the color you want..

    <style name="ThemeAzul" parent="ThemeAzul.Base">
            < style>
              <style name="ThemeAzul.Base" parent="Theme.AppCompat.Light">
               <item name="android:navigationBarColor">#0d3655</item>
                <item name="colorPrimary">#166099</item>
             <item name="colorPrimaryDark">#0d3655</item>
    </ style>
    

    If you are going to want everything to change color, the only way I found was to give Finish(); and save the value before the application closes then get it to start and as the value gets the color that was chosen... (I did a list of colors in a listview), after obtaining the value, call :

    SetTheme(Resource.Style.ThemeAzul);

    and before than start the application this is put.

  • AlexDunnAlexDunn USMember ✭✭✭

    You don't have to restart your app to change the theme.
    Here's a bit of code I've used in the past: (note that ThemeColor is a custom enum and the UserStore is a custom memory store, and my styles are custom)

    private void UpdateTheme()
            {
                RunOnUiThread(() =>
                {
                    if (App.Current?.CurrentUserStore?.CurrentUser?.ThemeColor != null)
                    {
                        switch (App.Current.CurrentUserStore.CurrentUser.ThemeColor)
                        {
                            case ThemeColor.Red:
                                SetTheme(Resource.Style.Theme_Main_Red);
                                Window.SetStatusBarColor(new Color(211, 47, 47));
                                break;
                            case ThemeColor.Pink:
                                SetTheme(Resource.Style.Theme_Main_Pink);
                                Window.SetStatusBarColor(new Color(194, 24, 91));
                                break;
                            case ThemeColor.Lightblue:
                                SetTheme(Resource.Style.Theme_Main_LightBlue);
                                Window.SetStatusBarColor(new Color(2, 136, 209));
                                break;
                            case ThemeColor.Green:
                                SetTheme(Resource.Style.Theme_Main_Green);
                                Window.SetStatusBarColor(new Color(56, 142, 60));
                                break;
                            case ThemeColor.Deeppurple:
                                SetTheme(Resource.Style.Theme_Main_DeepPurple);
                                Window.SetStatusBarColor(new Color(84, 45, 168));
                                break;
                            case ThemeColor.Deeporange:
                                SetTheme(Resource.Style.Theme_Main_DeepOrange);
                                Window.SetStatusBarColor(new Color(244, 81, 30));
                                break;
                            case ThemeColor.Cyan:
                                SetTheme(Resource.Style.Theme_Main_Cyan);
                                Window.SetStatusBarColor(new Color(0, 151, 167));
                                break;
                            case ThemeColor.Amber:
                                SetTheme(Resource.Style.Theme_Main_Amber);
                                Window.SetStatusBarColor(new Color(255, 160, 0));
                                break;
                        }
                    }
                });
            }
    
    

    This was run in my MainActivity on either Xamarin.Forms Android or Native

  • Luis_dmLuis_dm USMember ✭✭✭

    I did not know, thank you @AlexDunn , I will apply it immediately.

Sign In or Register to comment.