Tyrant
Tyrant

Reputation: 60

Layout handling in Android

I have a custom list layout. The contents of list should be,

+--------------+-------------+------------+
|an image icon | text label  |a checkbox  |
+--------------+-------------+------------+

My problem is, the checkboxes are not aligned to right of the screen. It occupies the place based on the size of the text label. If the label is too big, the checkbox is never displayed / half displayed.

layout file sample as follows

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout android:id="@+id/tableLayout1"
    android:layout_width="match_parent" android:layout_height="wrap_content">
    <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView android:id="@+id/imageView1" android:src="@drawable/icon"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
        <TableLayout android:id="@+id/tableLayout2"
            android:layout_width="match_parent" android:layout_height="wrap_content">
            <TableRow android:id="@+id/tableRow5" android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView android:layout_width="wrap_content"
                    android:text="TextView" android:id="@+id/textView1"
                    android:layout_height="wrap_content"></TextView>
            </TableRow>
            <TableRow android:id="@+id/tableRow6" android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView android:text="TextView" android:id="@+id/textView2"
                    android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
            </TableRow>
            <TableRow android:id="@+id/tableRow7" android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView android:text="TextView" android:id="@+id/textView3"
                    android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
            </TableRow>
            <TableRow android:id="@+id/tableRow8" android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    android:text="x"
                    android:id="@+id/textView4" android:layout_width="wrap_content"
                    android:layout_height="wrap_content"></TextView>
            </TableRow>
        </TableLayout>
        <CheckBox android:text="" android:id="@+id/checkBox1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
    </TableRow>

</TableLayout>

</LinearLayout>

Upvotes: 0

Views: 1283

Answers (3)

Nolesh
Nolesh

Reputation: 7028

Play with layout_weight property and try this sample:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <ImageView
                android:layout_width="fill_parent"
                android:src="@drawable/icon"
                android:layout_height="wrap_content"
                android:layout_weight="1">
        </ImageView>
        <LinearLayout
                android:layout_width="fill_parent"
                android:orientation="vertical"
                android:layout_height="wrap_content"
                android:layout_weight="1">
            <TextView
                    android:layout_width="fill_parent"
                    android:text="some text 1"
                    android:layout_height="wrap_content">
            </TextView>
            <TextView
                    android:layout_width="fill_parent"
                    android:text="some text 2"
                    android:layout_height="wrap_content">
            </TextView>
        </LinearLayout>
        <CheckBox
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content">
        </CheckBox>
    </LinearLayout>
</LinearLayout>

Upvotes: 3

use this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/relativeLayout1">

        <ImageView android:id="@+id/imageView1"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:src="@drawable/icon" android:layout_alignParentTop="true"></ImageView>
        <CheckBox android:id="@+id/checkBox1"
    android:layout_alignParentTop="true" android:text="CheckBox"
    android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true"></CheckBox>
        <TextView android:text="@string/hello" android:id="@+id/TextView1"
        android:layout_toRightOf="@id/imageView1" android:layout_toLeftOf="@id/checkBox1"
    android:layout_height="wrap_content" android:layout_width="fill_parent"></TextView>

    </RelativeLayout>

Upvotes: 1

Richa
Richa

Reputation: 3193

If u fix the textview's width then checkbox will come in right, or if u insert some long text in textview then only ur CheckBox comes in right.You have given Wrap_content for width thats y it wont come to right....

Another option is that u can use Relative layout and accordingly set Components....

may my solution helpful to u.

All The Best

Upvotes: 0

Related Questions