2

I have a LinearLayout with like a contact form for the user to edit his profile. However, the size of the editText just doesn't change no matter what size I give to the layout_weight. Tyvm for any help!

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
        android:background="@color/first_grey"
        android:orientation="vertical"
        tools:ignore="ContentDescription">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/toolbar_all_activities" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/viewUploadPicture"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="25"
                android:layout_marginTop="20dp"
                android:clickable="true"
                android:orientation="horizontal">

                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/imgUserProfile"
                    android:layout_width="0dp"
                    android:layout_height="90dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="0.5"
                    android:src="@drawable/skate_boarder" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="80dp"
                    android:layout_marginEnd="15dp"
                    android:layout_marginRight="15dp"
                    android:layout_weight="0.5"
                    android:gravity="center_vertical|start"
                    android:text="@string/user.edit.upload_photo"
                    android:textColor="@color/fourth_grey"
                    android:textSize="18sp" />

            </LinearLayout>

            <TextView
                style="@style/editProfileTextView"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:text="@string/user.first_name" />

            <EditText
                android:id="@+id/txtEditProfileFirstName"
                style="@style/editTextRoundGrey"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="50" />

            <TextView
                style="@style/editProfileTextView"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:text="@string/user.last_name" />

            <EditText android:id="@+id/txtEditProfileLastName"
                style="@style/editTextRoundGrey"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="50" />

            <TextView
                style="@style/editProfileTextView"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:text="@string/user.edit.favourite_hobby" />

            <EditText
                android:id="@+id/txtEditProfileFavouriteHobby"
                style="@style/editTextRoundGrey"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="50" />

            <TextView
                style="@style/editProfileTextView"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:text="@string/user.edit.birthday_date" />

            <LinearLayout
                android:id="@+id/birthdayLayout"
                android:layout_width="300dp"
                android:layout_height="0dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="10dp"
                android:layout_weight="50"
                android:orientation="horizontal">

                <EditText
                    android:id="@+id/txtDayBirthdayDate"
                    style="@style/editTextNumberRoundGrey"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2" />

                <EditText
                    android:id="@+id/txtMonthBirthdayDate"
                    style="@style/editTextNumberRoundGrey"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp"
                    android:layout_weight="0.2" />

                <EditText
                    android:id="@+id/txtYearBirthdayDate"
                    style="@style/editTextNumberRoundGrey"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp"
                    android:layout_weight="0.5" />

            </LinearLayout>

            <TextView
                style="@style/editProfileTextView"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:text="@string/user.edit.bio" />

            <EditText
                style="@style/editTextRoundGreyBigger"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="120" />

            <TextView
                style="@style/editProfileTextView"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:text="@string/user.edit.city" />

            <EditText
                android:id="@+id/txtEditProfileCity"
                style="@style/editTextRoundGrey"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="50" />

            <TextView
                style="@style/editProfileTextView"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:text="@string/user.edit.languages" />

            <EditText
                android:id="@+id/txtEditProfileLanguages"
                style="@style/editTextRoundGrey"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="50" />

            <TextView
                style="@style/editProfileTextView"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="40"
                android:text="@string/user.edit.password" />

            <EditText
                android:id="@+id/txtEditProfilePass"
                style="@style/editTextRoundGrey"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="50" />

            <Button
                android:id="@+id/btnSubmitChangedData"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="15dp"
                android:layout_weight="100"
                android:padding="10dp"
                android:background="@color/red"
                android:text="@string/user.edit.submit" />

        </LinearLayout>

    </ScrollView>
</LinearLayout>

3 Answers3

2

you can't use layout_weight in ScrollView because it depend on the height of the child layout and layout_weight too depend on the height of the outside layout and that's contradiction, so you can use custom layout_height to every child .

I know the answer is late but i hope it help someone else .

Mohamed Ben Romdhane
  • 1,005
  • 3
  • 11
  • 22
0

For changing Edittext height you should also prefer to use attributes like maxHeight, minHeight, maxLines, inputTypes, and textAppreance attributes,

This below example might help you :

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:ignore="ContentDescription">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_marginTop="20dp"
            android:paddingLeft="5dip"
            android:paddingRight="5dip"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="FirstName" />

            <!-- Person Name EditText-->

            <EditText
                android:id="@+id/txtEditProfileFirstName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxHeight="40dip"
                android:inputType="textPersonName"
                android:maxLines="1"
                android:maxLength="25"
                android:textAppearance="?android:attr/textAppearanceMedium"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="LastName" />

            <EditText android:id="@+id/txtEditProfileLastName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxHeight="40dip"
                android:inputType="textPersonName"
                android:maxLines="1"
                android:maxLength="25"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="Hobby" />

            <EditText
                android:id="@+id/txtEditProfileFavouriteHobby"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxHeight="40dip"
                android:inputType="text"
                android:maxLines="2"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="Birthday Date" />

            <LinearLayout
                android:id="@+id/birthdayLayout"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="10dp"
                android:weightSum="1"
                android:orientation="horizontal">
`enter code here`                <EditText
                    android:id="@+id/txtDayBirthdayDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.3"
                    android:inputType="number"
                    android:gravity="center"
                    android:layout_gravity="center"
                    android:maxLength="2"
                    />

                <EditText
                    android:id="@+id/txtMonthBirthdayDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp"
                    android:layout_weight="0.3"
                    android:gravity="center"
                    android:layout_gravity="center"
                    android:inputType="number"
                    android:maxLength="2"
                    />
                <EditText
                    android:id="@+id/txtYearBirthdayDate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginStart="10dp"
                    android:layout_weight="0.4"
                    android:inputType="number"
                    android:gravity="center"
                    android:layout_gravity="center"
                    android:maxLength="4"
                    />
            </LinearLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="Bio" />

            <!--MultiLine EdiText-->

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textMultiLine"
                android:minHeight="80dp"
                android:maxLines="4"
                android:isScrollContainer="true"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="City"
                />
            <EditText
                android:id="@+id/txtEditProfileCity"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxHeight="40dip"
                android:inputType="textPostalAddress"
                android:maxLines="1"
                android:maxLength="25"
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="Languages" />
           <EditText
                android:id="@+id/txtEditProfileLanguages"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxHeight="40dip"
                android:inputType="text"
                android:maxLines="1"
                android:maxLength="25"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="password" />
            <!--Password EditText-->
            <EditText
                android:id="@+id/txtEditProfilePass"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxHeight="40dip"
                android:inputType="textPassword"
                android:maxLines="1"
                android:maxLength="25"
                android:textAppearance="?android:attr/textAppearanceMedium"/>

            <Button
                android:id="@+id/btnSubmitChangedData"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="15dp"
                android:layout_weight="1"
                android:padding="10dp"
                android:background="@color/red"
                android:text="Submit" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>
Mohamed Ben Romdhane
  • 1,005
  • 3
  • 11
  • 22
Sathiamour
  • 137
  • 11
  • @AntónioVieira, what have you tried? See my example below. You must use the `android:weightSum` attribute in order for `android:layout_weight` to have any effect. – VIN Jul 21 '16 at 02:40
  • @Kaleb I've put `android:weightSum=915` in the `LinearLayout` after the `ScrollView` and doesn't change anything in the layout. I even tried what @Sathiamour suggested with the floating point but I thought that when you use once floating point every child as to use it as well.' – António Vieira Jul 21 '16 at 08:40
  • @Sathiamour the inputTypes I had in my style defined in the file `styles.xml`. I've looked through your answer and tried but the height stays the same, I think it's because of the wrap_content, if I change directly to like `30dp` or `40dp` the height increases as expected but I think it's not the best practice, that's why I used `layout_weight` – António Vieira Jul 21 '16 at 09:43
0

The android:layout_weight attribute is used in conjunction with the android:weightSum attribute. Your children layout_weights must sum to the weightSum of their parent layout.

You use layout_weight many times in your xml code without declaring a weightSum. Without android:weightSum, android:layout_weights will have no effect. For instance, if you want two views within a parent LinearLayout to span evenly, you can do something like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100">

    <EditText
        android:id="@+id/my_edit_text"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="50"
        android:hint="type something">

    <Button
        android:id="@+id/my_button"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="50"
        android:text="a button">
</LinearLayout>

Note that your child views' layout_heights will need to be set to "0dp" for vertical LinearLayout orientation. For a horizontal orientation, you'd set the child views' layout_widths to "0dp".

For another example, see the accepted answer at https://stackoverflow.com/a/7452788/4138919

I also notice that you mix floating point values with non-floating point values for layout_weights. Consider sticking to one or the other for readability purposes.

Community
  • 1
  • 1
VIN
  • 6,385
  • 7
  • 38
  • 77
  • I've made some tests in other layouts and in the one I'm referring to the question and the `weightSum` it only works for me if I define a certain size to the parent (`ScrollView`) which I'm specifying the `weight_sum` (`LinearLayout`). If I do like this, the size of everything will be alright, `EditText` and the other stuff. However in this case it really must be `match_parent` because it's a `ScrollView`, so I'm a little bit stuck. – António Vieira Jul 21 '16 at 12:58