0

I want make a list of Checkbox with white text in black background. We are using following code:

CheckBox chkAdditionalPack = new CheckBox(MainActivity.this);
chkAdditionalPack.setTag(j);
chkAdditionalPack.setText(offerPackageListForAddl.get(j).getOfferPackageName().toString());
chkAdditionalPack.setTextColor(Color.WHITE);

It gives a view like below: checkbox in black background

The problem is now boxes of checkboxes is not clearly visible. How can I make it clearly visible keeping intact other parts?

0

4 Answers 4

8

If you are using android-studio, select your checkbox, and in the properties, set 'buttonTint' property value to whatever color you want using the editor. Or, if you prefer the XML solution, use

android:buttonTint="@color/white"

Note: This assumes that the color white is defined.

2
  • it required above 21 , What for below 21 Jun 4, 2016 at 14:16
  • Below 21 use: tools:buttonTint="@color/white"
    – M. Marmor
    Jul 17, 2018 at 11:11
2

try the below xml code ref from solution

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_checked="true" 
        android:drawable="@drawable/cbchk_blue"
        android:state_focused="false" >
    </item>

    <item
        android:state_checked="true" 
        android:drawable="@drawable/cbchk_blue"
        android:state_focused="true" >
    </item>

    <item
        android:state_checked="false" 
        android:drawable="@drawable/cbunchk_blue"
        android:state_focused="false" >
    </item>

    <item
        android:state_checked="false" 
        android:drawable="@drawable/cbunchk_blue"
        android:state_focused="true" >
    </item>
</selector>
-1

One really simple way to do it, within the xml for the checkbox, simply set the android:background variable to the colour you wish.

Instead of having the text and check box created together like this, simply create a custom list item layout xml file.

e.g custom_list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000000"

        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        />


</LinearLayout>
6
  • Not getting the idea. Checkbox and adjacent text totally come under same view CheckBox. If you set black background, it will look same as it is in the image. Aug 4, 2014 at 13:13
  • @dev_android see my edit, the trick is to create a custom list item layout in an xml file and not create it programatically, then you have the freedom to decide the colour for separate components Aug 4, 2014 at 13:25
  • For CheckBox, U never need any TextView at all. If you use it, it will not change checked status on click on text. Very bad idea. Aug 5, 2014 at 4:47
  • @dev_android if you want flexibility and customisation of your list items then this is a good idea. Of course you could do all this with just the check box, but that doesnt help you with your colour problem as your checkbox and text are still one item, thats why you split them into two components. You have to implement this properly, so that onclick of the list item the check box is ticked etc... I understand this is extra work, but otherwise youre going to struggle to change the colour of only the check box Aug 5, 2014 at 6:19
  • No struggle at all...just write a selector drawable and set it to the button of checkbox. Not only OnClick action, think of the layout load, if you have 200 rows, layout have to hold 400 views instead of 200 with higher memory consumption. To write a program does not means just meet the requirement only, meet the same with quality. Aug 5, 2014 at 10:24
-1

Simply use this :

android:buttonTint="#FFFFFF"

** replace #FFFFFF with any color

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.