1

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?

WSBT
  • 33,033
  • 18
  • 128
  • 133
  • Have you tried with table layout? – Chitrang Oct 07 '14 at 19:54
  • Yes, but I cannot get TextView and EditView each to fill 50% of the width. – WSBT Oct 07 '14 at 20:04
  • table layout would be essentially the same thing. I would suggest you worry about performance when performance gives you reason to worry. Otherwise, there is a way of splitting a relativelayout in 2: http://stackoverflow.com/a/22362494/671543 – njzk2 Oct 07 '14 at 20:12
  • Your concern about "nested weights, which is bad for performances." can be removed by having android:layout_weight="0.5" in TextView and EditView anfd remove weight="1" from parent linear layout. – Chitrang Oct 07 '14 at 20:12
  • @njzk2 thanks... I do not have a performance issue to worry about; I just don't like to see these warnings :) – WSBT Oct 07 '14 at 20:14
  • GridLayout is available API 14+. TableLayout internally use LinearLayout so they are the same performance. In my opinion, you should not worry about performance unless your form has a lot of controls. Use any solution you like. – LHA Oct 07 '14 at 20:27
  • Do you actually need the height to be divided? on a large tablet, in portrait, it may not look good. May be you could use a fixed height? – njzk2 Oct 07 '14 at 20:30

2 Answers2

1

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

Gaëtan Maisse
  • 12,208
  • 9
  • 44
  • 47
0

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.

Jay Snayder
  • 4,298
  • 4
  • 27
  • 53