How do I hide keyboard programmatically with java - java

I am trying to hide a numbers keyboard programmatically with java and I used this code in a onClick of a button
​        ​InputMethodManager​ inputMethodManager ​=​(​InputMethodManager​)getSystemService(​Activity​.​INPUT_METHOD_SERVICE​);
​        inputMethodManager​.​hideSoftInputFromWindow(view​.​getWindowToken(), ​0​);
But as a result when the user click the first time the numbers keyboard become strings keyboard (normal keyboard) and when the user click the 2nd time the keyboard hides.
I want the keyboard get hide from the first click, any help ?

Try this code for hiding keyboard:
/*--for hiding keyboard on click--*/
InputMethodManager imm=(InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view3=getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view3==null){
view3=new View(this);
}
assert imm != null;
imm.hideSoftInputFromWindow(view3.getWindowToken(), 0);

Related

Need soft keyboard to feed TextView, but I don't want the input field on-screen

I am able to open and close the soft keyboard on Android and accept text input to a TextView. All works fine. The problem is, I don't want to actually see the text input field above the keyboard that takes up the entire screen. I've set the TextView's input type to InputType.TYPE_NULL, which hides the text field and only shows the keyboard, but when I do this it does not accept input from the keyboard:
//doesn't show text field above soft
//keyboard, but also doesn't take input
tview.setInputType(InputType.TYPE_NULL);
I can't seem to figure out how to disable the text field. I only want the keyboard because I need to show things in the UI while the user is typing without the text input box covering the entire screen.
UPDATE: Turns out I could just send the key input from the soft keyboard directly to a non-TextView. In other words, it is possible to get soft keyboard input to any subclass of View, and it does not specifically need to deal with text or text editing. This answer explains it: https://stackoverflow.com/a/7386854/420163
I modified the onCreateInputConnection (which is part of the View class) to do this:
#Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
//Log.d(TAG, "onCreateInputConnection");
BaseInputConnection b = new BaseInputConnection(this, false);
outAttrs.actionLabel = null;
outAttrs.inputType = InputType.TYPE_NULL;
outAttrs.imeOptions = EditorInfo.IME_FLAG_FORCE_ASCII|EditorInfo.IME_FLAG_NO_FULLSCREEN|EditorInfo.IME_FLAG_NO_EXTRACT_UI;
return b;
}
The EditorInfo.IME_FLAG_NO_FULLSCREEN|EditorInfo.IME_FLAG_NO_EXTRACT_UI flags hid the text edit box that shows with the soft keyboard and kept it from covering the screen. The main view now receives key events from the soft keyboard using public boolean onKey(View v, int keyCode, KeyEvent event).
Try this.
tview.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
tview.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

Android - EditText- selectable, uneditable

I have been developing a simple Calculator Application in Android Studio.
The result window (where the numbers are placed for calculation) I initially set as a TextView. However, I haven't found a way to implement a cursor while using the TextView. My goal is to have the result window display a cursor and have text selectable but not editable. When using EditView I have tried to disable the soft keyboard, disable input, etc., with no success. Can I accomplish this using either an EditView/TextView?
Soft keyboard issue
Screenshot
I obviously do not want the user to be able to utilize the soft keyboard, but I still want to maintain the cursor and have the text selectable.
To hide keyboard keys in fragment interface use
EditText edt=(EditText)View.findViewById(R.id.editTextForNumbers);
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edt.getWindowToken(), 0);
If it is in an activity use
EditText edt=(EditText)findViewById(R.id.editTextForNumbers);
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edt.getWindowToken(), 0);
Set these properties for your edittext or textview
android:cursorVisible="true"
android:textCursorDrawable="#null"

Android, how to show software keyboard on click

I have a dialog box in my Android app; it is the only EditText control on the dialog (the others are spinners and buttons), and so it gets focus when the dialog is shown. This prevents the on-screen keyboard from ever showing up, meaning you can't easily enter any text into the box unless you have a hardware keyboard.
I believe (but I couldn't swear to it) that this is because the control starts with focus, and the system check as to whether to show the keyboard or not happens in the onfocus event. Is there any way the programmatically show the on-screen keyboard?
In order to implement the ability to force the keyboard open when the user presses a button on the screen then the following should help.
InputMethodManager inputMethManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
However an alternative to help regain focus of the dialog window can be found below. This code should open the software keyboard for you by resetting any flags initially set by AlertDialog. This code should be placed after the creation of the dialog window.
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

Is it possible to hide the keyboard with a button?

In my app, I have a Tab system, and in one of the tab layouts, there is an EditText.
And if I go to a different tab, the keyboard stays up.
So I've added a button that would hide it:
So how do I set the Button to closing the keyboard? It would also be very helpful if the keyboard went away when switching tabs.
private void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getTabHost().getApplicationWindowToken(), 0);
}
This method hides the keyboard. Works in tab case. Just set on the each tab the same OnClickListener, which calls hideKeyboard().
What i've done in my app is try and not use a button to close the softkeyboard cause i'm already using a button, instead of your Done, called Del whcih empties out the EditText field. What i've done is on the first press of the EditText show the keyboard, and at a second press just hide it.
Try using this in the onClick callback function for the EditText, OR if you want to keep the Submit button, then add this on it's onClick listener and for every tab you have as well.
InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Hope this helps, cheers.
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
add these to the event when you want to hide soft keyboard...

How to use android soft keyboard in SurfaceView

I have a SurfaceView that I draw onto, there are no controls, text boxes or any other sort of form-type item. I would like to be able to :
1: Touch one of the circles I draw on the SurfaceView onDraw() method and open the default android soft keyboard. (I know how to do the touch detection part, just need to open the soft keyboard somehow)
2: As I type I want the letters I select to be shown on the canvas (Again I know how to do the drawing part, but how do I capture the keys the user has selected in the first place?)
many tanks
David
you can try this to show the keyboard:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
if that shows it for you then I imagine you could listen for the key events by overriding the onKeyDown() method of your activity

Categories

Resources