Annie
Annie

Reputation: 2587

Checkbox with multiline text

I want to align checkbox at top with two line text. I want Checkbox and text in the same line at top

Note : I have tried android:gravity="top" but getting some extra padding inside text of checkbox.

I have tried : How to align CheckBox to the top of its description in Android but it didn't worked.

I have tried :

<CheckBox
     android:id="@+id/chkConfirmSymbol"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_marginLeft="@dimen/margin_10"
     android:layout_weight="1"
     android:button="@drawable/selector_checkbox_black"
     android:enabled="false"
     android:gravity="top"
     android:paddingTop="0dp"
     android:paddingLeft="@dimen/margin_10"
     android:text="@string/at_least_1_symbol"
     android:textColor="@drawable/selector_checkbox_text_black"
     android:textSize="@dimen/font_11" />

Output :

enter image description here

As per the image above i am getting some padding in the text.

Any help would be appreciated. Thank you!

Upvotes: 0

Views: 3601

Answers (4)

Secko
Secko

Reputation: 7716

This is basically how you do it:

<CheckBox
    android:layout_width="match_parent"
    android:minLines="2"

This would still get scaled into a single line on tablet (landscape) screens.

Upvotes: 1

karan
karan

Reputation: 8843

You can find default drawable file used for checkbox in sdk > platforms > API_VERSION > data > res > drawable

If you check any of resource image used with default drawable, for eg btn_check_off used in this drawable you can see that the default image is having some default padding in image itself. And the custom image that you are using is missing it. Add default padding to your image resource and it should work fine.

See below image for reference. enter image description here

Upvotes: 2

Nitin Gurbani
Nitin Gurbani

Reputation: 1240

I have just managed it by giving some android:paddingTop, you can also try to do it in the same way.

XML Code (for layout):

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:id="@+id/cb_confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:enabled="false"
        android:paddingTop="5dp"
        android:paddingStart="10dp"
        android:paddingEnd="0dp"
        android:text="@string/at_least_1_symbol"
        android:textColor="@android:color/background_dark"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

Screenshot:

enter image description here

I hope, this helps you.

Upvotes: 2

Rumit Patel
Rumit Patel

Reputation: 12459

Can't you do it simple way? why you needed android:layout_width="0dp" and android:layout_weight="1"?

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:text="At least 1 symbol(!@#$%^&*)"
    android:textSize="20sp" />

Or you can have look at this answer. it should help you.

Upvotes: 3

Related Questions