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);
}
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 create an parental control app, where i want to put the Simple password authentication before disable to administrator mode by used . I am using DeviceAdminReceiver . An Idea or sample code which help . Thankyou
Unfortunately there is no direct approach to achieve it. However there is a workaround which can be done by overriding onDisableRequested() method of DeviceAdminReceiver
public class AdminReceiver extends DeviceAdminReceiver {
#Override
public CharSequence onDisableRequested(Context context, Intent intent) {
DevicePolicyManager mDPM =(DevicePolicyManager)getApplicationContext().getSystemService("device_policy");
mDPM.lockNow();
// You can also display overlay screen
return "Are you sure you want to disable the Device admin?";//OR whatever message you would like to display
}
}
As per documentation
Called when the user has asked to disable the administrator, as a result of receiving ACTION_DEVICE_ADMIN_DISABLE_REQUESTED, giving you a chance to present a warning message to them. The message is returned as the result; if null is returned (the default implementation), no message will be displayed.
Note:
If you are trying to display overlay screen, do note that disable popup will and deactivate screen will have high visibility precedence. Any attempts to do so won't help much. One work around is to lock screen first and then display overlay screen.
I am trying to make a spinner I have selectively translate strings on a page. I have my app set up to translate the bulk of the page based on locale languages already. In the image below, The locale language will effect the top string, but I have a spinner on my home page that has all the available android languages in it, and I want users to be able to select one of the spinner items, and as a result the bottom string in the picture below will be translated to that language. I can't find a way to set it up, all the advice I've seen so far is just how to configure the application to set up locale language's. Any direction would be greatly appreciated!
I think you can use Android-LocalizationActivity
Here a portion of its readme:
It's basic for android application to be supported multiple languages. Yeah! It's very easy because android has String Resource. Developer just had to prepare the text for different languages then android system will use itself. But frequently problem is "On-time Language Changing". Because the String Resource was designed to be depending on current device language. but if we want to change the language by click some button. It will be difficult to handle it. This problem will solved because I have created a new library to handle application language. It called "Localization Activity" library.
It's so simple to implement it. Just extend your Activity with LocalizationActivity. Here a sample code with Button:
import android.os.Bundle;
import android.view.View;
import com.akexorcist.localizationactivity.LocalizationActivity;
public class MainActivity extends LocalizationActivity implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
findViewById(R.id.btn_th).setOnClickListener(this);
findViewById(R.id.btn_en).setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btn_en) {
setLanguage("en");
} else if (id == R.id.btn_th) {
setLanguage("th");
}
}
}
When user click btn_th (Thailand), all language will be translated to Thai language. So you only need to adjust it with spinner.
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 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!