0

I have 3 components on my application, 1 textview(inputType:textMultiline, scrollbar:vertical, gravity:bottom|right) at the top, 1 editview at the middle and 1 button at the bottom. When I type something on my editview and I click the button, the text written on the edit view displays on the textview. Every time i hit ok, the text displays the the bottom and what is displayed is the first three inputs. I have to scroll down the textview in able for me to see my last input.

Now, I want my users to see their last input on their textview. I want to know if there is such code for auto scrolldown for textviews everytime I input a new text on it. I am new on developing android apps. Thanks!

ron
  • 19
  • 1
  • 5
  • Here's a similar question: http://stackoverflow.com/questions/3506696/auto-scrolling-textview-in-android-to-bring-text-into-view – jonvuri Apr 26 '12 at 02:54

4 Answers4

5

When you set text to textview just set foucus to it. like

tv.setFocusable(true);

It will automatically focus your view whenever you change your string on textview.

If you are adding text to your text view again and again then you can try this

    int scroll_amount = tv.getBottom();
    tv.scrollTo(0, scroll_amount);

Hope it will work not sure..

Try this also

    int scroll_amount = (int) (tv.getLineCount() * tv.getLineHeight()) - (tv.getBottom() - tv.getTop());
    tv.scrollTo(0, scroll_amount);
Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29
  • But everytime i enter a text on my textview, the text will go under the previous text that i have entered. Since my textview is only 150dp, after 3-4 lines of texts, i wont be able to see the next lines that i have entered. I have to scrolldown first before I see the texts that i have entered. – ron Apr 26 '12 at 04:22
  • Shouldn't it be 'tv.scrollTo(0,scroll_amount)' because the parameters of scrollTo(x,y)? y parameter is the one that holds the vertical scroll? Just asking.. – ron Apr 26 '12 at 04:47
  • Its ok. I'm at office right now and my laptop is at home, ill try this as soon as I get home.Thank you! :D – ron Apr 26 '12 at 04:54
  • I am also in office but tell me is it working i will also try to do it.. at home... – Bharat Sharma Apr 26 '12 at 04:56
  • I surely will, but I have to wait for 7 hours before I can go home. Thank you again. – ron Apr 26 '12 at 05:01
  • The tv.getBottom() doesn't work while the (int) (tv.getLineCount() * tv.getLineHeight); scrolls down too much. xD – ron Apr 26 '12 at 11:45
  • I already got the answer. I answered my question already. Look at my answer. I got the idea from the codes you provided. I wanted to vote you up but I don't have enough reputation. xD Thanks a lot! :D – ron Apr 26 '12 at 12:31
1

you can use scrollView.scrollTo(x,y) to auto scroll to position that you want.

/*edit*/

create custom class for scrollview

package com.android.mypackage
public class myScrollView extends ScrollView{
    private int maxY = 0;
    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
       if(y>maxY)
        maxY=y;
    }
    public void moveToEnd(){
        this.scrollTo(0, maxY);
    }
}

using this custom class in layout xml as below:

...
<com.android.mypackage.myScrollView
    android:id="@+id/my_scrollview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView .../>
</com.android.mypackage.myScrollView>
.....

when you press Ok button just call function myscrollViewObj.movetoEnd(); it's just a draft code, still not tested.

MichaelP
  • 2,761
  • 5
  • 31
  • 36
  • What should be the value of my y? – ron Apr 26 '12 at 02:47
  • you should override onScrollChanged and debug to know what y value you want – MichaelP Apr 26 '12 at 03:08
  • Ok, ill try to research about onScrollChanged. Thank you! :D – ron Apr 26 '12 at 03:11
  • just create a custom class extend scrollview, then override onScrollChanged. using your custom scrollview in layout xml. – MichaelP Apr 26 '12 at 03:18
  • I'm really new on java and android development so i'll try to study about how to override classes.But is there a more basic solution for this kind of problem where i don't have to create a new class? – ron Apr 26 '12 at 03:26
  • ok, i'm not pretty sure about your layout, it will be more clear if you post layout xml on here.i just imagine that you have a scrollview and textview,editview,OK button put inside scrollview. after you finish typing text on editview, you press Ok button and want to see the scrollview auto scroll or scrollview of textview auto scroll? – MichaelP Apr 26 '12 at 03:39
  • Well i can't do it at the moment cuz I'm on the office and my laptop is at home. Sorry. Only the textview is inside the scroll view and yes, you got it right sir. I need my textview to auto scrolldown. My code for my text view i like this. 'textview.setText(textview.getText() + "\n" + editview.getText());'. – ron Apr 26 '12 at 03:48
  • I just edited my answer above, refer it as an idea. Im still not test it. – MichaelP Apr 26 '12 at 04:02
  • I don't have to call myScrollView on my main java code? I just have to change the ScrollView to mypackagename.MyScrollView on my layout xml? – ron Apr 26 '12 at 04:16
1

I already got the answer for this! Thank you for giving me some idea. I might be able to use them in the future. @Bharat Sharma, you almost got the answer! Thanks!

public void onClick(View v) {
            // TODO Auto-generated method stub
            log.setText(log.getText() + "\n" +input.getText());

            if(log.getLineCount() > 5){
                scroll_amount = scroll_amount + log.getLineHeight();
                log.scrollTo(0, scroll_amount);
            }
        }

I called the variable scroll amount outside the onCreate(). Thanks again!

ron
  • 19
  • 1
  • 5
0

This is it.

public void onScrollDownClicked(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        mTextView.scrollTo(0, mTextView.getLayout().getHeight() - mTextView.getHeight());
    }
}
LEAD2M
  • 186
  • 1
  • 5