Search code examples
androidkotlinandroid-edittextime

Android EditText MultiLine with Sentence Caps


I am trying to instantiate an EditText with mutli-line capability as well as the first character capitalization flag, like so:

myEditText?.setSingleLine(false)
myEditText?.inputType =
  InputType.TYPE_CLASS_TEXT or
  InputType.TYPE_TEXT_FLAG_CAP_SENTENCES or
  InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE
  myEditText?.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION)

I've also tried combining the flags with the plus instead of or:

myEditText?.setSingleLine(false)
myEditText?.inputType =
  InputType.TYPE_CLASS_TEXT +
  InputType.TYPE_TEXT_FLAG_CAP_SENTENCES +
  InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE
  myEditText?.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION)

But, for some reason, I could only get multi-line or first-character capitalization, but not both. The result is either a keyboard with no return key, or a return key that just adds spaces to the same line.

Am I missing a flag, or perhaps using the worng ones?


EDIT: I forgot to mention that my UI is purely built by code with no XML layout file.


Solution

  • Recently, I found out that when we set inputType, it will override singleLine settings.

    1. Set input type.

    2. Set single line = false.

    --

    editText.inputType = InputType.TYPE_TEXT_FLAG_MULTI_LINE or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES
    editText.setSingleLine(false)
    

    I am not setting imeOptions. This seems to work well.

    This seems to work on Emulator and Google Pixel 3xl.