0

Demo

Have the following layout

<NestedScrollView>
    <LinearLayout>
        <TextView/>
        <EditText/>
    <LinearLayout/>
</NestedScrollView>

This layout is all contained in another ScrollView, which is why I'm using a NestedScrollView.

The EditText is multiline with a fixed minHeight. Currently, if you exceed the height limit, the EditText will grow and the NestedScrollView will adapt to the growth.

I'm looking to change this EditText to grow upwards instead of downwards. I've tried several things, but the NestedScrollView is not handling the upward growth as expected.

Any help would be much appreciated :)

Rushabh Shah
  • 371
  • 1
  • 7
  • 22
user3213730
  • 19
  • 1
  • 4
  • Possible duplicate https://stackoverflow.com/questions/27698139/how-to-make-android-edittext-expand-vertically-when-full – Dillon Burton May 30 '17 at 01:40
  • @DillonBurton That question answers vertical growth, but it grows downwards instead of upwards. The scrollview also adds another layer of complexity. – user3213730 May 30 '17 at 01:42
  • Check [this one](https://stackoverflow.com/questions/12593747/have-android-edittext-extend-upwards-instead-of-downwards) there's only one answer there, but it seems he answered his own question. – Dillon Burton May 30 '17 at 01:45
  • @DillonBurton I did see that answer as well. My EditText is in a ScrollView, so I think the solution will be a bit different. There may be some useful hints from that answer, but the layout details are very vague. – user3213730 May 30 '17 at 01:50
  • The fact that it's in a `NestedScrollView` shouldn't have any effect on the fact that it's still expanding downward. Have you tried `android:gravity="bottom`? Also, if you could post code, I could help you more. – Dillon Burton May 30 '17 at 01:57
  • @DillonBurton I can put `android:layout_gravity="bottom" on the LinearLayout which makes it expand upward, but it seems that the `NestedScrollView` or `LinearLayout` still see it as growing downward. Once it grows upward off the screen, there is more blank space below the `EditText` that I can scroll to, but I cannot scroll up to see all of the `EditText`. I will setup a demo project this evening to demonstrate. Thanks for the help :) – user3213730 May 30 '17 at 02:03
  • No problem. Check my answer below. – Dillon Burton May 30 '17 at 02:14
  • @DillonBurton Please see an example [here](https://github.com/KyleDBoyd/UpwardGrowingEditTextInScrollView). If there is no layout_gravity on the `LinearLayout`, it grows downward, and I can scroll the entire view up and down once it exceeds the height of the device. With `layout:gravity="bottom"`, it grows upward, but it does not scroll anymore. – user3213730 May 30 '17 at 03:37

2 Answers2

0

If I understand what you are trying to do correctly, give this a shot.

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="200dip">

        <EditText
            android:layout_alignParentBottom="true"
            android:text="Put something really long here..."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>

The centerInParent isn't really relevant, but just to show that it doesn't matter where it's positioned.

Dillon Burton
  • 131
  • 10
0

It feels like what you are trying to achieve is a scrollable edittext where the focus is always at the bottom. So maybe you could try to give your edittext a TextWatcher and force scroll to bottom upon text change.

Add the following to your TextWatcher callback and see if that's what you want.

nestedScrollView.postDelayed(new Runnable() {
    @Override
    public void run() {
        nestedScrollView.fullScroll(ScrollView.FOCUS_DOWN);
    }
});
StoneBird
  • 1,900
  • 13
  • 12
  • This is the behavior I'm looking for, but unfortunately it isn't very smooth. This implementation causes it to "jump" a bit. – user3213730 May 30 '17 at 17:18
  • @user3213730 try ```setSmoothScrollingEnabled()``` on the scrollview to enable its scroll animation. Also you can use ```getLineCount()``` on your Edittext inside Textwatcher to figure out when actually the content got wrapped or a new line is created. You can let the scrollview scroll only under this circumstance to sort of minimize calls to force the scroll – StoneBird May 31 '17 at 01:58