스위치는 직관적으로 ON/OFF 상태를 표시할 수 있다.
<Switch
android:id="@+id/transDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:fontFamily="@font/elice_regular"
android:text="다크모드"
android:textColor="#3F51B5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/listView" />
스위치를 사용해 간단한 다크모드를 구현하고자 하였다.
Q. 리스너를 등록해 이벤트를 적용하려면?
1. OnCheckedChangeListener인터페이스 구현
2. setOnCheckedChangedListener() 에서 리스너 등록
// 스위치 -> 다크모드 활성화
transWB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(transWB.isChecked()) {
transPoint = "BLACK";
main.setBackgroundColor(Color.BLACK);
tvTitle.setTextColor(Color.WHITE);
listView.setBackgroundColor(Color.BLACK);
btnPartremove.setBackgroundColor(0xFF00293C);
btnPartremove.setTextColor(Color.WHITE);
}
else {
transPoint = "WHITE";
main.setBackgroundColor(Color.WHITE);
tvTitle.setTextColor(Color.BLACK);
listView.setBackgroundColor(Color.WHITE);
btnPartremove.setBackgroundColor(0xFFDAEDF6);
btnPartremove.setTextColor(Color.BLACK);
}
adapter.notifyDataSetChanged();
}
});