For my Android app I need to show numeric keyboard when the user clicks on an image, but without showing an EditText.
I want to then be notified when the user pressed a specific key, so that I can handle UI appropiately.
How could it be done?
Thank you
Try this...
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(imageview_reference.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
Related
I have an AppCompatActivity where I programmatically toggle the softinput. There is only one scenario where my keyboard open/close boolean is not set properly because I cannot intercept the back-button event. This event closes the keyboard when executed.
The following is printed when pressing the on-screen back button when a keyboard is opened.
I/ViewRootImpl#af03171[MainActivity]: The input has been finished in ImeInputStage.
I've tried all the toggleSoftInput variants/flags I could find to pray it would somehow circumvent the issue
I tried to catch the press with "dispatchKeyEvent", "backPressed", "onKeyUp", "onKeyDown", "onKeyPreIme" with no luck.
And as the message says the event is finishing somewhere else. I just can't find anything on the web to realize the functionality I need for my application.
In addition, this is my toggle function.
public void toggleSoftInput() {
InputMethodManager imm = (InputMethodManager) getAndroidContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
keyboardOpened = !keyboardOpened;
}
Edit: I've searched over any resource I could find and still haven't been able to resolve this edge case in my application. Any pointers are greatly appreciated.
I am developing an application in which i need to allow the user to change the input keys shown in the default keyboard, upon request or by default, for example, i may prompt the user at the beginning to select the default language and after that, whenever the default keyboard is used, the app always displays the keys of the keyboard the selected language,
I know this is possible, because in default keyboard app, when multiple input methods are selected, then long pressing the spacebar allows to change the input methods at runtime, if this is possible then my requirement is also possible...
I dont want to prompt for default keyboard like following:
InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imeManager.showInputMethodPicker();
I dont want to change the locale and restart my activity all the time like:
Resources res = getBaseContext().getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("ru".toLowerCase());
res.updateConfiguration(conf, dm);
Log.i("inside onStart","after ever");
setContentView(R.layout.activity_main);
I just want to show the keyboard input in my desired language.
You can change keyboard without user notification only and only if your app is running as a System app for security reasons.
You need to give Android permission first in your app's AndroidManifest.xml
"android.permission.WRITE_SECURE_SETTINGS"
Then you need to determine id of your keyboard.
-> To know id, you need to keep your keyboard default from setting menu manually then put this print somewhere,
System.out.println(Settings.Secure.getString(getContentResolver(),Settings.Secure.DEFAULT_INPUT_METHOD));
Once you print id and you know your keyboard id you can do as per below
( I have changed my default keyboard to Japanese )
InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
//imeManager.showInputMethodPicker(); //This is to see available keyboards.
imeManager.setInputMethod(null,"jp.co.omronsoft.openwnn/.OpenWnnJAJP");
Enjoy !!
After doing some research here and there found the answer, first of all you have to create a custom keyboard View which extends keyboardView and in it create static key value variable like
static final int KEYCODE_LANGUAGE_SWITCH_ENG = -102;
static final int KEYCODE_LANGUAGE_SWITCH_URDU = -103;
after that in your IME class where you have implemented the inputMethodService, create the keyboards inside the onInitializeInterface override function. like
mSymbolsKeyboard = new Keyboard(this, R.xml.qwerty2);
mEngQwertyKeyboard = new Keyboard(this, R.xml.eng_qwerty);
after this add these final static keys in the onKey override function as switch cases, and in the cases set the keyboards accordingly:
setKeyboard(mEngQwertyKeyboard);
I came across this piece of code but I am wondering what this code does and what exactly is InputMethodManager and where should I type in this code in my class? Will it go in the onCreate() method or should I make a new method? And again, I want to understand how this method works.
Thanks for your answer in advance :) I appreciate the help
InputMethodManager inputManager =
(InputMethodManager) context.
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(
this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
The javadoc of InputMethod is quite descriptive about it
Central system API to the overall input method framework (IMF) architecture, which arbitrates interaction between applications and the current input method. You can retrieve an instance of this interface with Context.getSystemService().
In your particular case you are interseted in this use case
An input method (IME) implements a particular interaction model allowing the user to generate text. The system binds to the current input method that is use, causing it to be created and run, and tells it when to hide and show its UI. Only one IME is running at a time.
Also from the description of hideSoftInputFromWindow you can extract
public boolean hideSoftInputFromWindow (IBinder windowToken, int flags)
Synonym for hideSoftInputFromWindow(IBinder, int, ResultReceiver) without a result: request to hide the soft input window from the context of the window that is currently accepting input.
Parameters
windowToken IBinder: The token of the window that is making the request, as returned by View.getWindowToken().
flags int: Provides additional operating flags. Currently may be 0 or have the HIDE_IMPLICIT_ONLY bit set.
This makes this in your code refer to a View, so that code is part of a class that extends View
Here is an example of its usage.
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
I am attempting to use a swipe gesture to finish a fragment, and if the fragment is displaying the keyboard, the keyboard will be hidden. The following is a sample:
public void finishFragment() {
View focus = getCurrentFocus();
InputMethodManager imm = null;
if (focus != null) {
imm = (InputMethodManager) focus.getContext().
getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(focus.getWindowToken(), 0);
}
}
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
}
}
Unfortunately, since I am calling a service along side the fragment stack, the service is hanging up the UI display, thus giving it a laggy appearance. Does anyone know how I can hide the keyboard without calling a service or should I call an Async process? Even better, does anyone know how onBackPressed() removes the keyboard?
There are only 2 ways to control the keyboard on Android:
Programmatically with the InputMethodManager like you are doing (which will as far as I know always requires you to call .getSystemService() to use)
Using android:windowSoftInputMode="X" to specify the behavior of the keyboard in the context of the given activity tag in the AndroidManifest
So to answer your questions of: "Does anyone know how I can hide the keyboard without calling a service"? You could do the following in the AndroidManifest, where ActivityX is the activity you want to have this type of behavior.
AndroidManifest.xml:
<activity
android:name="com.namespace.of.ActivityXYZ"
android:windowSoftInputMode="stateHidden" />
This will serve to hide the keyboard initially for ActivityX, but I am not sure if this will give you the desired behavior/lack of laggyness when swiping to finish the fragment. Also try changing "stateHidden" to "stateAlwaysHidden" or "stateUnchanged". Checkout what these do here if you do not know.
Another approach would be: instead of checking the focus and getting the keyboard programmatically like you are doing, why not just finish() the activity managing the fragment you wish to end? This will also hide the keyboard if it was open.
Hope this helps!
I am unable to press 'Go' button on searching something on Nexus 7' tablet. We don't have any text or content description for the 'Go' button on the keyboard. I tried using following -
//Search something say "fun"
new UiObject(new UiSelector().text("Enter URL or Search & Win")).setText("fun");
getUiDevice().pressEnter();
OR
getUiDevice().pressSearch();
Also tried :
getUiDevice().pressKeyCode(66); //for enter
getUiDevice().pressKeyCode(84); // for search
But this is not working.
Could anyone help me out with this.
Thanks
Try using the button attribute with reference to index.
i.e :
UiObject cancelButton = new UiObject(new UiSelector().className("android.widget.Button"));
To click on "Done" button with UIAutomator just try below code
use
just make sure that correct layout in which input keyboard is open is used
UiObject(new UiSelector().resourceId(LAYOUTID)).clickBottomRight();