Renjith
Renjith

Reputation: 3617

Trouble aligning TextView and Checkbox in a layout

I have a linear layout that contains a textview and checkbox.

 <LinearLayout style="@style/linearLayoutStyleNoBgColor" >
     <TextView
         android:id="@+id/useFollowupDate"
         style="@style/textFieldStyleType1WithCheckbox" />

     <CheckBox
         android:id="@+id/checkFollowDate"
         style="@style/checkboxStyleType1"/>
 </LinearLayout>

And the styles are:

<style name="linearLayoutStyleNoBgColor">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">fill_parent</item>
    <item name="android:layout_margin">5dp</item>
    <item name="android:weightSum">1.0</item>
</style>


<style name="textFieldStyleType1WithCheckbox" parent="@style/textFieldStyleType1">
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_weight">0.30</item>
    <item name="android:gravity">center_vertical</item>
</style>

<style name="checkboxStyleType1">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_weight">0.70</item>
    <item name="android:gravity">right|end</item>
</style>

What I am trying to achieve is

TextView                |     checkbox
(30%percent screen)     |     (to the right)

But I am getting now is

TextView            | checkbox
(30%percent screen) | (left aligned)

Where am I gone wrong?

Upvotes: 5

Views: 8654

Answers (4)

Krrishnaaaa
Krrishnaaaa

Reputation: 689

Styles.xml

<style name="linearLayoutStyleNoBgColor">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">fill_parent</item>
    <item name="android:layout_margin">5dp</item>
    <item name="android:weightSum">1.0</item>
</style>

<style name="textFieldStyleType1WithCheckbox">
    <item name="android:layout_width">0dip</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_weight">1</item>
    <item name="android:gravity">center_vertical</item>
</style>

<style name="checkboxStyleType1">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:gravity">right|end</item>
</style>

activity_main.xml

<LinearLayout
    style="@style/linearLayoutStyleNoBgColor"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/useFollowupDate"
        style="@style/textFieldStyleType1WithCheckbox"
        android:text="test"
        android:layout_width="wrap_content" />

    <CheckBox
        android:id="@+id/checkFollowDate"
        style="@style/checkboxStyleType1"
        android:gravity="center_vertical"
        android:text="checkbox" />

</LinearLayout>

Upvotes: 0

krishna
krishna

Reputation: 142

if you want to align them aptly then you can use relative layout. make check box to the right of the textview

<CheckBox
         android:id="@+id/checkFollowDate"
         style="@style/checkboxStyleType1"
android:layout_toRightOf="@id/useFollowupDate"
/>

Upvotes: 0

Divya Motiwala
Divya Motiwala

Reputation: 1669

Use gravity and layout_gravity properties at proper places;

This might be useful : http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/

Upvotes: 1

vaske
vaske

Reputation: 416

I think you should change android:gravity to android:layout_gravity for checkboxStyleType1

Upvotes: 0

Related Questions