Search code examples
androidcheckboxviewdrawandroid-xml

Why checkbox rectangle is by far smaller than the container that contains it?


Can understand why the rectangle that get color (when checked) and uncolored (when unchecked) isn't take all the place (please find attached screenshot). Try to investigate the issue but didn't come to any conclusion. Hope that someone can help me here The layout xml is:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_bright"
    tools:context=".MainActivity">


   <TextView
       android:id="@+id/textViewww"
       android:layout_width="150dp"
       android:layout_height="80dp"
       android:text="Just For Try"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       android:gravity="center"
       android:background="#34ab56"
       android:layout_margin="15dp"/>

    <Button
        android:id="@+id/button_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Draw Again On TextView"
        app:layout_constraintTop_toBottomOf="@id/textViewww"
        app:layout_constraintStart_toStartOf="parent"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintTop_toBottomOf="@id/button_b"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="15dp"/>

    <androidx.appcompat.widget.AppCompatCheckBox
        android:id="@+id/checkbox_checkbox"
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toBottomOf="@id/textView2"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="20dp" />


</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here


Solution

  • The CheckBox size is based on the bounds of the drawable as far as my research could tell, so when you specify

            android:layout_width="42dp"
            android:layout_height="42dp"
    

    you're actually over-sizing the view but not the contents, if that makes sense.

    Use wrap_content to get the snug fit again, or supply your own custom drawable that supports 42dp width/height.