Michael Brooks
Michael Brooks

Reputation: 489

how can I align a listview correctly under TextViews

I am trying to line up the 6 fields in a list view I have with the 6 label fields that reside at the top of the screen. I have made all the text the same size, the maxEms the same size and the layout weight all the same. My problem is that the list view does not align all the way with the left of the screen. It appears to have some default left margin built in. I may be able get rid of the weights and set manual margins onmy labels to make everything lineup but I think that could be a nightmare when different size screens come into play.

Here is that first layout for my screen (this layout contains the TextViews which represent the labels for my listView. The linear layout sits inside of a relative layout )

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:layout_below="@+id/dateDisplayTextView"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:maxEms="2"
        android:text="#1"
        android:textColor="@color/green"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:maxEms="2"
        android:text="#2"
        android:textColor="@color/green"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:maxEms="2"
        android:text="#3"
        android:textColor="@color/green"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:maxEms="2"
        android:text="#4"
        android:textColor="@color/green"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:maxEms="2"
        android:text="#5"
        android:textColor="@color/green"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:maxEms="2"
        android:text="PB"
        android:textColor="@color/red"
        android:textSize="16sp" />

</LinearLayout>

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_above="@+id/emailButton1"
    android:layout_below="@id/linearLayout1" >

</ListView>

 <Button
    android:id="@+id/emailButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="Back" />

 <Button
     android:id="@+id/emailButton1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:onClick="emailTickets"
     android:layout_alignParentRight="true"
     android:text="Email tickets to contacts" />

and here is the layout that gets expanded inside my custom adapter

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >


<TextView
    android:id="@+id/number1TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxEms="2"
    android:layout_weight="1"
    android:textSize="16sp"
    android:text="TextView" />

<TextView
    android:id="@+id/number2TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxEms="2"
    android:layout_weight="1"
    android:textSize="16sp"
    android:text="TextView" />

<TextView
    android:id="@+id/number3TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxEms="2"
    android:layout_weight="1"
    android:textSize="16sp"
    android:text="TextView" />

<TextView
    android:id="@+id/number4TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxEms="2"
    android:layout_weight="1"
    android:textSize="16sp"
    android:text="TextView" />

<TextView
    android:id="@+id/number5TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxEms="2"
    android:layout_weight="1"
    android:textSize="16sp"
    android:text="TextView" />

<TextView
    android:id="@+id/numberPBTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxEms="2"
    android:layout_weight="1"
    android:textSize="16sp"
    android:text="TextView" />

</LinearLayout>

And here is image of my layout running demonstrating my issue

Screen shot

I tried to adjust the left margin of the linear layout that my label fiends reside but it had no impact. I apologize about the long post, I just wanted to make sure I presented all the relevant info. Thanks in advance

Upvotes: 1

Views: 907

Answers (3)

Manishika
Manishika

Reputation: 5574

Try giving your textViews for custom adapter gravity as left

 android:gravity="left"

Upvotes: 0

jboi
jboi

Reputation: 11912

The combination of:

    android:layout_width="wrap_content"
    android:layout_weight="1"

means, that the text field has at least the size of the content and than, whatever space is left, is evenly distributed between the fields. Having different content, will lead to different widths of the text fields. The android:maxEms doesnt change that as it defines the maximum width, the field can be of less width.

Two possible definitions that will lead to the right size. Take that code for each text filed in the header and the lines:

1. Let Android figure out the right width:

<TextView
    android:id="@+id/..."
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textSize="16sp"
    android:text="TextView" />

Setting the width to 0 means, that all space is left and evenly (weighted by layout_weight) distributed.

2. Set the width of a field via ems

<TextView
    android:id="@+id/..."
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ems="2"
    android:textSize="16sp"
    android:text="TextView" />

The ems define the width. Again, the rest of the space will be distributed.

Upvotes: 2

AndyBoy
AndyBoy

Reputation: 594

you can use

android:layout_padding

this can be useful for you.

Upvotes: 0

Related Questions