Change default design theme to customized color

左心房为你撑大大i 提交于 2019-12-01 08:17:18

First Method:

In res/values-v21/styles.xml,

<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
    <item name="android:colorAccent">@color/custom_theme_color</item>
    <item name="android:colorPrimaryDark">@color/custom_theme_color</item>
    <item name="android:colorPrimary">@color/custom_theme_color</item>
</style>

The above code is simple and working well. In Android studio, it could be able to change from default sky blue color to any other colors like pink or green!

Second Method:

Create 3 files under xml ie. res/xml such as checked.xml, unchecked.xml and custom_checkbox.xml

checked.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2px" android:color="#FF00FF" />
<size android:height="20dp" android:width="20dp"/>
</shape>

unchecked.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2px" android:color="#ffc0c0c0" />
<size android:height="20dp" android:width="20dp"/>
</shape>

custom_checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" 
      android:drawable="@xml/checked" /> 
<item android:state_pressed="true"
      android:drawable="@xml/checked" /> 
<item android:drawable="@xml/unchecked" /> 
</selector>

From above second method, it (customized file) also could be able to create any shapes and colors based on our requirements. Even we can be able to customize the style, shapes and themes for a widget by using 2nd method.

Above two methods are working fine for me!

Also in from v23 with AppCompat you can use android.support.v7.widget.AppCompatCheckBox and change CheckBox style like this:

<android.support.v7.widget.AppCompatCheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:theme="@style/CheckBox"
    />

where in style.xml:

<style name="CheckBox" parent="Widget.AppCompat.CompoundButton.CheckBox">
    <item name="colorControlNormal">@color/checkbox_normal</item> <!-- border color -->
    <item name="colorControlActivated">@color/checkbox_activated</item> <!-- fill color -->
    <item name="colorControlHighlight">@color/checkbox_highlight</item> <!-- animation color -->
</style>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!