I really tried to, but I can't understand how Android interprets layout_weight
setting...
What I'm trying to achieve is
When typing I'd like to grow the EditText to a specific height and to start scrolling if the text entered exceeds the available height. Doing this I need the surrounding LinearLayout
to grow together with the EditText.
If I define a specific height for the inner LinearLayout
it won't grow. If I don't, the inner layout takes ALL the space instead of the ScrollView, no matter what I try using layout_weight
. :(
My current XML looks like that:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I'm a header" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="bottom">
<LinearLayout
android:id="@+id/itemContainer"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:padding="2dp"
android:gravity="bottom"
android:isScrollContainer="true"
android:background="@drawable/steel" >
<EditText
android:id="@+id/edittext01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:fadingEdgeLength="60dp"
android:maxHeight="40dp"
android:textSize="15sp" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:layout_gravity="right"
android:text="Send"
android:textStyle="bold"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
Any tips are greatly appreciated!
layout_weight
is used to determine how Android divides up any left over space after the various widgets get all of the space they want.
So say you have 3 widgets in a row, each of which want 10 pixels, but you have 50 pixels worth of space. Here are a few examples of layout_weights and the number of pixels each widget will claim:
widget1: weight = 1, 30px
widget2: weight = 0, 10px
widget3: weight = 0, 10px
widget1: weight = 1, 20px
widget2: weight = 1, 20px
widget3: weight = 0, 10px
widget1: weight = 1, 16.5px
widget2: weight = 1, 16.5px
widget3: weight = 1, 16.5px
You can think of the weight as a percentage of available space. It will add up all of the values, and then give out portions as appropriate. Thus, if it helps you can use weights that add up to 100 to do percentages:
widget1: weight = 0, 10px
widget2: weight = 25, 15px
widget3: weight = 75, 25px
If I understand correctly, you want the bottom widget to start at a particular size, then expand to some maximum size if necessary, and then scroll.
And, you want the widget above it to take over all of the extra space.
Unfortunately in Android its not realy possible to specify a maximum size. I think the best you could do would be to give the ScrollView some fixed size, with a layout_weight = 1 and tell the bottom LinearLayout to wrap_content. This would (I think) cause the LinearLayout to expand until it can't anymore, because the ScrollView has already claimed the space.
However, the challange is specifying a fixed size for the ScrollView that makes sense at all different screen sizes and resolutions. You might end up having to create different layouts for the different resolutions, which is probably not worth it. Personally, I'd just give the editText a fixed size...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With