views:

102

answers:

4
+1  Q: 

Android XML Layout

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

A: 

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
@uthark 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
PM - Paresh Mayani
Aa, I see it. Then you can do it with table layout. I updated code.
uthark
@uthark 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
PM - Paresh Mayani
@Paresh You can define width to what you need.
uthark
A: 

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 Chandrasekaran
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
@Praveen 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
PM - Paresh Mayani
Praveen Chandrasekaran
+2  A: 
ud_an
@ud_an 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
PM - Paresh Mayani
@ud_an check my image for the TextView01 length
PM - Paresh Mayani
k edited answer new solution try that
ud_an
in my new answer first textview size will be full otherwise you have to define size i think
ud_an
check last code
ud_an
A: 

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>
PM - Paresh Mayani