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);
Related
This question already has answers here:
How to close/hide the Android soft keyboard programmatically?
(126 answers)
Closed 3 years ago.
I am sharing some text to other apps using android default sharing
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, sharingString);
shareIntent.setType("text/plain");
startActivity(shareIntent);
The Issue:
When I share to another app i.e (WhatsApp, slack, etc) open the keyboard in that app (i.e. to edit the message, etc.), and then send or return back to my application. in some cases the keyboard remains open in that app and when I try to close the keyboard on resume using the following code it is not working.
public static void hideKeyBoard(Activity activity, View view) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (view == null) {
view = new View(activity);
}
if (imm != null) {
if(view.getWindowToken()!=null)
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
if(view.getApplicationWindowToken()!=null)
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
view.clearFocus();
}
And calling the function from onResume() of the fragment
if(getActivity()!=null){
Utils.hideKeyBoard(getActivity(),getActivity().getCurrentFocus());
}
I have found many answers related to hiding the keyboard but any answer is not working in this case.
Note: in the normal case, when I use the hideKeyBoard() method, it is working perfectly. It is just this case that is not working. Can anyone help me?
Edit
As I've mentioned above. that why this quesiton is not like the prviosly answered ones is explained in the above note. So kindle Plesase read that. And I've also tried this link. but not working.
You can try below code that may help you.
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Here is some bonus
create activity instance instead use getActivity() methos on fragment. getActivity() returns null when fragment not visible
on your fragment host activity
public static MainActivity mainActivity;
public static MainActivity getInstance() {
return mainActivity;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivity = this;
}
inside onResume() call hideKeyboard(MainActivity.getInstance())
Also, don't forget to add android:windowSoftInputMode="stateAlwaysHidden" in your Manifest
When the user clicks on an editText the keyboard is brought up for the user to type. Can I make the keyboard to appear when a user clicks on a button instead of the editText? Can the numpad appear instead of the normal keyboard?
Can i make the keyboard to appear when a user clicks on a button
instead of the editText?
Yes, you need to set the focus and pop up the keyboard using InputMethodManager
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Focus the field.
editText.requestFocus();
// Show soft keyboard for the user to enter the value.
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
Can the numpad appear instead of the normal keyboard?
Yes, using input type
Either in the xml tag of edittext
<EditText...
android:inputType="number"/>
or in java
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
public static void toggleKeyboard(Context context) {
try {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
} catch (Exception e) {
Log.e("On show keyboard error: %s", e.getMessage());
}
}
I am changing the dataset of the recyclerview adapter with different search query results. I am able to populate the data as well but the populated data only shows if the keyboard is closed.
This query("a") is valid but the result is shown only after I close the keyboard
Why not remove the keyboard as soon as the data is populated.
try {
InputMethodManager imm = (InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
}
This question already has answers here:
How to close/hide the Android soft keyboard programmatically?
(126 answers)
Closed 8 years ago.
I am creating a calculator app where i have an edittext where the result is shown.I want the data to be entered in the edit text only on clicking the number buttons NOT by the android keyboard.How do i disable the keyboard in my android app.Please help!
Try
editText.setInputType(EditorInfo.TYPE_NULL)
Or
((EditText) findViewById(R.id.LoginNameEditText)).setFocusable(false);
Or
editText.setKeyListener(null);
Try this :
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
try below code:-
EditText ed = (EditText)findViewById(R.id.editText1);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(ed.getWindowToken(), 0);
ed.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(ed, InputMethodManager.SHOW_IMPLICIT);
}
});
or
Window.SetSoftInputMode (SoftInput.StateAlwaysHidden);
If other situations you can prevent the keyboard showing simply using in the manifest
android:configChanges="orientation|keyboardHidden"
In this way the Android keyboard will not auto-open but is shown only if you click directly the EditText
Just posting an Answer what #yahya already commented.
You should consider using a TextView which is not editable and hence won't make any calls to the Keyboard.
In onClick() event of your button's call the setText(String) method and the apply the text you wish. :)
Ypu can also easily append things as setText(yourTextView.getText().toString + "Your New Text");
Hi Kindly try this: For this you no need any Widgets Context...
InputMethodManager miInputManager = (InputMethodManager) Activity.this
.getSystemService(Context.INPUT_METHOD_SERVICE);
miInputManager.hideSoftInputFromWindow(Activity.this
.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
Use
android:windowSoftInputMode="stateHidden"
for <activity> in Android_Manifest.xml
I am displaying a dialog with an edittext view. However, the softkeyboard will open only if the user presses inside the editview. So I tried calling an InputMethodManager with the following code.
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(dialogField,0);
The dialogField is the input field. However, when exactly am I supposed to do this? I tried it in the onStart() method of the dialog, but nothing happens. I also tried requesting the focus for the dialogField before, but that changes nothing.
I also tried this code
dialogField.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
public void onFocusChange (View v, boolean hasFocus)
{
if (hasFocus)
{
Main.log("here");
dialogInput.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
/*
InputMethodManager mgr =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(dialogField,0);
*/
}
}
});
in both versions. But no soft keyboard would like to appear. The Main.log is just a log, which shows me that the function is actually called. And yes, it is called.
I could get the keyboard with the SHOW_FORCED flag before the dialog opens. But then it will not close on exit. And I can only do that BEFORE I show the dialog. Inside any callbacks it does not work either.
Awesome question, I was trying to do that too and found a solution.
Using the dialog builder class AlertDialog.Builder you will have to invoke the dialog like this:
AlertDialog.Builder builder = new AlertDialog.Builder();
AlertDialog dialog;
builder.set...
dialog = builder.create();
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
This worked fine for me.
Note: you must import android.view.WindowManager.LayoutParams; for the constant value there.
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.show();
Window window = dialog.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Kotlin
Here's the tested code.
val dialog = AlertDialog.Builder(requireContext()).apply {
setTitle(…)
setView(editText)
setPositiveButton(…)
setNegativeButton(…)
}
val window = dialog.show().window
window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
Make sure you access the window property from show() method. Getting window from create() method was returning null for me, so the keyboard wasn't showing.
Import AlertDialog from androidx.appcompat.app.AlertDialog.
Import WindowManager from android.view.
Dialog Fragment With Kotlin
override onStart Method
override fun onStart() {
super.onStart()
dialog.window?.
setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
)
}
if you want to close after dismiss then override dismiss method with below code
override fun onDismiss(dialog: DialogInterface?) {
val inputMethodManager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_IMPLICIT_ONLY)
}
Here's my solution, it's working well for dialog.
txtFeedback.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Maybe also you need to add this to your activity tag in AndroidManifest.xml for closing the keyboard when the dialog is dismissed.
android:windowSoftInputMode="stateAlwaysHidden"