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?
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 have created an app where the user can enter some text and it searches the database once the user presses the search button. I have now modified this so it works with NFC. The NFC tags stores some text (Hello World) and once the tag has been tapped on the phone the text (Hello World) is inserted into the textview. The user can then press the search button and so on.
Is it possible to automatically press the search button once text has been entered into the textbox. For Example, The user taps their phone on the NFC tag, Hello World is entered in the textbox and the search occurs automatically without the user having to press the button.
Is this possible?
Thanks
When user taps their phone on the NFC tag, enter Hello World in the textbox and programmatically click SearchButton using performClick().
button.performClick();
Yes it can be by using textwatcher on editext.
SerachEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(s.toString().contains("HelloWord Or You unique NFC Text")){
//Enter here Button Pressed Code
}
}
#Override
public void afterTextChanged(Editable arg0) {
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
});
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
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"
I have a login page,having 2 textfields username,password and button "login" button.
when the username or password not entered alert is poping up,
when i suddenly clicks on this login button 2 alerts are poping up,
since more than click is taken.
I need to disable the button and enable during next click. What should i do?
in order to disable a button, you have to call
yourButton.setEnabled(false)
on a Button instance. To re enable it call:
yourButton.setEnabled(true)
create a method as below
boolean checkit= true;
private void showHide1() {
if (checkit) {
// first click
} else {
// second click
}
checkit= !checkit;
}
and on you button click add the method showHide1();
You can combine blackbelt's solution with mine.
Add a TextWatcher to your EditText and enable or disable the button when text is entered in the EditText.
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
charCount = Integer.valueOf(s.length());
if (charCount > 0) {
// ENABLE THE BUTTON
boolean blnButtonStatus = YOUR_BUTTON.isEnabled();
if (blnButtonStatus== false) {
YOUR_BUTTON.setEnabled(true);
}
} else if (charCount == 0) {
// DISABLE THE BUTTON
YOUR_BUTTON.setEnabled(false);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// DO NOTHING HERE
}
#Override
public void afterTextChanged(Editable s) {
// DO NOTHING HERE
}
This way, the code will automatically disable the Button if there is nothing typed in the Password and /or Username fields. No accidental clicks.