1

How do i define a following layout in my xml file:

alt text

I hope you got it from image that i want to display TextView1 on 1st Row, and in 2nd Row, TextView2 on Left and TextView3 on Right side.

How do i define this layout?

i know the layout_gravity attribute but not getting success, so pls share your code

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295

4 Answers4

2

I think this solve your problem

alt text

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">
    <TableLayout android:id="@+id/TableLayout01"
        android:layout_height="wrap_content" android:layout_width="fill_parent"
        android:stretchColumns="0">
        <TableRow android:id="@+id/TableRow01" android:layout_height="wrap_content"
            android:layout_width="fill_parent">
            <TextView android:text="@+id/TextView01" android:id="@+id/TextView01"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

</TableRow>
        <TableRow android:id="@+id/TableRow02" android:layout_height="wrap_content"
            android:layout_width="fill_parent">
            <TextView android:text="@+id/TextView03" android:id="@+id/TextView03"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
            <TextView android:text="@+id/TextView04" android:id="@+id/TextView04"
                android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        </TableRow>
    </TableLayout>
</LinearLayout>

new solution work fine i think for you

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

<TextView android:text="@+id/TextView01" android:id="@+id/TextView01" android:layout_height="wrap_content" android:layout_width="fill_parent"></TextView>
    <LinearLayout android:id="@+id/LinearLayout02"
        android:layout_height="wrap_content" android:layout_width="fill_parent">
        <TextView android:text="@+id/TextView02" android:id="@+id/TextView02"
            android:layout_height="wrap_content" android:layout_width="fill_parent"
            android:layout_weight="1"></TextView>
        <TextView android:text="@+id/TextView03" android:id="@+id/TextView03"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    </LinearLayout>
</LinearLayout>

k i got perfect one now alt text

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

<TextView android:text="@+id/TextView01" android:id="@+id/TextView01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginRight="15dip"></TextView>
    <LinearLayout android:id="@+id/LinearLayout02"
        android:layout_height="wrap_content" android:layout_width="fill_parent">
        <TextView android:text="@+id/TextView02" android:id="@+id/TextView02"
            android:layout_height="wrap_content" android:layout_width="fill_parent"
            android:layout_weight="1"></TextView>
        <TextView android:text="@+id/TextView03" android:id="@+id/TextView03"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    </LinearLayout>
</LinearLayout>
ud_an
  • 4,939
  • 4
  • 27
  • 43
  • thanx, 2nd row is fine but 1st row is not showing in FULL_LENGTH, it should actually cover-up FILL_PARENT (full area) length, Textview01 and Textview03 is showing up of same length, just check the image that i have uploaded where it is clearly shown that Textview01 is having the full-length – Paresh Mayani Oct 01 '10 at 07:18
  • check my image for the TextView01 length – Paresh Mayani Oct 01 '10 at 07:20
  • in my new answer first textview size will be full otherwise you have to define size i think – ud_an Oct 01 '10 at 07:30
0

For example, the following layout declares what you need:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
    <TextView
        android:text="@+id/TextView01"
        android:padding="3dip" />

</TableRow>

<TableRow>
    <TextView
        android:text="@+id/TextView02"
        android:padding="3dip" />
    <TextView
        android:text="@+id/TextView03"
        android:gravity="right"
        android:padding="3dip" />
</TableRow>
</TableLayout>
uthark
  • 5,333
  • 2
  • 43
  • 59
  • thanx, this creates the layout where in 2nd row, textview2 and textview3 are coming side-by-side whereas i wants to have textview3 on right-side in 2nd row – Paresh Mayani Oct 01 '10 at 06:28
  • Aa, I see it. Then you can do it with table layout. I updated code. – uthark Oct 01 '10 at 06:46
  • updated layout code gives me the correct solution but there is a minor mistake you have done, so not getting exact solution according to the image, your code defines the layout in which TextView3 has the largest area whereas in my image, it is clearly shown that TextView2 has the largest area – Paresh Mayani Oct 01 '10 at 06:58
  • @Paresh You can define width to what you need. – uthark Oct 01 '10 at 07:31
0

The Layout file looks like below:

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:text="@+id/TextView01" android:id="@+id/TextView01"
            android:layout_width="fill_parent" android:layout_height="wrap_content" />

        <TextView android:text="@+id/TextView02" android:id="@+id/TextView02"
            android:layout_below="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentLeft="true" />
        <TextView android:text="@+id/TextView03" android:id="@+id/TextView03"
            android:layout_below="@+id/TextView01" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentRight="true" 
 android:layout_toRightOf="@+id/TextView02"/>
    </RelativeLayout>
Praveen
  • 90,477
  • 74
  • 177
  • 219
  • Solution is good, but your use two layout objects. And Google recommends to use as less layouts, as possible (http://developer.android.com/resources/articles/layout-tricks-efficiency.html). But I'm not sure, if my answer with one layout, but which is TableLayout is better or not of two linear/relative laouts. – uthark Oct 01 '10 at 06:51
  • solution is correct but just see the image where 2nd Row is divided amongst 2 Textviews, in that TextView2 is having largest area and in rest of the area TextView3 should be appeared at right-side, i think you got it what i am trying to say – Paresh Mayani Oct 01 '10 at 06:55
  • @uthark: Okay then. I made a change in your code. just add a `layout_alignParentRight` and `Layout_alignParentLeft` attributes on the Text view 2 & 3. Now i achieved it in a Single Layout. – Praveen Oct 01 '10 at 07:18
0

Finally, i have implemented with belox xml code, its working fine for me:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="6dip"
  android:orientation="vertical">

 <LinearLayout android:id="@+id/LinearLayout01" 
               android:layout_height="wrap_content" 
               android:layout_width="fill_parent">

        <TextView 
            android:text="@+id/TextView01" 
            android:id="@+id/txtViewTop" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:textSize="18dip">
        </TextView>
 </LinearLayout>

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

        <TextView 
            android:text="@+id/TextView02" 
            android:id="@+id/txtBottomLeft" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:textSize="14dip">
        </TextView>

        <TextView 
            android:text="@+id/TextView03" 
            android:id="@+id/txtBottomRight" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_marginRight="6dip"
            android:textSize="14dip">
        </TextView>
</LinearLayout>

</LinearLayout>
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295