Color Resource ใช้ยังไง ?
android:background="@color/coral"
- Color State List
android:textColor="@color/button_color_actions.xml"
Color Resource + Button Example
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="color from res"
android:textColor="@color/button_color_actions" />
</LinearLayout>
res/color/button_color_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"android:color="#FF0000" /> <!-- สีตอนกดปุ่ม -->
<item android:color="#77d" /> <!-- default -->
</selector>
Color Resource + EditText Example
<EditText android:layout_width="match_parent"
android:layout_height="100dp"
android:textColor="@color/my_edittext_colors"
android:inputType="text"/>
<EditText android:layout_width="match_parent"
android:layout_height="100dp"
android:textColor="@color/my_edittext_colors"
android:inputType="text"/>
res/color/my_edittext_colors.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#FF0000"/>
<item android:color="#000202"/>
</selector>
Color Resource + CheckBox Example
<CheckBox
android:text="CheckBox"
android:buttonTint="@color/my_checkbox_colors"
android:textColor="@color/my_checkbox_colors"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/checkBox1"/>
<CheckBox
android:text="CheckBox"
android:buttonTint="@color/my_checkbox_colors" // สีของปุ่ม checkbox
android:textColor="@color/my_checkbox_colors" // สีของข้อความของ checkbox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/checkBox2"/>
res/color/my_checkbox_colors.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#00B8D4"/>
<item android:color="#030000" /> <!-- default -->
</selector>
Color Resource with RadioButton Example
.....เหมือน checkbox........