Rad
Rad

Reputation: 830

Align EditTexts next to TextView

I want my EditTexts to be aligned above each other, be equally long, and aligned to the right. I'm using Linear Layout. Right now it looks like this.

Here's my Android Layout XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:orientation="vertical" android:configChanges = "orientation"
    android:screenOrientation = "portrait" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:maxLines="1"
            android:singleLine="true"
            android:text="Mail:"
            android:textAppearance="?android:attr/textAppearanceSmall" />



        <EditText
            android:id="@+id/tbopmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Fornavn:"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <EditText
            android:id="@+id/tbopfornavn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPersonName" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Efternavn:"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <EditText
            android:id="@+id/tbopefternavn"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPersonName" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:singleLine="true"
            android:text="Adresse:"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <EditText
            android:id="@+id/tbopadr"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1" android:inputType="textPersonName"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Telefon:"
            android:textAppearance="?android:attr/textAppearanceSmall" />





        <EditText
            android:id="@+id/tboptlf"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:maxLength="8" android:layout_weight="1"/>

    </LinearLayout>




        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >


            <Button
                android:id="@+id/btopret"
                android:layout_width="170dp"
                android:layout_height="wrap_content"
                  android:layout_gravity="right"
                android:layout_marginTop="50dp"
                android:text="Opret mig som bruger!" android:layout_marginLeft="100dp" android:layout_marginRight="1dp"/>



    </LinearLayout>

</LinearLayout>

Upvotes: 1

Views: 2833

Answers (5)

MattDavis
MattDavis

Reputation: 5178

What you're looking for is the android:layout_weight attribute, which tells the Activity the percentage of a parent each view should take up. Each parent view considers this attribute when laying its children out, the parent layout has a weight of "1" to assign to each of its views. This corresponds to 100% of the view. If you give the children (your textviews and edittexts) android:layout_weight=".5", they will each expand to take up 50% of the parent's space. For your case, it seems like you may want something like a weight of .25 for your text views and .75 for your edit texts.

Additionally, if you don't like working with decimals, you can assign your parent view the android:weightSum attribute. Your new number will correspond to 100% of the parent view, instead of 1. For example, if you wanted a 25%-75% split for your views, give each parent LinearLayout a weightSum of 4, the TextViews a weight of 1, and the EditTexts a weight of 3.

I just looked at your code before submitting and realized that you already use the layout_weight attribute. To get a little more in depth, when you give a single view in a parent a weight of 1, as you are, you're telling it to take up all available space after the other views have been laid out. So your textview is wrapping it's content and taking up a minimal amount of the parent, and the EditTexts get the rest. Basically, weight will not prevent a view from being laid out. If it requires more space in a parent than the weight is providing it, the remaining space is divided based on the weight of the other children.

Upvotes: 1

Vinayak Bevinakatti
Vinayak Bevinakatti

Reputation: 40503

Just add android:orientation="vertical" to your linearlayout1,2,3...

<LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

Upvotes: 0

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

you can use weightSum in LinearLayout ...

Upvotes: 0

Thkru
Thkru

Reputation: 4199

You could try to use a RelativeLayout

http://developer.android.com/resources/tutorials/views/hello-relativelayout.html

or otherwise try a TableLayout with equal colums

http://developer.android.com/resources/tutorials/views/hello-tablelayout.html

Upvotes: 0

yiddow
yiddow

Reputation: 121

i don't try but maybe you can use table layout for that problem. http://developer.android.com/resources/tutorials/views/hello-tablelayout.html can take a look to this link

Upvotes: 0

Related Questions