이 튜토리얼에서는 Android 앱에서 체크박스(CheckBox)의 색상을 변경하는 방법을 단계별로 살펴봅니다. 예제에서는 버튼을 클릭했을 때 체크박스의 텍스트 색상과 배경색이 함께 변경되도록 구현합니다.
1단계: 새 프로젝트 생성
Android Studio에서 File ⇒ New Project를 선택하고, 새 프로젝트 생성에 필요한 모든 항목을 입력하여 프로젝트를 만듭니다.
2단계: 레이아웃 파일 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다. TextView, CheckBox, Button을 배치하여 화면을 구성합니다.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click the check box" /> <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Check box" android:layout_below="@id/tv" android:paddingRight="15dp"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Change Color" android:layout_below="@id/checkbox" /> </RelativeLayout>
3단계: MainActivity 코드 작성
src/MainActivity.java에 다음 코드를 추가합니다. 버튼 클릭 이벤트 발생 시 setTextColor() 메서드로 텍스트 색상을 빨간색으로, setBackgroundColor() 메서드로 배경색을 변경합니다.
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkBox.setTextColor(Color.RED);
checkBox.setBackgroundColor(Color.parseColor("#cbff75"));
}
});
}
}
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>
앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 Android 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤 툴바에서 Run 아이콘을 클릭하세요. 실행 대상으로 모바일 기기를 선택하면, 기기 화면에 앱의 기본 화면이 표시됩니다.


참고: API 21 이상에서는 buttonTint 활용
위 예제는 체크박스의 텍스트와 배경색을 변경하는 방식입니다. 만약 체크 표시(체크 마크) 자체의 색상을 변경하고 싶다면, Android 5.0(API 21) 이상에서는 XML의 android:buttonTint 속성을 사용하는 것이 더 간편합니다.
<CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Check box" android:buttonTint="@color/colorAccent" />