Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scrollable linearlayout with weights bigger than screen in android

I am trying to create a vertical linearlayout with weights that has a size bigger than the screen. Let's say 2x the size of the screen. In order for this to work I would obviously need to be able to scroll through it. Unfortunately I can't figure out a way to do this. I tried using the layout weights, and setting the weight sum as half of the actual sum of the weights of all components (so if all components weights sum is 20 I set the weight sum as 10) and managed to make it work but unfortunately the scrolling is not working anymore for some reason.

Is there anything that I am missing?

this is the code that makes the linearlayout twice as big as the screen but the scroll is not working:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fillViewport="true">

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

        <EditText android:id="@+id/id1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:textSize="25dp"
            android:gravity="center"
            android:layout_weight="2"/>

        <EditText android:id="@+id/id2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:textSize="25dp"
            android:gravity="center"
            android:layout_weight="2"/>

    </LinearLayout>
</ScrollView>
like image 968
sakis kaliakoudas Avatar asked Jun 10 '13 21:06

sakis kaliakoudas


2 Answers

You declared height of your LinearLayout "match_parent" that is equal to its parents height. It will never scroll as long as the content is bigger then ScrollView. First of all you have to give a fixed height like (50dp) or wrap_content or you have to set Its height programmatically(like 2x screen height as you mention).

weightSum and weight will always force your items to fit in your LinearLayouts current size so try not to use it.

I hope this helps.

like image 30
Ercan Avatar answered Oct 23 '22 20:10

Ercan


The problem here is your use of layout_weight and weightSum is invalid. It's important to remember that android:layout_weight can only use the remaining space available in the view; anything exceeding that boundary is automatically cropped.

Therefore, in your example, your first EditText is taking up the entirety of the screen, and your second one is entirely excluded from the view. Because the second EditText is cropped, the LinearLayout has taken the entire screen and there's nothing for the ScrollView to do.

I'm not entirely sure what your end goal is; are you trying to have text inputs that grow with user entry, and the ScrollView handles the overflow?

If so, this will work:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:background="#000000"
    android:fillViewport="true" >

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

        <EditText
        android:id="@+id/id1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#DCDCDC"
        android:gravity="center"
        android:text="ONE ONE ONE"
        android:textIsSelectable="false"
        android:textSize="45sp" />

    <EditText
        android:id="@+id/id2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#AAAAAA"
        android:gravity="center"
        android:text="TWO TWO TWO"
        android:textIsSelectable="false"
        android:textSize="45sp" />
    </LinearLayout>

</ScrollView>

I've included some basic background colors so that you can see where each item begins & ends in layout preview. Typing in the first EditText will correctly push the second one down, producing a scroll bar.

Also note that you should use sp instead of dp for textSize values.

Edit: I should also note, for clarification, that weightSum will also take away space when necessary. To test this, set the weightSum of your LinearLayout to 2, and then add android:layout_weight="1" to each of the EditText controls. The end result will be a 50/50 split when the view loads, and then as you start typing in the first control, the 2nd space will shrink accordingly. Adding text to the second control will result in a scrollbar appearing.

like image 119
Mike P. Avatar answered Oct 23 '22 22:10

Mike P.