WSBT
WSBT

Reputation: 36333

How to efficiently make a form with TextView and EditText?

I want to make a clean and neat form like so:

Name:          ____________
Foo:           ____________
Bar:           ____________
Whatever:      ____________

Every line share the screen height equally (in this case, 25% of full screen), and each TextView should be on the left half of the screen, and EditText should be starting from the center of the screen.

My current solution is (essentially):

<LinearLayout android:orientation="vertical">
    <LinearLayout android:orientation="horizontal" weight="1">
        <TextView weight="1">
        <EditView weight="1">
    </LinearLayout>

    <LinearLayout android:orientation="horizontal" weight="1">
        <TextView weight="1">
        <EditView weight="1">
    </LinearLayout>

    <LinearLayout android:orientation="horizontal" weight="1">
        <TextView weight="1">
        <EditView weight="1">
    </LinearLayout>

    <LinearLayout android:orientation="horizontal" weight="1">
        <TextView weight="1">
        <EditView weight="1">
    </LinearLayout>
</LinearLayout>

This works well but creates nested weights, which is bad for performances. Is there a better way to do this in API level 8?

Upvotes: 1

Views: 565

Answers (2)

Jay Snayder
Jay Snayder

Reputation: 4338

If you would like you could rightAlign the EditText and leftAlign the TextView so that you get an aligned component within your layout. Then you could have the text input from right to left. This is one potential approach if you want to get out from using nested weights. It might not get the type of style that you are asking though.

Playing around further with minWidth on the fields to fill out a Dialog could provide a somewhat clean structure. Just guessing what you are trying to accomplish from the post and your overall concept of course.

Upvotes: 0

Ga&#235;tan Maisse
Ga&#235;tan Maisse

Reputation: 12347

You can use GridLayout, it's easy to use and it will flatten your view (by removing all unnecessary LinearLayout)

<GridLayout 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:columnCount="2"
   android:rowCount="4">

     <TextView
        android:layout_column="0"
        android:layout_row="0"/>

     <EditView
        android:layout_column="1"
        android:layout_row="0"/>

      ....    
</GridLayout>

For more details take a look at Android dev doc

Upvotes: 1

Related Questions