Android: How to trigger any key pressing event on soft keyboard? - android

Is there any way to press key of Android Soft Keyboard programmatically.
Like: When keyboard will appear, I want to press "J" key through my code not from fingers.

First method:
IBinder binder = ServiceManager.getService("window");
IWindowManager manager = IWindowManager.Stub.asInterface(binder);
manager.injectKeyEvent(new KeyEvent(KeyEvent.yourAction, KeyEvent.yourKeyCode),true);
You can see more details here.
There is another method in this link too.
Second method, using instrumentation:
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
And you can see this question that explains how to use instrumentation and webview to do that.
You don't need the keyboard to do this, you can show it or not.
A list of keyCodes if you want.
This link will show keyCode for every key that you press, I think it works with the android and linux keyboards, but don't know if the code will be the same using another OS.

Related

InputMethodManager.showInputMethodPicker not showing when custom keyboard in my app is not currently selected

I am implementing a custom keyboard with InputMethodService. Calling showInputMethodPicker from a service that is a chat head (a floating widget like in facebook messenger) when the selected keyboard is my custom one works but if current selected keyboard is something else e.g. Gboard, the input method picker UI doesn't show up, no error message on Run console in android studio. By selected keyboard, i mean the default keyboard in settings.
// inside main activity, start 'chat head' service when a button in app is clicked
startService(Intent(this#MainActivity, FloatingViewService::class.java))
// inside 'chat head' service, show picker UI when a button in expanded 'chat head' menu is clicked
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
imm.showInputMethodPicker()
How to make showInputMethodPicker work even though my custom keyboard is not selected or is it not possible? Not familiar with Android, so alternative approach is welcome (if the method above is wrong from the start)
Followed this blog post for creating the 'chat head' and this stackoverflow post for how to use showInputMethodPicker
related post, question mentions about using a postDelay, so my current workaround is: when button clicked in chat head service, create an intent to start activity with a boolean data using putExtra, activity onCreate gets called, if the boolean is true, call showInputMethodPicker after some delay (500ms for me works), set boolean to false
start activity from service details
delay using postDelayed details

Android Edittext close keyboard event

Im trying to resolve a usability problem in my app, im using Xamarin Android but for this question i think we can ignore Xamarin.
I have a list where each ítem has a edittext like a counter for each ítem.
The user is capable of changing that value by clicking on + and - buttons or directly by writing in the edittext with the softKeyboard.
My problem shows when the user opens the keyboard, change the number, but do not click on done but instead just close the keyboard using the backbutton.
Im using something like this to get the counter change.
editCount.EditorAction += delegate (object sender, TextView.EditorActionEventArgs e) {
if (e.ActionId == Android.Views.InputMethods.ImeAction.Done)
{
Of course, thats not being called if the user simply close the keyboard instead of hitting done.
I know i could use TextChanged instead of EditorAction but it will be a real nightmare to make those changes to this app...
Just forcing the user to hit the done button to close the keyboard should be enought, much better if i can restore the previous value of the edittext in case they just close the keyboard.
How can i achieve something like this?

In keyboard app, I want to return to my keyboard app after changing another

I create a keyboard app.
It changes to another by the functionality when it is fulfilled with specific situation.
This method is that switchInputMethod() which inherits InputMethodService class. And I tried to change the keyboard temporary.
However, when I call switchInputMethod(), it behaves as changing “Default” App of Android device.
After that, I can’t show my keyboard app.
In this case, I reselect “Default” of “language ant input” from “Setting” in my device.
I’d like to realize the following.
There is Activity has EditText1, EditText2 and EditText3.
In the case of EditText1 and EditText3, I input with my keyboard app.
And EditText2, I do with another.
[[add image]]
If its your app that's running, you can override the default input method for a given field via android:inputMethod in xml on the TextView or setInputMethod in Java. Then it would use that input method for the TextView and return to default for others.

Is it possible to listen for touches on the physical Android back button?

I couldn't find it in the docs, is there a module, or some other way to catch events from the android back button? If not it would be a nice and probably quick module to add.
No: the back button just pops you one item back in the history stack. You do something like change the hash fragment to track navigation through your app (frameworks like Backbone.js can do this for you automatically).
The reason we've taken that approach is there's no hardware back button on iOS so we're wary of setting people up to rely on it in their app, only for the app to be fundamentally broken on that platform: we're aiming for consistency of completeness at the moment.
Update: due to popular demand, we've added support for controlling the back button behaviour on Android: http://docs.trigger.io/en/v1.4/modules/event.html#backpressed-addlistener - note backPressed.preventDefault too.
The event handler is passed a function which, when invoked, closes the app, so you could have code like:
forge.event.backPressed.addListener(function (close) {
if (atHomeScreen) {
close();
}
}

Start Launcher Selection Programmatically / Android

I'm developing an industrial app that's running effectively in a KIOSK mode - that is users must not be able to exit it.
To achieve this I've simply made the app a launcher/home screen replacement. This works very effectively and so far seems to be working as a method of preventing people getting out.
The only problem I have is that if I'm not careful we're going to end up with bricked devices where we can't get back to a normal launcher application.
What I'm looking for is a method of programmatically presenting the Android Launcher Selection Dialog.
Android seems to do this on it's own when you first launch a launcher, but I can't figure out a way of doing it programmatically.
What I'm looking for is a method of pro grammatically presenting the Android Launcher Selection Dialog.
Intent.createChooser() returns an Intent that will launch a chooser (which I think is what you mean by "Android Launcher Selection Dialog") for a given Intent. So, create an Intent for ACTION_MAIN and CATEGORY_HOME, wrap that in Intent.createChooser(), and call startActivity() on the resulting Intent.
If I understand this question right you don't want to get the user out of your app if he don't want it?
I would do it like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return true;
}
Now button clicks will be ignored for the activity. Now you only have to make a menu point for the exit.

Categories

Resources