PeakGen
PeakGen

Reputation: 23035

Too much gap between table rows

Please have a look at the following code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".HomeScreen" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="*"
        android:layout_margin="10dp"
        android:layout_centerInParent="true"
        android:weightSum="4" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/fStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                android:gravity="center"
                android:clickable="true"
                android:background="@android:drawable/btn_default"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/rStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/sStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/cStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/aStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/lStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/oStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/eStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView10"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView10"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/hStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView11"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView11"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/dStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView12"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView12"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/aStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

        </TableRow>
    </TableLayout>

</RelativeLayout>

In WVGA 5.1 Screen, this generates the following UI

enter image description here

As you can see, there is a massive gap between rows. I want to avoid this, so it will look good in all smartphones. How can I do this?

Upvotes: 2

Views: 200

Answers (5)

GrIsHu
GrIsHu

Reputation: 23648

In your TableLayout Set the height to wrap_content and your space will be removed. android:layout_height="wrap_content"

Also to get the best result of the android:weight property set the each layout's width to 0dp wherever you have assigned a weight.

Upvotes: 0

Manishika
Manishika

Reputation: 5574

Make height & width of tableRow as fill_parent and height of every LinearLayout also as fill_parent

Use this data

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".HomeScreen" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="*"
        android:layout_margin="10dp"
        android:layout_centerInParent="true"
        android:weightSum="4" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".3"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/fStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".3"
                android:gravity="center"
                android:clickable="true"
                android:background="@android:drawable/btn_default"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/rStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/sStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/cStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/aStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/lStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/oStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/eStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView10"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView10"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/hStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView11"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView11"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/dStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight=".3"
                android:gravity="center"
                android:background="@android:drawable/btn_default"
                android:clickable="true"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/imageView12"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView12"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/aStr"
                    android:textAppearance="?android:attr/textAppearanceSmall" />

            </LinearLayout>

        </TableRow>
    </TableLayout>

</RelativeLayout>

Upvotes: 1

Ajinkya S
Ajinkya S

Reputation: 570

It's because your using linear layout in vertical and applying weight of 3

Upvotes: 0

Sivakumar S
Sivakumar S

Reputation: 681

Table row's height could be match_parent and width could be 0dip.

Upvotes: 1

Wasiq Ali
Wasiq Ali

Reputation: 261

Try setting android:layout_margin to abit less and let me know if it solves the problem.

Upvotes: 0

Related Questions