I'm trying to hide my keyboard when Edittext is tapped.
problem is when setting an OnClickListener I have to double tap the Edittext if i want the hideKeyboard(); function to start.
Is it possible to set 2 OnClickListeners or something like that so it will appear like a double tap?
this is how i hide the keyboard:
private void hideKeyboard() {
System.out.println("Hiding keyboard");
inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
I have this code in my android app, for an alert dialog and keyboard, and when the dialog is shown I want the keyboard to appear. But its not and I am not sure why?
Here is my code:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Log.i(TAG, "in focus");
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);
}
}
});
But the keyboard does not appear even though the edit text is in focus, and I tell it to appear why, doesn't it come up?
Thanks for the help in advance.
I tested your code and the keyboard opened when alert.show() was executed. If you are using an emulator be sure that you have the virtual keyboard enabled. Otherwise a keyboard will never appear on the device screen.
How do I freeze the listview scroll when keyboard pops up and then regain scroll after keyboard is hidden.
I cannot seem to start anywhere
In On ScrollState Change check keyboard is open or not:
public void onScrollStateChanged(AbsListView view, int scrollState) {
//check keyboard is open?
InputMethodManager imm = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
writeToLog("Software Keyboard was shown");
} else {
writeToLog("Software Keyboard was not shown");
}
}
and stop listview with
listView.setScrollContainer(false);
I need to hide the android keyboard after a button click.
I have seen many examples of how to do this, however, they all appear to use a specific editText object.
e.g.
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
My problem is that I am building the screen dynamically, thus there could be mane edit text fields. Is there a way the keyboard can be hidden without me having to specify which editText object I am hiding it for.
You could instead set it to your layout, ie:
LinearLayout mainLayout;
// Get your layout set up, this is just an example
mainLayout = (LinearLayout)findViewById(R.id.myLinearLayout);
// Then just use the following:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mainLayout.getWindowToken(), 0);
This is an example assuming that your layout will be created regardless of how many EditText objects (or other objects) are placed on it.
Edit: Also, something I find very useful is to make sure that the keyboard is hidden when an activity first launches (ie: if an EditText is the first thing focused). To do that, I put this in onCreate() method of Activity:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Dont forget to use try catch blog because in case when your keyboard not open and if you use key key board hide code app will crash
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
You can hide the keyboard using the following code, probably on the Button click Event :
//================ Hide Virtual Key Board When Clicking==================//
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow("Your Button/EditText Object".getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
//======== Hide Virtual Keyboard =====================//
If the problem is on an activity simply the following will work:
try {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
else if the code is required in a fragment do the following
try {
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
This will handle the hiding of keyboard on a button click or any other event as deemed specific when written within the event block.
edittext.onEditorAction(EditorInfo.IME_ACTION_DONE);
For the record and based in the answers of #burmat and #Prashant Maheshwari Andro
Let's say that you call the click button as follow. where buttonAndroidLogin_button is the Button object.
protected void onCreate(Bundle savedInstanceState) {
// your code...
buttonAndroidLogin_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hideKeyboard((Button)v);
// ....
} // end onCreate
On the activity, you must set the next method
public void hideKeyboard(View view) {
try {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch(Exception ignored) {
}
}
It hides the input using the same button, so we don't need any linear layout, text view or any other arbitrary control. Also, the code is reusable for more buttons.
You use this code
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
IN KOTLIN :
In your fragment :
Create extension like this :
fun Fragment.hideKeyboard() {
val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(requireView().windowToken, 0)
}
Then use it like this :
hideKeyboard()
In your activity :
Create extension like this :
fun AppCompatActivity.hideKeyboard() {
val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.window.attributes.token, 0)
}
Then use it like this :
hideKeyboard()
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
I want to open calculator which I created. This is the code I wrote:
TextView.OnClickListener listener = new TextView.OnClickListener(){
public void onClick(View v) {
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textOut1.getWindowToken(), 0);
imm.hideSoftInputFromWindow(textOut2.getWindowToken(), 0);
imm.hideSoftInputFromWindow(textOut3.getWindowToken(), 0);
imm.hideSoftInputFromWindow(textOut5.getWindowToken(), 0);
startActivity (new Intent("com.easyPhys.start.calculator"));
}
};
textOut1.setOnClickListener(listener);
textOut2.setOnClickListener(listener);
textOut3.setOnClickListener(listener);
textOut5.setOnClickListener(listener);
But what happens is virtual keyboard opens and my calculator opens only than I press ENTER. What is wrong with my code?
Try the onClick event listener on the TextView, instead of the OnEditorActionListener.
For it to work you also need to add the following attribute to the TextView in the xml.
android:clickable="true"
Don't forget it, without it it won't work.
The OnEditorActionListener only fires when some action is performed on the editor, and that's why the activity only shows when you click the keyboard. On the other hand, the onClick listener should fire right after you click the TextView.