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?

dev_android
  • 8,698
  • 22
  • 91
  • 148

4 Answers4

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

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>
Adeel
  • 2,901
  • 7
  • 24
  • 34
Yograj Shinde
  • 839
  • 12
  • 23
-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>
gavlaaaaaaaa
  • 612
  • 2
  • 7
  • 11
  • 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. – dev_android Aug 04 '14 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 – gavlaaaaaaaa Aug 04 '14 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. – dev_android Aug 05 '14 at 04: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 – gavlaaaaaaaa Aug 05 '14 at 06: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. – dev_android Aug 05 '14 at 10:24
  • I understand what you're saying, but this isn't necessarily true as views are reused as you scroll to solve this problem, so only the views on the page plus a few extras are drawn. In addition, this allows you to change the position of the text box, its size in isolation of the text size etc... I guess we will have to agree to disagree, both of these solutions here are equally as effective - your chosen solution is more simple, however in regards to your quality issue, for adaptable code thats modularised and easy to edit in the future, this solution is of the same if not better quality. – gavlaaaaaaaa Aug 05 '14 at 10:28
-1

Simply use this :

android:buttonTint="#FFFFFF"

** replace #FFFFFF with any color

Han
  • 575
  • 8
  • 21