4

I have checkbox code in a CardView layout file. The CardView has a white background. Normally, I think the unchecked Checkbox is a black square. My layout shows no blank checkbox. All I see is just the white CardView background (top CardView in the screenshot). When I click on the right-most area of the CardView where the checkbox code is formatted, a green Checkbox appears (bottom CardView in the screenshot). What am I missing here? enter image description here.

layout file:

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/background4main"  >

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/singlecard_view1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="@android:color/white"
    card_view:cardCornerRadius="6dp"
    android:orientation="horizontal"
    android:layout_margin="4dp">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:selectableItemBackground"  >

    <TextView
        android:id="@+id/cardBlankText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="todo"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="20sp"  />

    <TextView
        android:id="@+id/cardBlankText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cardBlankText2"
        android:text="note1"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="20sp"  />

    <CheckBox
        android:id="@+id/chkSelected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"  />

    </RelativeLayout>

</android.support.v7.widget.CardView>

</LinearLayout>

Adapter file:
...
public class ListViewHolder extends RecyclerView.ViewHolder {

    TextView cardBlankText2;
    TextView cardBlankText3;
    CheckBox chkSelected;

    public ListViewHolder(View itemView) {
        super(itemView);

        cardBlankText2 = (TextView)itemView.findViewById(R.id.cardBlankText2);
        cardBlankText3 = (TextView)itemView.findViewById(R.id.cardBlankText3);
        chkSelected = (CheckBox) itemView.findViewById(R.id.chkSelected);
    }
...
@Override
public void onBindViewHolder(final ListViewHolder holder, final int position) {

        holder.cardBlankText2.setText(dbList.get(position).getTodo());
        holder.cardBlankText3.setText(dbList.get(position).getNote1());
        holder.chkSelected.setChecked(dbList.get(position).isSelected());
        holder.chkSelected.setTag(dbList.get(position));
}
AJW
  • 1,578
  • 3
  • 36
  • 77

3 Answers3

7

Use below code :

<android.support.v7.widget.AppCompatCheckBox
                android:id="@+id/settings_notification_checkbox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                app:buttonTint="@color/colorPrimary"/>

Instead of this :

<CheckBox
        android:id="@+id/check"
        android:button="@drawable/customdrawablecheckbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
Narendra
  • 1,010
  • 1
  • 12
  • 11
2

Drawable customcheckbox.xml:

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

and your xml file:

<CheckBox
android:id="@+id/check"
android:button="@drawable/customdrawablecheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Ramesh Bhati
  • 1,239
  • 16
  • 25
1

Possible reasons:

  • Your checkbox could be in white color.

  • It's VISIBILITY may be gone

Also add toLeftOf attribute for the textview's so that they may not overlap with the checkbox.Check your theme for the color of checkbox.If that does not help you can put drawable ?android:attr/listChoiceIndicatorMultiple for unchecked state

Ravi Theja
  • 3,371
  • 1
  • 22
  • 34
  • ok I will try. How do I check what color the checkbox will be for my theme? – AJW Apr 19 '16 at 05:12
  • 1
    @RT "textColorSecondary" in my theme did the trick. Answer upvoted and accepted. Cheers. – AJW Apr 19 '16 at 05:53