[Android] Color of Button Android – Button color Android
To set the color for Button can be used android:background=”color code” Button's
CEO: android:background=”#F00″ => me red.
However with this method when clicking the button we will not see the reaction it seemed Button should not click.
To fix this do the following:
Step 1: Create color.xml file in the folder res / values with the following content (This file defines the colors to be used):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#f00</color> <!-- Màu đỏ -->
<color name="blue">#00f</color> <!-- Màu xanh da trời -->
</resources>
Step 2: Create button_color.xml file in the folder res / drawable (without this folder, you can create new) with content (This file creates the color for button):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/red"
android:state_pressed="true"/> <!-- Hiện màu đỏ khi ấn vào nút -->
<item android:drawable="@color/blue"
android:state_pressed="false"/> <!-- Hiện màu xanh da trời khi không nhấn nút -->
</selector>
Step 3: Use the command android:background=”@drawable/button_color” to put color in button_color file created
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="My Button"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp">
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:textSize="30sp"
android:background="@drawable/button_color"/> <!-- Đặt màu trong file button_color -->
</LinearLayout>
</LinearLayout>




Recent Comments