Giới thiệu
Bài viết minh họa cách đổi màu nền Button khi người dùng nhấn (click) bằng cách set background color trong OnClickListener.
Các bước thực hiện
Bước 1: Tạo dự án mới
Khởi tạo project Android Studio.
Bước 2: Giao diện Button (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nhấn để đổi màu!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
</android.support.constraint.ConstraintLayout>
Bước 3: Xử lý click đổi màu (src/MainActivity.java)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}
});
}
}
Bước 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Chạy ứng dụng
Kết nối thiết bị, nhấn Run. Mỗi lần nhấn Button, màu nền sẽ đổi sang colorPrimary (mặc định là xanh dương).


Cách tốt hơn: Selector Drawable (Khuyên dùng)
Thay vì code Java, hãy dùng XML selector để xử lý trạng thái pressed, focused, disabled tự động:
Tạo res/drawable/btn_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/colorPrimaryDark" />
<item android:state_focused="true" android:color="@color/colorAccent" />
<item android:color="@color/colorPrimary" /> <!-- mặc định -->
</selector>
Áp dụng cho Button
<Button
android:background="@drawable/btn_selector"
android:textColor="@android:color/white" />
Lợi ích của Selector
- Tách biệt logic UI khỏi code Java/Kotlin
- Xử lý tự động các trạng thái: pressed, focused, disabled, checked
- Hiệu suất tốt hơn, không tạo object mới mỗi lần click
- Dễ bảo trì, đổi màu chỉ sửa XML