When testing an android project on an emulator the keyboard seems to work fine. However, when I test on my samsung phone the keyboard works incorrectly. One problem is that when my first display comes up the keyboard automatically appears although it doesn't in an emulator. Secondly, if I type something and hit the return or enter key, the keyboard won't disappear on my phone. Is there a separate step needed to dismiss the keyboard? Thirdly, sometimes when I hit hit the enter key, it causes the cursor to go to a new line rather than submitting the data. Again, none of this is a problem with the emulator. So what do I have to do to make the keyboard work correctly on my phone device?
Below is my code for receiving and submitting data with ana AutocompleteText view.
autoComplete = (AutoCompleteTextView) findViewById(R.id.suggest);
autoComplete.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable editable) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
String newText = s.toString();
int len = newText.length();
// Toast.makeText(getApplicationContext(), "Text changed count = " + String.valueOf(len), Toast.LENGTH_LONG).show();
if(len > 1)
new getData().execute(newText);
}
});
Sometimes when I hit the enter key, it goes to another line rather than executing my asynchronous task.
you required to forcefully open and close the soft keyboard for your requirements.
for Open soft keyboard:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(your_edit_text, InputMethodManager.SHOW_FORCED);
for Close soft keyboard:
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(your_edit_text.getWindowToken(), 0);
for correct working done(Enter) key you should put following properties to Edit Text in .XML file.
android:singleLine="true"
android:imeOptions="actionDone"
Related
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));
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?
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
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.
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")).