How to give the user the possibility to switch between different colors skin in your App

余生颓废 提交于 2019-12-03 00:28:42

Regarding to @Joaquim Ley's answer we can change Theme before super.onCreate() .

In my app(at work) my styles.xml :

This is my default theme

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:colorAccent">@color/colorPrimary</item>
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    <item name="android:colorFocusedHighlight">@color/colorPrimary</item>
    <item name="android:colorControlNormal">@color/amber_300</item>
    <item name="android:colorControlActivated">@color/amber_300</item>
    <item name="android:colorControlHighlight">@color/colorControlHighlight</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    <item name="android:windowTranslucentStatus">false</item>
    <item name="actionBarItemBackground">?attr/selectableItemBackground</item>
    <item name="colorControlHighlight">@color/colorControlHighlight</item>
    <item name="android:windowContentTransitions">true</item>
    <!-- Customize your theme here. -->
</style>

And this is my Green Theme :

<style name="AppTheme.Green" parent="AppTheme">
    <item name="colorPrimary">@color/green_500</item>
    <item name="colorPrimaryDark">@color/green_700</item>
    <item name="android:colorAccent">@color/green_300</item>
    <item name="android:statusBarColor">@color/green_700</item>
    <item name="android:colorFocusedHighlight">@color/green_500</item>
    <item name="android:colorControlNormal">@color/green_300</item>
    <item name="android:colorControlActivated">@color/green_300</item>
</style>

When changing theme :

@Override
protected void onCreate(Bundle savedInstanceState) {
    mPrefs=getSharedPreferences(getResources().getString(R.string.preference_name),MODE_PRIVATE);
    mEditor=mPrefs.edit();
    mEditor.apply();
    defaultColor=mPrefs.getInt(getResources().getString(R.string.default_color),0);
    //you can do some switch case or if else of defaultColor and change theme
    setTheme(R.style.AppTheme_Green);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_voice_record);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

This is my first app in work, but in my personal app i created a BaseActivity and created a method handleColor(int color,int darkcolor). But this won't change entire theme just Toolbar, StatusBar, NavigationBar and EditText marker and underline colors:

public class BaseActivity extends AppCompatActivity {

public void handleColor(int color,int darkcolor){
    //changing toolbar color
    if(getSupportActionBar()!=null){
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
    }

    //if bigger than Api 21 change status bar color
    if(isLolipop()){
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(darkcolor);
    }
}

public boolean isLolipop(){
    return  Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
  }

}

When changing color i am using Material Dialogs Color Picker. Sample apk : sample.apk and Github: material-dialogs. If you want to see how it looks like i can give you a video of my app.

Colors from : Material Color Palette

Anyways i know 2 different approach , if you like one of them i can explain more.

@Edit : Good news for you, I found this Github repo : app-theme-engine and its working good. You can try sample apk. If you can't import it via gradle try this : compile 'com.github.naman14:app-theme-engine:0.5.1@aar'

This should be done in the styles/AppTheme.xml

It’s only possible to change the theme of an Activity in the onCreate() method and that to only before super() has been called. Changing the theme is fairly straight forward, something like below should do it:

setTheme(someBooleanFlag ? R.style.AppThemeOne : R.style.AppThemeTwo);

Read more here:

Ali Muzaffar's Medium post on this

Styles and Themes

Material Theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!