1

I want to have 5 lines in an edittext, so the user can press Enter just 4 times, I tried like this but still can't work

myEtidtext.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    // if enter is pressed start calculating
    if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
        // get EditText text
        String text = ((EditText) v).getText().toString();
        // find how many rows it cointains
        int editTextRowCount = text.split("\\n").length;
        // user has input more than limited - lets do something
        // about that
        if (editTextRowCount >= 5) {
            // find the last break
            int lastBreakIndex = text.lastIndexOf("\n");
            // compose new text
            String newText = text.substring(0, lastBreakIndex);
            // add new text - delete old one and append new one
            // (append because I want the cursor to be at the end)
            ((EditText) v).setText("");
            ((EditText) v).append(newText);
        }
    }
    return false;
}
});
user user
  • 2,122
  • 6
  • 19
  • 24
  • Have tried android:maxlines=4 in your xml or setMaxlines(int) method in your java or your refer this link http://stackoverflow.com/questions/7092961/edittext-maxlines-not-working-user-can-still-input-more-lines-than-set – Pragnani Feb 03 '13 at 13:08
  • i already tried that and have seen the qestions – user user Feb 03 '13 at 13:10

2 Answers2

0

Link to dynamically set the characters length limit for the edit text box -SOURCE CODE is also availiable : http://www.mobisoftinfotech.com/blog/android/android-edittext-setfilters-example-numeric-text-field-patterns-and-length-restriction/#comment-174948 filter and length restrictions also available

Venus
  • 219
  • 1
  • 3
  • 13
0

You can set the number of chars in that EditText to (for example 300 chars)

et_description.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // 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 afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                if (s.toString().length() > 300) {
                    et_description.setText(s.toString().subSequence(0, 300));
                }
            }
        });
William Kinaan
  • 28,059
  • 20
  • 85
  • 118