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>