I have alertdialog, in which I create Radiogroup, how can I change the color of the text and the color of the RadioButton?
Private Fun Showdialog () {
Lateinit Var Dialog: Alertdialog
Var Them_Alert = 0
Val Builder = Alertdialog.builder (this)
VAR TEXT = TextView (BaseContext)
Text.Settext ("Choose theme")
Text.Setgravity (gravity.center)
Text.SetPadding (0.40,0,0)
text.settextSize (25f)
IF (Them == 1) {
text.settextcolor (Color.Black)
text.setbackgroundcolor (Color.White)
}
ELSE {
text.settextcolor (Color.white)
Text.SetBackGroundColor (Resources.Getcolor (R.Color.pole))
}
Var Checked = 1
Builder.SetcustomTitle (Text)
Builder.SetpositiveButton (Android.r.String.yes) {
Dialog, Which- & gt;
if (Them_alert == 1) {
DARK ()
}
ELSE If (Them_alert == 2) {
Light ()
}
}
Builder.SetnegativeButton (Android.r.String.cancel) {
Dialog, Which- & gt;
}
IF (Them == 1) {
Checked = 1.
}
ELSE {
Checked = 0.
}
Val Array = Arrayof ("Dark", "Light")
Builder.SetsinglechoiceItems (Array, Checked, {_, Which- & gt;
Val Color = Array [Which]
if (Color == "Dark") {
Them_alert = 1 // Dark
}
if (Color == "Light") {
Them_alert = 2 // Light
}
})
Dialog = Builder.Create ()
IF (Them == 1) {
Dialog.GetWindow () !!. SetbackgroundDrawABlereSource (R.Color.White)
//dialog.getbutton(Dialoginterface.button_negative ).setTextColor(Color.Black)
}
ELSE {
dialog.getwindow () !!. SetbackgroundDrawABlereSource (R.Color.pole)
}
dialog.show ()
}
Answer 1, Authority 100%
To change the button color, you will need to create your use:
android: buttontint = "@ color / youur_color"
So it looks like in XML markup of your activity / fragment:
& lt; radiobutton
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id / radio"
Android: Checked = "True"
Android: beuttontint = "@ color / your_color" / & gt;
and on color in colors.xml
& lt; color name = "your_color" & gt; # E75748 & lt; / Color & GT;
In the code, this is done so programmatically:
if (build.version.sdk_int & gt; = 21)
{
ColorStatelist ColorStateList = New ColorStatelist (
NEW INT [] [] {
New int [] { -Android.r.attr.state_enabled}, // disabled
New int [] {android.r.attr.state_enabled} // Enabled
},
NEW INT [] {
Color.Black // disabled.
, Color.Blue // Enabled
}
);
Radio.SetButtontintList (ColorStatelist); // Set The Color Tint List
Radio.Invalidate (); // COULD NOT BE NECESSARY
}
You can also create a button for text text:
& lt; selector xmlns: android = "http://schemas.android.com/apk/res/android" & gt;
& lt; Item Android: state_pressed = "True" Android: color = "# 0F0" / & gt;
& lt; item Android: state_checked = "true" android: color = "# FFF" / & gt;
& lt; Item Android: Color = "# 00F" / & gt;
& lt; / selector & gt;
and connect it to the button:
& lt; radiobutton
Android: layout_width = "wrap_content"
Android: Checked = "False"
Android: layout_height = "wrap_content"
Android: TextColor = "@ Color / RadioButtonState" / & gt;
both methods work well, the simplest is buttonTint
. To change the color of the text, use the textColor in the xml markup. Here has a question on your topic and here another one.