2

I want to have two multi line textedits, which both grow as more stuff get typed in.

This is what I have got so far:

<RelativeLayout ...>
    // Some other stuff

    <LinearLayout
        android:id="@+id/content_layer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_lists_repeat"
        android:layout_below="@id/title_bar"
        android:orientation="vertical">

        <TextView
            android:id="@+id/multi_line_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"/>


        <EditText
            android:id="@+id/multi_line_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:scrollbars="vertical"
            android:inputType="textCapSentences|textMultiLine"/>

        <TextView
            android:id="@+id/multi_line_text_view_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"/>

        <EditText
            android:id="@+id/multi_line_edit_text_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:scrollbars="vertical"
            android:inputType="textCapSentences|textMultiLine"/>

    </LinearLayout>
</RelativeLayout>

There are some problems though:

  1. EditText grows and grows infinitely if you just press enter
    • would be nice if the EditText grows only until all space is taken of the view and no more further
  2. When selecting words in EditText the Action Bar at the top for copying / pasting / cutting is not always shown especially if you select something at the bottom of the EditText or if the EditText is big

How can I address those issues?

EDIT

I don't want to set a maximum of lines, since we have different screen sizes and therefore a hard coded number is not really a good solution.

Niklas
  • 23,674
  • 33
  • 131
  • 170

2 Answers2

0

TRY:

android:maxLines="5" 

or

your_text_view.setMaxLines(5);

EDIT:

or you can hook up addTextChangedListener() to your edit text:

your_edit_text.addTextChangedListener(this);  
@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub

}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub
    //Here you can take necessary action if the edit text grows beyond some height
}
Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
  • I don't want to set a maximum lines, since we have different screen sizes and therefore a hard coded number is not really a solution. – Niklas Sep 16 '14 at 11:11
  • you can hook up a `onTextChangeListener()` and not allow new-lines – Sagar Pilkhwal Sep 16 '14 at 11:13
  • Don't you mean `addTextChangedListener(new TextWatcher ...` – Niklas Sep 16 '14 at 11:16
  • Even then it won't let me block the input. If I wanted to do that, I would have to use an `InputFilter`. – Niklas Sep 16 '14 at 11:19
  • yes `InputFilter` will be great idea if you want to completely disallow new line chars and limit your line. found this [post for inputfilter](http://stackoverflow.com/a/4401227/3326331) – Sagar Pilkhwal Sep 16 '14 at 11:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61339/discussion-between-niklas-and-sagar-pilkhwal). – Niklas Sep 16 '14 at 11:39
0

Try this :

 <EditText
            android:id="@+id/multi_line_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:scrollbars="vertical"
            android:maxLines="5"
            android:inputType="textCapSentences|textMultiLine"/>
Rustam
  • 6,485
  • 1
  • 25
  • 25