3
votes

This question is different from all the others already asked here.

Problem & question

I want the caps lock enable like if I double click (or long press) the shift key, when opening the keyboard. Another request is that the caps lock must be disable if the user presses the shift key.

I have already tried most of the proposed solutions in stackoverflow like android:inputType="textCapCharacters" or setAllCaps(true) but what happens is that the caps lock can't be disable. With the above solutions, upon pressing shift the user will insert one single character in lowercase and then the system automatically sets the keyboard back to caps lock.

This is not the correct way I want, I only want to have the caps enable the first time the user opens the keybaoard and then he will handle by himself the caps status.

Note

Keep in mind that I started the question with "like if I double click (or long press) the shift key", because using the inputType solution you have this situation: enter image description here That has not the white caps dash like if I manually enable caps lock: enter image description here

1
you can achieve this programatically by setting edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});Shivanshu Verma
use android:inputType="textCapCharacters" check below answerNaz141
Thank you @ShivanshuVerma for the try but it has the same behaviour than android:inputType="textCapCharacters": the case is always upper and there is no chance to put lower by clicking on the shift key.Link 88

1 Answers

0
votes

I have found the solution to the problem!

I have to keep using android:inputType="textCapCharacters" but when the user presses the shift key and type a single character in lowercase, a textwatcher removes the flag textCapCharacters.

As follow the textwatcher that does the trick:

public class EditTextWatcher  implements  TextWatcher{

    private EditText editText;

    public PtlEditTextWatcher(EditText editText) {
        this.editText = editText;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }

    @Override
    public void afterTextChanged(Editable s) {
        if (editText != null && s.length() > 0 && (editText.getInputType() & InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS) > 0)
            if (Character.isLowerCase(s.toString().charAt(s.length() - 1)))
                editText.setInputType(editText.getInputType() & ~InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
    }
}

a simple use of it is:

addTextChangedListener(new EditTextWatcher(myEditText));