0

i created this form:

enter image description here

writing this XML layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/fields"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp">

        <LinearLayout
            android:id="@+id/layoutCountry"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textCountry"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="18sp"
                android:text="@string/text_country"
                android:textColor="@color/black" />

            <Spinner
                android:id="@+id/spinnerCountry"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1.5"
                android:textSize="18sp"
                android:inputType="text"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/editCustom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:inputType="text"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/city"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="18sp"
                android:text="@string/text_city"
                android:textColor="@color/black" />

            <EditText
                android:id="@+id/editCity"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:textSize="18sp"
                android:inputType="text"
                android:textColor="@color/black" />
         </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/postal"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="18sp"
                android:text="@string/text_postal"
                android:textColor="@color/black" />

            <EditText
                android:id="@+id/editPostal"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:textSize="18sp"
                android:inputType="text"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textAddress"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="18sp"
                android:text="@string/text_address"
                android:textColor="@color/black" />

            <EditText
                android:id="@+id/editAddress"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:textSize="18sp"
                android:inputType="text"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textTel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="18sp"
                android:text="@string/text_tel"
                android:textColor="@color/black" />

            <EditText
                android:id="@+id/editTel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:textSize="18sp"
                android:inputType="text"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textEmail"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="18sp"
                android:text="@string/email"
                android:textColor="@color/black" />

            <EditText
                android:id="@+id/editEmail"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:textSize="18sp"
                android:inputType="textEmailAddress"
                android:textColor="@color/black" />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

As you can see the second EditText is too long and i would align it to the other fields, like this:

enter image description here

I tried to use there a RelativeLayout and do some changes with LinearLayout but i'm getting crazy because i can't reach my purpose. I am not so expert about XML layouts... :(

smartmouse
  • 13,912
  • 34
  • 100
  • 166

5 Answers5

1

I edit only second LinearLayout that problem , here this is solution :

  1. In EditText you must set layout_weight="1.5" as same as other and android:layout_width="0dp"
  2. In Parent View of EditText , LinearLayout set android:weightSum=2.5" (1 + 1.5)
  3. set android:gravity="right" to LinearLayout Fin!

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2.5"
        android:gravity="right"
        android:orientation="horizontal">
    
        <EditText
            android:id="@+id/editCustom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:inputType="text"
            android:layout_weight="1.5"
            android:textColor="@color/black" />
    </LinearLayout>
    
Phonbopit
  • 3,393
  • 3
  • 21
  • 29
  • This is very cool! Can you explain the behaviour of weightSum 1+1,5? – smartmouse Jan 30 '15 at 10:23
  • 1
    @smartmouse On the other LinearLayout , you have TextView with layout_weight = 1 and EditText layout_weight = 1.5 that total 2.5 you can read this that explain better than me http://stackoverflow.com/questions/7452741/what-is-androidweightsum-in-android-and-how-does-it-work – Phonbopit Jan 30 '15 at 13:26
1

replace your code for second EditText via

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/textCountry"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="" />
        <EditText
            android:id="@+id/editCustom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.5"
            android:inputType="text"
            android:textColor="@color/black"
            android:textSize="18sp" />
    </LinearLayout>

you can add empty Textview and by defining proper weight for both you achieve it. And make sure when you define layout_weight for any view define width as android:layout_width="0dp"

Pragati
  • 21
  • 2
0

Try using TableLAyout instead

http://www.mkyong.com/android/android-tablelayout-example/

Flaxie
  • 540
  • 3
  • 13
0

Try setting gravity right and then some margins. Might do what you want. Most of the times I prefer using RelativeLayouts since they are easier to manipulate.

funkycookie
  • 226
  • 2
  • 16
0

In your EditText's parent LinearLayout view have this:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alignLeft="@id/spinnerCountry"
        android:orientation="horizontal">

This will align the entire LinearLayout's left hand side to the left hand side of the Spinner.

Sam Bains
  • 548
  • 2
  • 8
  • Do you mean `android:layout_alignLeft="@id/spinnerCountry`? In this case i get this error: `Invalid layout param in a LinearLayout: layout_alignLeft` – smartmouse Jan 30 '15 at 10:29
  • Sorry, I misread the layout in the scrollview as Relative not Linear. The error you get makes sense then – Sam Bains Jan 30 '15 at 10:49