Search code examples
androidandroid-linearlayoutandroid-xmlandroid-layout-weight

Typing in edittext increase its space and decreases the other edittext space


I have a linearlayout with two textviews and two editText. The problem is whenever I start writing in any of the editText, the other editText starts being small in space and the focused editText space gets increased as much as typing goes on.

My XML code is:

   <LinearLayout
                android:id="@+id/l6"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/l5"
                android:layout_margin="10dp"
                android:orientation="horizontal"
                android:padding="2dp"
                android:weightSum="4">

                <TextView
                    android:id="@+id/txtOverAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.2"
                    android:text="Oversease Address: " />

                <EditText
                    android:id="@+id/edtOverAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="12sp"
                    android:layout_weight="2.6"
                    android:inputType="text"
                    android:padding="5dp" />

                <TextView
                    android:id="@+id/txtReffBy"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_weight="0.2"
                    android:text="Reffered By: " />

                <EditText
                    android:id="@+id/edtReffBy"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textSize="12sp"
                    android:inputType="text"
                    android:padding="5dp" />

            </LinearLayout>

I don't know where I am doing wrong. Need help! Thanks in advance.


Solution

  • I have done few change in your code but I am not sure what is your requirement like 1. do you want to move the text in next line if text exceeded the width of Edittext? 2. Or do you want to scroll the text horizontally if it exceeds the width of Edittext?

    I have done both the things in the EditText given. First one move to new line and second will scroll

    <LinearLayout android:id="@+id/l6"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/l5"
    android:layout_margin="10dp"
    android:orientation="horizontal"
    android:padding="2dp"
    android:weightSum="4"
    xmlns:android="http://schemas.android.com/apk/res/android">
    
        <TextView
            android:id="@+id/txtOverAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="Oversease Address: " />
    
        <EditText
            android:id="@+id/edtOverAddress"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:layout_weight="2.6"
            android:text="Hello I am doing good. Hope you are also doing well"
            android:inputType="textMultiLine"
            android:padding="5dp" />
    
        <TextView
            android:id="@+id/txtReffBy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_weight="0.2"
            android:text="Reffered By: " />
    
        <EditText
            android:id="@+id/edtReffBy"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="12sp"
            android:inputType="text"
            android:text="Hello I am doing good. Hope you are also well"
            android:padding="5dp" />
    
    </LinearLayout> 
    

    Hope this will help.