Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: why does Checkbox not show empty checkbox for unchecked state?

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));
}
like image 914
AJW Avatar asked Jan 07 '23 05:01

AJW


2 Answers

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" />
like image 94
Narendra Avatar answered Jan 08 '23 18:01

Narendra


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" />
like image 42
Ramesh Bhati Avatar answered Jan 08 '23 19:01

Ramesh Bhati