android - Enable caps lock (double click shift like) programatically on android - android

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:
That has not the white caps dash like if I manually enable caps lock:

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));

Related

Android - catch backspace (delete) button on custom EditText

I want to catch the backspace/delete button event in soft keyboard on EditText custom.
How can I do it ?
I tried these solutions but they do not work for me :
Android custom EditText and back button override
EditText OnKeyDown
Get back key event on EditText
Android EditText delete(backspace) key event
Thanks for your help !
EDIT :
I found a fix for that isssue with the function DispatchKeyEvent :
#Override public boolean dispatchKeyEvent(#NonNull KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
// ...
}
return super.dispatchKeyEvent(event);
}
I found a fix for that isssue with the function DispatchKeyEvent :
#Override public boolean dispatchKeyEvent(#NonNull KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
// ...
}
return super.dispatchKeyEvent(event);
}
Catching Key Events from soft input methods in Android is unreliable. Here is an excerpt from the JavaDoc for the KeyEvent class:
As soft input methods can use multiple and inventive ways of inputting
text, there is no guarantee that any key press on a soft keyboard will
generate a key event: this is left to the IME's discretion, and in
fact sending such events is discouraged. You should never rely on
receiving KeyEvents for any key on a soft input method. In particular,
the default software keyboard will never send any key event to any
application targetting Jelly Bean or later, and will only send events
for some presses of the delete and return keys to applications
targetting Ice Cream Sandwich or earlier.
One workaround could involve using TextWatcher. Whenever a delete occurs in an EditText, the character count drops.
myEditText.addTextChangedListener(this);
//...
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count == 0) {
//text was deleted.
}
}
Edit: This is untested, so may not be completely reliable. In addition, it's worth pointing out that count may also read 0 if a long section of text is selected and then replaced. I created a library once that was designed to add Undo/Redo functionality to EditText, but can also identify the difference between text being replaced and deleted, so this may be of use to you if you require more accuracy.
Please, take a look at a solution I provided in another similar topic: Android - cannot capture backspace/delete press in soft. keyboard. You need to build your custom EditText class and override onCreateInputConnection method in it. It will give you access to deleteSurroundingText event which is firing when you click backspace. I've tested it in some devices and hope it will work in others also. Try it and give a feedback.
Here you go..
editText.addTextChangedListener(new TextWatcher() {
private int before;
private int after;
private int prevLength;
private boolean isBackPressed;
ArrayList<Integer> arrayList;
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
prevLength = charSequence.length();
}
#Override
public void onTextChanged(CharSequence charSequence, int start, int end, int count) {
}
#Override
public void afterTextChanged(Editable editable) {
isBackPressed = prevLength > editable.length();
if(isBackPressed){
Toast.makeText(getApplicationContext(),"Back",Toast.LENGTH_LONG).show();
}
}
});

Adding and deleting a keyboard without edittext in a fragment

I'm creating a remote control that also let's people enter characters for a Minix android dongle. Via another app on a tablet or smartphone, people can enter data that is send to the dongle for input purposes.
I'm trying to create a keyboard programmatically when i push on a button, with which I can get the keys that are entered in on the keyboard. I also have to delete the keyboard when i click on ANYTHING ELSE then the keyboard. This is the important part, since I'm using fragments and there are other views on the screen which, when pressed, also have to remove the keyboard.
So how can i know when there is a touchinput on the phone/tablet that is not on the keyboard? I've tried the onfocusChanged, but this didn't work since my focus won't change if I press something on my screen.
Currently I'm using this code, which allows me to show a keyboard when I press a button and intercept all the text that is entered on the keyboard.
mKeyboard = (Button) view.findViewById(R.id.buttonKeyboard);
mKeyboard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final EditText plainTextInput = (EditText) view.findViewById(R.id.PlainTextInput);
plainTextInput.requestFocus();
InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(plainTextInput, 0);
plainTextInput.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e(TAG, s.toString());
}
#Override
public void afterTextChanged(Editable s) {
}
});
plainTextInput.getLayout();
}
});
Now how can I know when the user has pressed somewhere outside the keyboard, so I can close the keyboard? Or are there other ways of adding a keyboard without invisible edittexts?

Close popoup when user start input

I have app with edittext where user simply writes something with the softkeyboard. After user will click at the button and he will see popupwindow, in this case user still have seen keyboard. When user will click outside the popup, it should disappear, and it work ok, but if user will click at keyboard, popup won't disappear. Maybe someone has deal with same problem and can help me.
Steps:
Open app
Invoke keyboard
Invoke popupwindow
Start input
result: Popup is not hidden, text is not inputted
If I understand you correctly, you want that the keyboard to disapper when the user clicks on it?
If so, do this for your EditText:
final EditText sample = (EditText) findViewById(R.id.popupET);
sample.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(sample.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
What it does is simply wait for the user to input any key (press on the keyboard) and then it hides it.
The sample EditText is the field you currently have in your code.
Let me know if everything works

android input method problems

A few questions about android:
Is it possible to replace the keys in default keyboards ? For example, is it possible to replace the dot in the numeric keyboard with a comma ?
I wrote a very simple IME, but I cannot set it to an EditText. What I want is to set one of my EditText to use the IME I wrote by default, not the default LatinIME. Is that possible ? How inputMethod attribute works ? I set the fully qualified class name of IME but it raises class not found exception.
Thanks.
Is it possible to replace the keys in default keyboards ?
You don't. Users are in control over their device, including what keyboard gets used.
But You can try to make some input methods
Read this tutorial: Creating an Input Method
clone this repo: LatinIME
And if replacing one character is your requirement, you can override text change listener of edittext, and check each entered character and if user entered dot then replace that with comma as
editText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
//Check if s contains dot and replace it with comma
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
But this method executes each key hit in EditText.
try override this method.return another keyCode and see the reslt
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == someKeyCode) {
//...... button is pressed
}
return super.onKeyDown(keyCode, event);
}
Pretty sure replacing keys in the default keyboard is not possible, you would need to write your own keyboard replacement, much like all the keyboard Apps do.

Android EditText change focus after validation and showing the error in a Dialog

I have a simple Activity with 3 EditText fields.
User, Pass, Confirmation
After typing something in the User field and the person clicks next on the keyboard, I have a setOnFocusChangeListener on there that will validate the input. If the validation fails a Dialog opens with a message and an OK button.
After the Dialog is closed I tried a requestFocus on my User EditText in many variations, by releasing it on Pass, by trying to release on User again, by requesting than clearing and requesting again but when I click on another field the softkeyboard won't open again or I end up with two EditText fields with the blinking cursor.
Any ideas?
I suggest validating the user's input with a TextWatcher:
EditText textbox = new EditText(context);
textbox.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// Your validation code goes here
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
Only handle validation in the afterTextChanged method, don't touch the other two, as advised in the documentation. However afterTextChanged get's fired, every time the input changes, so if the user enters the word "hello" this method get's called when h is entered, then again when e is entered and so on... Furthermore, if you modify the edittext value in afterTextChanged, the method get's called too.
An alternative is to validate the user input when the EditText loses focus. For this purpose you could use:
textbox.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// Your validation code goes here
}
});
However beware, that some widgets might not grab focus, so your Edittext never loses it (had that with Buttons for instance).
Furthermore the EditText offers a setError method, which marks the edittext with a red error mark and shows the text passed to setError to the user (the text can be set by you when calling setError("Your error message")).

Categories

Resources