4

I'm trying to develop an activity that will be part of a simple chat application for a college project. I want to have a setup where I have an EditText and a Button which when pressed takes the EditText's text and appends it to the contents of a TextView in the same activity. That seems to be working fine for me so far but I also want to make it so that when a message is sent to be displayed on the TextView if there is not enough space to show the new line of text it will scroll down automatically.

This is what my onCreate() in the Activity looks like

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText edit = (EditText) findViewById(R.id.editText1) ;
    final Button button = (Button) findViewById(R.id.button1) ;
    final TextView text = (TextView) findViewById(R.id.textView1) ;

    //text.setMaxHeight(200);
    text.setSelected(true);
    text.setMovementMethod(new ScrollingMovementMethod());

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(edit.getText().length() > 0) {
                text.append(edit.getText() + "\n") ;
                edit.setText("");
            }
        }
    });
}

This is the relevant section of the Activity's XML layout file.

<TextView
    android:id="@+id/textView1"
    android:ellipsize="marquee"
    android:focusable="false"
    android:marqueeRepeatLimit="marquee_forever"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="70dp"
    android:text=""
    android:scrollbars ="vertical"
    android:maxLines="10" />

What am I doing wrong? Or am I misunderstanding and is what I want not possible with a textview?

EDIT: Full XML file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/editText1"
    android:layout_alignBottom="@+id/editText1"
    android:layout_alignParentRight="true"
    android:text="@string/okString" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="107dp"
    android:layout_toLeftOf="@+id/button1"
    android:ems="10" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >

    <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="70dp"
            android:ellipsize="marquee"
            android:focusable="false"
            android:marqueeRepeatLimit="marquee_forever"
            android:maxLines="10"
            android:scrollbars="vertical"
            android:text="     " />
</ScrollView>

The Ice Mage
  • 445
  • 1
  • 7
  • 17
  • You have to put the `TextView` inside of a `ScrollView` – Hugo Hideki Yamashita Nov 25 '13 at 23:38
  • I just did that via the graphical layout interface in eclipse and it seems the application doesn't work now. (getting "Unfortunately, has stopped." as soon as it loads on emu) – The Ice Mage Nov 26 '13 at 00:00
  • @HugoHidekiYamashita I noticed in the log now it was complaining that it cannot cast from `ScrollView` to `TextView`... I don't understand why that is happening. – The Ice Mage Nov 26 '13 at 00:27
  • Can you post full layout xml file and code also? – Harsh Singal Nov 26 '13 at 00:45
  • Sure, will do asap. The XML that is. There really isn't much more to the code; just the autogenerated onCreateOptionsMenu. – The Ice Mage Nov 26 '13 at 00:49
  • check this link for perfect autoscrolling textview in vertical http://yuvarockers.blogspot.in/2017/04/vertical-autoscrolling-textview-in.html – YLS Apr 27 '17 at 03:14

3 Answers3

2
            String text="Im trying to develop an activity that will be part of a simple chat application for a college project. I want to have a setup where I have an EditText and a Button which when pressed takes the  "
            mText = (TextView) findViewById(R.id.textView1);
            mText.setText(text)
    mText.setMovementMethod(new ScrollingMovementMethod())

XML

    <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    android:text="                       
                    " />

Note:Instead of storing your text as normal string save it in HTML format which will add line break in your text.

 String text="Im trying to develop an activity that will<br> be part of a simple chat <br>application for a college project. I want to have a setup where I have an EditText and <br>a Button which when pressed takes the  "

and then add this line to above code

 mText.setText(Html.fromHtml(text));

This will return more proper result

Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
1

Assuming that you have inserted a closing tag </RelativeLayout> in your xml file, I tried your code and it is working fine on my end. Please try to clean project and rebuilding it again from

Project -> Clean
Project -> Build Project

EDIT: Can you please try this for layout? Please note that I have removed ScrollView from layout and inserted the line text.setMovementMethod(new ScrollingMovementMethod()); again.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<TextView 
    android:text="    " 
    android:id="@+id/textView1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:scrollbars="vertical"
    android:maxLines="10">
</TextView>

<RelativeLayout 
    android:id="@+id/InnerRelativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <Button 
        android:text="Button1" 
        android:id="@+id/button1"
        android:layout_alignParentRight="true" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </Button>

    <EditText 
        android:id="@+id/editText1" 
        android:layout_width="fill_parent"
        android:layout_toLeftOf="@id/button1"
        android:layout_height="wrap_content">
    </EditText>

</RelativeLayout>

Harsh Singal
  • 377
  • 1
  • 7
  • Ahh...that fixed the RuntimeException...but it's still not what I want. I want the old text to be pushed up when a new message is sent, if there has been enough messages sent. Y'know, like when a new message comes in on facebook chat or any of many chat systems I imagine. – The Ice Mage Nov 26 '13 at 01:21
  • Edited the comment with layout file. Please note that the scroll will start working after you have inserted 10 lines into textview as you have set android:maxlines="10" – Harsh Singal Nov 26 '13 at 02:14
  • Upvoted because you were quite helpful in sorting out some issues but accepting Frane's answer because he gave the one thing that was crucial to what I was looking for; for the text view to scroll up the text _without the user touching it_ when there is more lines that the max line value. – The Ice Mage Nov 26 '13 at 11:13
1

You shouldn't put vertically scrollable component inside vertically scrollable scrollview, and since your textview is scrollable, i don't see why you need the scrollview in the first place. Then you can use text.setScrollY(text.getLayout().getHeight()). I'm not sure if it will work, but if it doesn't you can try Get the size of a text in TextView

Community
  • 1
  • 1
Frane Poljak
  • 2,315
  • 23
  • 25