I'm new to mobile automation and I've been trying to find a way to close the soft keyboard on Android (using Java). The best solution I've come across so far was from this post:
Close/hide the Android Soft Keyboard
The function that I'm trying to use from there is:
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);
}
I want to be able to call this function when the keyboard pops up, but what I don't understand is how to pass through the Activity when I call this function?
Before calling the function to close the keyboard I have the following:
// Create object of DesiredCapabilities class and specify android platform
DesiredCapabilities capabilities = DesiredCapabilities.android();
// set the capability to execute test in android app
capabilities.setCapability(MobileCapabilityType.PLATFORM, Platform.ANDROID);
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Emulator_1");
capabilities.setCapability(MobileCapabilityType.VERSION, "8.0");
capabilities.setCapability("appPackage", "com.spreeza.shop.stag.debug");
capabilities.setCapability("appActivity", "com.spreeza.shop.ui.features.splash.EntryPointActivity");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
// click on the login button
driver.findElement(By.id(identifierName)).click();
// close the keyboard
hideKeyboard();
Activity => activity mean a reference of a Activity object.
Call the method as below .
hideKeyboard(YourActivity.this);
Checkout your Activity name and replace YourActivity with the name .
You can call the function from Activity as hideKeyboard(MainActivity.this) and from Fragment as hideKeyboard((Activity)getActivity())
I managed to find a simple solution that works for me:
http://discuss.appium.io/t/can-we-hide-android-soft-keyboard/6956/4
I just added in these 2 capabilities:
capabilities.setCapability("unicodeKeyboard", true);
capabilities.setCapability("resetKeyboard", true);
/**
* Hide the keyboard
*/
private void hideKeyboard() {
View view = getCurrentFocus();
if (view != null) {
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).
hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Related
Hey,
In my app I have a Drawer. When it's open the editText from the underlying fragment still works. Does anyone know how I can fix this?
At the red arrow is an editText that shouldn't respond right now, when the Drawer is opened.
x
A very simple way is to just hide the keyboard then the drawer opens. The only way to get the keyboard showing again is to click the edittext.
Here is one way to hide it:
fun hideKeyboard(activity: Activity) {
val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
//Find the currently focused view, so we can grab the correct window token from it.
var view: View? = activity.currentFocus
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = View(activity)
}
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
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
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.
I need to display the virtual keyboard when the application starts, but so far I've failed.
I use this code in method "OnCreate"to display the virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(txtBuscar.getId(), InputMethodManager.SHOW_FORCED);
this code works fine on any screen at any time, but does not work when the "first" activity begins. Why?
I tried it when I start another activity and it works, but does not work when I start the "first"activity.
I tried to put this code in the events "OnCreate"and many more .... but it seems not work.
is there anyway to "force" to display the keyboard when I start the application?
Thanks in advance.
I Found the solution:
txtPassword.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(txtPassword, 0);
}
},200);
Thanks !!!
onCreate will not be called if the activity is first brought from background. Have you tried put that code in onResume?
onCreate is called only when activity is first start or the activity is killed and user navigate to the activity again. So if the activity is still alive but in background, it will not call onCreate.
On the other hand, onResume will be called every time the activity comes to foreground (visible on screen) from background.
Here is link to activity life cycle if you are interested http://developer.android.com/reference/android/app/Activity.html.
Hope it helps.
I faced with the same issue, this method below helped me
public static void showKeyboard(Context context) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
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"