0

I've got a problem here. I needed to adjust the custom 4-checkboxes in the center of the linear layout. Already tried with gravity, layout_weight, setting gravity within the xml item. However, the button is always aligned to the left. Here is the pattern of views I've implemented:

<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"
tools:context="">

<TextView
    android:id="@+id/tv_amount_tickets"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="16dp"
    android:text="@string/select_amount_tickets"
    android:textSize="16sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/til_amount_tickets"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:inputType="number"
    android:imeOptions="actionDone"
    android:maxLines="1"
    android:hint="@string/amount_placeholder"
    app:layout_constraintEnd_toEndOf="@+id/tv_amount_tickets"
    app:layout_constraintStart_toStartOf="@+id/tv_amount_tickets"
    app:layout_constraintTop_toBottomOf="@+id/tv_amount_tickets"
    />

<TextView
    android:id="@+id/tv_ticket"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:textSize="16sp"
    android:textStyle="bold"
    android:text="@string/tv_ticket_type"
    app:layout_constraintEnd_toEndOf="@+id/til_amount_tickets"
    app:layout_constraintStart_toStartOf="@+id/til_amount_tickets"
    app:layout_constraintTop_toBottomOf="@+id/til_amount_tickets" />

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:gravity="center"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="@+id/tv_ticket"
    app:layout_constraintStart_toStartOf="@+id/tv_ticket"
    app:layout_constraintTop_toBottomOf="@+id/tv_ticket">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:foregroundGravity="center"
        android:button="@drawable/checkbox_circle_check_selector" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:foregroundGravity="center"
        android:button="@drawable/checkbox_circle_check_selector" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:foregroundGravity="center"
        android:button="@drawable/checkbox_circle_check_selector" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:foregroundGravity="center"
        android:button="@drawable/checkbox_circle_check_selector" />

</LinearLayout>

selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/checkbox_circle_check"
    android:state_checked="false"/>
<item android:drawable="@drawable/checkbox_circle_check_selected"
    android:state_checked="true"/>
<item android:drawable="@drawable/checkbox_circle_check"/>

normal checkbox

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="3dp" />
        <solid android:color="#FFFFFFFF" />
        <stroke android:width="0.5dp" android:color="#FF8492A6"/>
    </shape>
</item>
<item android:drawable="@drawable/ic_circle_check" />

selected checkbox

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="3dp"/>
        <solid android:color="@color/colorPrimary"/>
        <stroke android:width="0.5dp" android:color="#FF8492A6"/>
    </shape>
</item>
<item android:drawable="@drawable/ic_circle_check_white" />

Thomas Marques
  • 981
  • 2
  • 7
  • 16
  • You have added constraints on linearLayout alignment corresponding to "tv_ticket". And width of "tv_ticket" is wrapcontent. What is the width of "tv_ticket" – Ravi Mar 13 '19 at 20:59
  • The width to tv_ticket is all space available minus some margin dps, it is set to zero because I am using constraint layout. – Thomas Marques Mar 13 '19 at 21:05

2 Answers2

1

To center your checkboxes, you can try changing their parent LinearLayout width to android:layout_width="wrap_content", especially when it is a child of ConstraintLayout, and add android:layout_gravity="center" if it's not.

<LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content" // instead of match_parent
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_gravity="center" // instead of android:gravity
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="@+id/tv_ticket"
        app:layout_constraintStart_toStartOf="@+id/tv_ticket"
        app:layout_constraintTop_toBottomOf="@+id/tv_ticket">
Aaron
  • 3,764
  • 2
  • 8
  • 25
  • Did not work. I modified the structure but still unsuccessfully https://stackoverflow.com/questions/55187932/align-checkbox-button-to-center-of-view – Thomas Marques Mar 15 '19 at 17:40
1

Why do you use LinerLayout here? You could remove it and use constrainlayout chain tools for keeping a flat view hierarchy. Check this: https://constraintlayout.com/basics/create_chains.html

Vladyslav Ulianytskyi
  • 1,401
  • 22
  • 22