-4

I have still some difficulties with Android layouts, so here is what I want to do. On the same line (a view), I want to place 3 textviews, let's call them date, name and value.

Here is what I want it to look like :

enter image description here

Of course date is 12/03/2013, name is SOGEFFRP and value is + 45,23.

What I don't know how to do is how to get the value aligned at the top right of the line, and the name occupying all the remaining place but without erasing it. I tried to set gravity/layout_gravity to right for value but it doesn't work.

Kitkat
  • 77
  • 16
Rob
  • 15,732
  • 22
  • 69
  • 107

4 Answers4

2
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

The second TextView should have it's layout_width match_parent and layout_weight=1

Eddy
  • 489
  • 4
  • 10
  • if you use weight you should give the width as `0dp` when use `orientation = horizontal` see [`this`](http://stackoverflow.com/questions/4986861/android-layout-weight) –  Jan 13 '14 at 10:47
  • yeah..you are right, i used to fix those problems by fixing lint alerts. – Eddy Jan 13 '14 at 10:53
1

Take a Relativelayout as parent.

Add a Textview holding value with the property android:layout_alignParentRight="true"

Add one Textview with the property android:layout_alignParentLeft="true"

and one other TextView with the property android:layout_toRightOf=" the id of the date textview simple".

  • 1
    you also should add leftOff to the id value textview and match_parent to name textView width,overwise your name textview will go over value textview in case you have a long name :) – Eddy Jan 13 '14 at 10:55
0

try this:

<LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
 <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
            android:layout_weight="1"
        >
    <TextView
                    android:text:"Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"/>
        <TextView
                     android:text:"Date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"/>
    </LinearLayout>

    <TextView
             android:text:"value"
                    android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"/>

    </LinearLayout>
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
0

Brother use this layout:

 <?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="match_parent"
        android:orientation="horizontal"
        android:background="#fff"
        android:padding="5dp" >


        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="12/03/2013"
            android:gravity="center_vertical"
            android:padding="5dp"
            />


            <TextView 
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="SOGEFFRES"
            android:gravity="left"
            android:padding="5dp"
            />


                <TextView 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="+ 45,23"
             android:padding="5dp"
             android:gravity="center"
            />

    </LinearLayout>
Ravind Maurya
  • 977
  • 15
  • 24