tags:

views:

54

answers:

2

In my Android app, I have a tabbed Activity. In one of the tabs I have two TextViews and two EditTexts.

The first EditText is only one line, and that's fine. However, I want the other EditText, android:id="@+id/paste_code", to take up the remaining space, but no matter what I do to it, it will only show one line. I don't want to manually set the number of lines, since the number that would fit on the screen differs based on your device.

Here's the relevant code. It's nested inside all the necessary components for a tabbed Activity.

<ScrollView
    android:id="@+id/basicTab"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_weight="1" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Paste title"
            android:layout_weight="0" />
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/paste_title_hint"
            android:id="@+id/paste_title"
            android:lines="1"
            android:gravity="top|left"
            android:layout_weight="0" />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Paste text"
            android:layout_weight="0" />
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:hint="@string/paste_hint"
            android:id="@+id/paste_code"
            android:gravity="top|left"
            android:layout_weight="1" />
    </LinearLayout>
</ScrollView>
A: 

The problem seems to come from your use of ScrollView. I've tested your code using a ScrollView as the parent container, and got the same problem. However if I replaced the ScrollView with a LinearLayout, then the second EditText properly expanded to fill the whole screen. The problem must be that ScrollViews are designed to wrap to their smallest possible size, regardless of what settings you put in android:layout_height. I experimented with another few layouts, e.g. a RelativeLayout using layout_above and layout_below, but those only affected its maximum size, not its size when empty. Unfortunately, that means I'm not sure how to solve your problem... Is there a way you can redesign your layout to use something other than the ScrollView as the parent container?

Steve H
Yeah, this tab would probably be fine without a ScrollView, since (if I remember correctly) EditTexts have their own scrollbars. I'll play around with a LineaLayout.Thanks.
Jamie
Also, I just found http://stackoverflow.com/questions/2033296/android-scrollview-problem which affirms your statement of android:layout_height not doing anything and offers another XML attribute to make it take up the rest of the screen. If that doesn't work I'll resort to a LinearLayout.
Jamie
A: 

Since the accepted answer doesn't address the situation fully, here's a proper fix for people coming to this while searching:

Firstly, Romain Guy from the Android dev team addresses this well in this blog post:

http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

Essentially, your ScrollView needs to contain the android:fillViewport="true" attribute.

If things aren't working once you've done that, here are a couple of things to check:

  • The layout inside the ScrollView (such as a LinearLayout) needs to have layout_height="wrap_content"
  • The view(s) you want to expand should have layout_height="wrap_content"
  • The view(s) you want to expand should have layout_weight="1.0" or similar

Don't forget to set minLines="3" or similar in the view(s) you want to expand if you don't want it/them to shrink too much.

HXCaine
Indeed; thank you for that. I commented on the other answer with a link to another SO question that solved my problem.
Jamie