android tablelayout with complex component placement

EDITED: I HAVE FOUND THE SOLUTION MYSELF. THANK YOU!

I want to create the following screen for an Android App:

Right now I have this:

with this code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:weightSum="1" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight=".65"
        android:weightSum="5" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="TOURNAMENT: "
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"/>
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="GAME: "
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_weight="1"
            android:layout_marginLeft="10dp" />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="BEST OF: "
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_weight="1"
            android:layout_marginLeft="10dp" />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="BRACKET STYLE: "
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_weight="1"
            android:layout_marginLeft="10dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight=".35"
        android:weightSum="5" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="10dp" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="10dp" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="10dp" />
        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="10dp" />

        <RelativeLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight="1" >

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="15dp"
                android:text="CANCEL" />

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/button2"
                android:text="CREATE" />

        </RelativeLayout>

</LinearLayout>

</LinearLayout>

The text can be aligned to the right using android:gravity="right", but how can I make it centered vertically?

--> AISHH, it's: android:gravity="center_vertical|right"

Thank you!!

Answers


Well TableLayout is not a good ideea. You should try using a RelativeLayout and some LinearLayout nested inside.

Try something like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:id="@+id/first_linear"
        android:orientation="vertical">
        <!-- Note that for text view,and editText you might need to use certain values (dip values) cuz wrap content will try to arrange the view -->
        <TextView
            android:id="@+id/first_text_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <EditText
            android:id = "@+id/first_edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>
   <!-- we align all the following linearlayouts below the first -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/first_linear"
        android:id="@+id/second_linear"
        android:orientation="vertical">
        <TextView
            android:id="@+id/second_text_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <EditText
            android:id = "@+id/second_edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

<!-- add all the layouts like those from above -->

<!-- ...... -->
<!-- after you added all the fields you can add a linear layout containing two buttons to float below the last one -->


I've done a similar layout like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_height="fill_parent" android:layout_width="fill_parent"
>
    <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0">
        <LinearLayout android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="fill_parent">
            <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:stretchColumns="1" android:shrinkColumns="1">
                <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
                    <TextView android:text="Value1" android:gravity="right" />
                    <EditText android:singleLine="true" android:id="@+id/editTextVal1" />
                </TableRow>
                <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
                    <TextView android:text="Long name value2" android:gravity="right" />
                    <EditText android:singleLine="true" android:id="@+id/editTextVal1" />
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </ScrollView>
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" />
    </LinearLayout>
</LinearLayout>

There's a scroll panel in there to have the buttons on the bottom of the screen at all times, in case your configuration goes longer.


Need Your Help

Logical Help Needed in a PHP Script

php mysql localhost

I am writing a little PHP script which is simply return data from a MYSQL table using below query

Reproduce writeToURL/initWithContentsOfURL behavior with array of custom objects instead of dictionaries

objective-c cocoa nsarray nsdictionary

I'm replacing an array of NSMutableDictionary objects with an array of custom objects. Each has 15 or so instance variables which need to persist and a couple more which are transient.