Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

How to programatically set the colour of a RadioButton Android Circle

I want to change the actual circle of the RadioButton.

I have looked all over StackOverflow and nothing seems to work.

I am using API 17 and cannot use a ColorStateList. Custom drawables seem to mess us the look and feel of things.

I want the same effect as changing the button hint but I need to do it programmatically.

I'm sure I must please missing something very simple!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How to programatically set the color of a RadioButton Android

That is really non-specific, so here are the most relevant I found:

RadioButton rad;//initialize first!
//You can set the background color
rad.setBackgroundColor(Color.BLUE);
//Text color
rad.setTextColor(Color.WHITE);
//or highlight color
rad.setHighlightColor(Color.GREEN);

The highlight color is the color that appears when you press and hold the RadioButton(default is yellow-ish)

EDIT:

In the initialization of the radio button, call it an AppCompatRadioButton instead

AppCompatRadioButton rad = ....
rad.setHighlightColor(Color.GREEN);

Reworked from: https://stackoverflow.com/a/32472971/6296561

EDIT

Try this:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">


    </RadioGroup>

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


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

        /**
         * First Radio Button
         */
        RadioButton RB1= (RadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);//initialize and set content
        RB1.setText("RB1");
        radioGroup.addView(RB1);//add the radiobutton to the radiogroup defined in the layout

        /**
         * Second Radio Button
         */
        RadioButton RB2 = (RadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);//initialize and set content
        RB2.setText("RB2");
        radioGroup.addView(RB2);//add the radiobutton to the radiogroup defined in the layout
    }
}

custom_radiobutton.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:buttonTint="@color/colorPrimary"
    android:text="">
<!-- leave empty -->
</RadioButton>

NOTE: buttonTintonly works in API 21+. (Untested) you can change RadioButton to AppCompatRadioButton. (It is untested so I am not sure if it works on api 20 and lower)

<?xml version="1.0" encoding="utf-8"?>
<AppCompatRadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:buttonTint="@color/colorPrimary"
    android:text="">
<!-- leave empty -->
</AppCompatRadioButton>

Documentation about AppCompatRadioButton

If you use AppCompatRadioButton, I think you also have to use AppCompatRadioGroup and edit the creation of the ACRB's to:

AppCompatRadioButton RB1 = (AppCompatRadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);//initialize and set content
RB1.setText("RB1");
radioGroup.addView(RB1);//add the radiobutton to the radiogroup defined in the layout

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...