0

I am trying to set Android sample soft keyboard to make first letter in sentence capital. It's the normal behavior of keyboard, but I don't know how to do this((

2 Answers 2

1

Must be a bit late to answer, but someone else might need this.

To capitalize the keyboard manually, you have to call your KeyboardView's setShifted (boolean shifted) method with desired shifted mode.

Here is an example:

private KeyboardView mInputView;
...
mInputView.setShifted(true);

But it is better if you let your SoftKeyboard to decide its capitalization mode by checking the attributes of the text editor. For example, emails usually are typed using lowercase letters. If you look at this sample here, it has a method named updateShiftKeyState(EditorInfo attr) which is used to set the capitalization mode automatically based on the initial attributes of the text editor in which the text will be typed in:

/**
 * Helper to update the shift state of our keyboard based on the initial
 * editor state.
 */
private void updateShiftKeyState(EditorInfo attr) {
    if (attr != null 
            && mInputView != null && mQwertyKeyboard == mInputView.getKeyboard()) {
        int caps = 0;
        EditorInfo ei = getCurrentInputEditorInfo();
        if (ei != null && ei.inputType != InputType.TYPE_NULL) {
            caps = getCurrentInputConnection().getCursorCapsMode(attr.inputType);
        }
        mInputView.setShifted(mCapsLock || caps != 0);
    }
}
1
  • great, you save my day Jul 30, 2020 at 9:12
0

Just set the android:capitalize xml attribute for the textview in your layout. Check the possible values for the attribute here

1
  • I develope keyboard, the textView may be from different application. I need set this logic in keyboard to capitalize letters in such textViews. Feb 17, 2013 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.