Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align CheckBox to the top of its description in Android

I would like to align CheckBox "symbol" to the top of its description.

What I have now: before

What I want to have: after

Current xml:

<CheckBox android:id="@+id/register_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/register_hint"/>

I've tried to achieve this by manipulating layout_gravity attributes, but it doesn't work.

like image 841
fragon Avatar asked Jan 07 '16 16:01

fragon


4 Answers

android:gravity="top" will fix the problem:

<CheckBox android:id="@+id/register_checkbox"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="@string/register_hint"           android:gravity="top"/> 

Note that android:gravity is different than android:layout_gravity.

like image 62
heloisasim Avatar answered Sep 20 '22 21:09

heloisasim


  • use

    android:gravity="top" it will pin the box on top

  • use

    padding_top="4dp" will adjust Text padding only (without changing box padding from top)

like image 26
Abdul Samad Avatar answered Sep 22 '22 21:09

Abdul Samad


I found that:

 android:gravity="fill"

works better than "top". Top wouldn't look right if the checkbox may also end up as only one line.

like image 29
Flyview Avatar answered Sep 23 '22 21:09

Flyview


You can try this code:-

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false">
        <layer-list>
            <item android:top="2dp">
                <bitmap android:gravity="center" android:src="@drawable/checkbox" />
            </item>
        </layer-list>
    </item>
    <item android:state_checked="true">
        <layer-list>
            <item android:top="2dp">
                <bitmap android:gravity="center" android:src="@drawable/checkboxselected" />
            </item>
        </layer-list>
    </item>
    <item>
        <layer-list>
            <item android:top="2dp">
                <bitmap android:gravity="center" android:src="@drawable/checkbox" />
            </item>
        </layer-list>
    </item>
</selector>
like image 26
Abhijeet Sharma Avatar answered Sep 22 '22 21:09

Abhijeet Sharma