webgenius
webgenius

Reputation: 904

Contents in ListView not centered

My app retrieves user details from DB and displays in the ListView. However the contents displayed in the ListView are not formatted properly.

This is how my layout is currently rendered: enter image description here

This is my layout file for the activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context=".LeaderBoard" >

    <LinearLayout
        android:id="@+id/column"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/column_header1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="@string/strLBoardHeaderRank"
            android:textColor="#f00"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/column_header2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:text="@string/strLBoardHeaderName"
            android:layout_gravity="center"
            android:textColor="#f00"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/column_header3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="@string/strLBoardHeaderScore"
            android:textColor="#f00"
            android:textSize="24sp" />
    </LinearLayout>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/column" >
    </ListView>

</RelativeLayout>

The layout file for the list activity is below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000" >

    <TextView
        android:id="@+id/rank"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:textColor="#f00"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:textColor="#f00"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:textColor="#f00"
        android:textSize="20sp" />

</LinearLayout>

Upvotes: 0

Views: 204

Answers (2)

Surender Kumar
Surender Kumar

Reputation: 1123

Try this one, this worked for me

//Main Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="horizontal" >
<TableLayout
                    android:id="@+id/add_rule_table"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="2dp"
                    android:layout_marginTop="5dp"
                    android:background="@color/list_divider"
                    android:orientation="vertical"
                    android:stretchColumns="*"
                    android:visibility="gone" >

                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/list_divider"
                        android:orientation="horizontal" >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="@color/list_divider"
                            android:ems="4"
                            android:gravity="center"
                            android:text="@string/rule_no"
                            android:textSize="12sp"
                            android:textStyle="bold" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="@color/list_divider"
                            android:ems="4"
                            android:gravity="center"
                            android:text="@string/lot"
                            android:textSize="12sp"
                            android:textStyle="bold" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="@color/list_divider"
                            android:ems="4"
                            android:gravity="center"
                            android:text="@string/discount"
                            android:textSize="12sp"
                           android:textStyle="bold" />


                    </TableRow>

                    <View
                        android:layout_width="fill_parent"
                        android:layout_height="1dp"
                        android:background="@color/list_divider" />
                </TableLayout>


                    <ListView
                        android:id="@+id/rule_list_view"
                        android:layout_width="0dip"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="2dp"
                        android:layout_weight="1"
                        android:background="@android:color/transparent"
                        android:cacheColorHint="#00000000"
                        android:choiceMode="none"
                        android:listSelector="@android:color/transparent" />

            </LinearLayout>


//Custom List View Items

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
  >

    <TableLayout
        android:id="@+id/add_rule_table"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:layout_marginTop="5dp"
        android:background="@color/white"
        android:orientation="vertical"
        android:stretchColumns="*" >

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/rule_nos"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"
                android:layout_marginRight="1dp"
                android:ems="3"
                android:gravity="center_horizontal"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:text="1"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/lots"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="4"
                android:gravity="center"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:text="23"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/discount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="4"
                android:gravity="center"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:text="34%"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/edit_rule"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/edit_rule"
                android:ems="4"
                android:gravity="center_horizontal"
                android:text="@string/edit"
                android:textColor="#ffffff"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/delete_rule"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center_vertical"
                android:background="@drawable/delete_rule"
                android:ems="4"
                android:gravity="center_horizontal"
                android:text="@string/delete"
                android:textColor="#ffffff"
                android:textSize="12sp" />
        </TableRow>
    </TableLayout>

</LinearLayout>

Upvotes: 0

M S Gadag
M S Gadag

Reputation: 2057

add android:gravity="center" for all textviews and layout file for the list activity textviews width should be 0dp because of weight property and u should also add android:orientation="horizontal"

<TextView
            android:id="@+id/column_header1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:gravity="center"
            android:text="strLBoardHeaderRank"
            android:textColor="#f00"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/column_header2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:gravity="center"
            android:text="strLBoardHeaderName"
            android:layout_gravity="center"
            android:textColor="#f00"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/column_header3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:gravity="center"
            android:text="strLBoardHeaderScore"
            android:textColor="#f00"
            android:textSize="24sp" />

layout file for the list

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/rank"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="center"
        android:textColor="#f00"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:gravity="center"
        android:textColor="#f00"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="center"
        android:textColor="#f00"
        android:textSize="20sp" />

</LinearLayout>

Upvotes: 1

Related Questions