Android

[Andriod, 안드로이드]라디오 버튼(RadioButton) 태두리(buttonTint) 색상 변경방법

라디오 버튼(RadioButton) 태두리(buttonTint) 색상 변경 예제 코드

[백엔드 코드]
public class RadioButtonActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button);

RadioGroup radioGroup = findViewById(R.id.radioGroup);

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checkedRadioButton = group.findViewById(checkedId);
int checkedIndex = group.indexOfChild(checkedRadioButton); //선택된 라디오 버튼 index값
}
});
}
}

[레이아웃 XML]
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”#389fac”>
<LinearLayout
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:gravity=”left”
android:layout_margin=”20dp”
android:layout_gravity=”center_horizontal”
android:orientation=”vertical”>

<RadioGroup
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical”
android:id=”@+id/radioGroup”
android:gravity=”left”>

<RadioButton
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”라디오버튼1″
android:id=”@+id/radio_btn_1″
android:textColor=”#ffffff”/>

<RadioButton
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”라디오버튼2″
android:id=”@+id/radio_btn_2″
android:textColor=”#ffffff”/>

<RadioButton
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”라디오버튼3″
android:id=”@+id/radio_btn_3″
android:textColor=”#ffffff”/>

</RadioGroup>
</LinearLayout>
</RelativeLayout>

라디오 버튼(RadioButton) 태두리(라운드) 색상 변경은 buttonTint 속성(attribute)을 사용하여 색상을 변경할 수 있다.
<RadioButton
    android:layout_width=”wrap_content”
    android:layout_height=”wrap_content”
    android:text=”라디오버튼3″
    android:id=”@+id/radio_btn_3″
    android:buttonTint=”#ffffff”
    android:textColor=”#ffffff”/>


백엔드에서 처리하고 싶다면 아래와 같이 처리한다. buttonTint 속성 변경은 안드로이드 롤리팝(21) 이후 부터 지원된다.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   RadioButton rad1 = findViewById(R.id.radioBtn1);
   rad1.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor(SettingActivity.this, R.color.colorxml_color_49)));
}

Leave a Reply

error: Content is protected !!