Change color of a radio button

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

I am developing an quiz based app. There will be 1 question and 4 option(radio buttons). If user select any wrong answer then I want to turn that radio button color to Red. How to do this?

回答1:

Just came to show something that really help me with this:

Everyone talks about how to use the tint and how to use the colorAccent, but, this wont work on phones with API less than 21.

So, the real fix on this or at least what helped me was to use android.support.v7.widget.AppCompatRadioButton instead of RadioButton

With this on your layout, you can use: app:buttonTint="@color/yourColor"

without getting warnings or problems about the compat of the view.

And, don't you forget about adding:

xmlns:app="http://schemas.android.com/apk/res-auto"

to your layout parent or to your widget.

Edit:

@aselims mention on a comment that there's not buttonTintin the app namespace.

So... here's my current style for this solution:



回答2:

The fastest thing to do is to set the buttonTint to your desired color:

 

In your values/colors.xml put your color in this case a reddish one:

#e75748

Result:

As @smashing pointed, this only will work on API level >= 21



回答3:

To change RadioButton button colour programmatically, and works on api level AppCompatRadioButton instead of RadioButton:

(otherwise will warn setbuttontintlist requrie api level 21)

single_choice_state_list.xml



回答4:

This site is really good for customizing Android components in general: android-holo-colors

Just choose the radio button, make the color red, download and use it in your project!



回答5:

Create a selector drawable for you radio button under drawable/radio_button.xml folder and mention all the required states for your radio button.

And specify android:button="@drawable/radio_button" for your radio button

Dont forget to add the corresponding images for different states of radio button.



回答6:

//get radio button reference from layout RadioButton raPrivate = (RadioButton) layout.findViewById(R.id.radioPrivate); //parse textColor from string hex code int textColor = Color.parseColor("#000000"); //set textcolor to radioButton raPrivate.setButtonTintList(ColorStateList.valueOf(textColor)); 

u can only assing ColorStateList objets as color for the radioButton, if u use valueOf it will only use one color.

Hope this helps :>



回答7:

You can perform a backwards compatible tint on the radio button

XML:

Or java:

CompoundButton button = (CompoundButton) findViewById(R.id.radioButton); CompoundButtonCompat.setButtonTintList(button, ContextCompat.getColorStateList(this, R.color.red)); 


回答8:

Create an image !like this and place it in your drawable folders.. call it by,

 RadioButton rb=(RadioButton) findViewById(R.id.radioButton1);         rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {              @Override             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                 // TODO Auto-generated method stub                 rb.setButtonDrawable(R.drawable.'you image here');             }         });     } 


回答9:

Hope this helps..

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        radioButton.setButtonTintList(ContextCompat.getColorStateList(mContext, R.color.colorGris));     }else {//Do something if you have a lower version} 

For me its working.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!