Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent enter key on EditText but still show the text as multi-line

How do I make an EditText on Android such that the user may not enter a multi-line text, but the display is still multi-line (i.e. there is word-wrap instead of the text going over to the right)?

It's similar to the built-in SMS application where we can't input newline but the text is displayed in multiple lines.

like image 613
Randy Sugianto 'Yuku' Avatar asked May 20 '11 10:05

Randy Sugianto 'Yuku'


People also ask

Which allows editing of multiple lines of text?

By default all the EditText widgets in Android are multi-lined.

How do I show soft keyboard when EditText is focused?

android:windowSoftInputMode="stateAlwaysVisible" -> in manifest File. edittext. requestFocus(); -> in code. This will open soft keyboard on which edit-text has request focus as activity appears.


1 Answers

I would subclass the widget and override the key event handling in order to block the Enter key:

class MyTextView extends EditText {     ...     @Override     public boolean onKeyDown(int keyCode, KeyEvent event)     {         if (keyCode==KeyEvent.KEYCODE_ENTER)          {             // Just ignore the [Enter] key             return true;         }         // Handle all other keys in the default way         return super.onKeyDown(keyCode, event);     } } 
like image 54
Tony the Pony Avatar answered Sep 23 '22 03:09

Tony the Pony