Android: Determine Active Input Method from Code

Determine enabled input method

I found my answer on another Stack Overflow question :

Android: Determine active input method from code

The following code (updated) did the trick :

public boolean isInputMethodEnabled() {
String id = Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);

ComponentName defaultInputMethod = ComponentName.unflattenFromString(id);

ComponentName myInputMethod = new ComponentName(mContext, MyInputMethodService.class);

return myInputMethod.equals(defaultInputMethod);
}

Open settings screen to activate input method

You can open the Android settings. Use below code

import android.provider.Settings; 
ctx.startActivity(new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS));

How to determine the current IME in Android?

You can get a default IME, use:

Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);

Best way to detect editText inputType in Android?

Try something like this:

if(txt.getInputType()==InputType.TYPE_CLASS_PHONE){
//do something
}

Also check this question: Android: Determine active input method from code it describes using the InputMethodManager

**

Another Approach:

**

when developing your custom keyboard: as described here http://developer.android.com/guide/topics/text/creating-input-method.html#IMEUI

you extends InputMethodService which provides the method onStartInputView(EditorInfo info, boolean restarting)

onStartInputView has the EditorInfo argument which has the inputType field that you can know the current edit text input type from

How to get current InputMethodService

If by saying "get" you mean get it's id, and you want to know the id of the currently selected (chosen as default) here is the way:

    String currentKeyboard =  Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);

How can I tell if the input method picker is open or closed?

There is no such mechanism for check InputMethodPicker is open or not.

But you can check it via another way like as using hasWindowFocus() method for check has focus of your root layout.

Below is sample code:

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<Button
android:id="@+id/btnPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Picker" />

<Button
android:id="@+id/btnCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />

</LinearLayout>

DemoappActivity.class

public class DemoappActivity extends Activity {
/** Called when the activity is first created. */

Button btn1 , btn2;
InputMethodManager imeManager;

LinearLayout mLayoutRoot;
TimerTask timertask;
Timer timer;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLayoutRoot = (LinearLayout)findViewById(R.id.mainlayout);
imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
btn1 = (Button)findViewById(R.id.btnPicker);
btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
showInputMethodPicker();
}
});
btn2 = (Button)findViewById(R.id.btnCheck);
btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

timer.cancel();
}
});
checkMyWindowHasFocus();
}
@Override
protected void onDestroy() {

timer.cancel();
super.onDestroy();
}

public void checkMyWindowHasFocus()
{
timertask = new TimerTask() {

@Override
public void run() {
System.out.println("has window focus..."+mLayoutRoot.hasWindowFocus());
runOnUiThread(new Runnable() {

@Override
public void run() {
Toast.makeText(getApplicationContext(), "Has focus "+mLayoutRoot.hasWindowFocus(), Toast.LENGTH_SHORT).show();
}
});
}
};
timer = new Timer();
timer.schedule(timertask, 500, 5000);

}

private void showInputMethodPicker() {

if (imeManager != null) {
imeManager.showInputMethodPicker();

} else {
Toast.makeText(this, "Error",Toast.LENGTH_LONG).show();
}
}
}

how to get user input method language

Please check this: I think all you need is inside, note it's fully working code.

Android: Detect softkeyboard RTL Vs LTR (language direction)

Here a short code to detect current language of the softkeyboard (on screen keyboard - Input method language): localeDisplayName is the language of the keyboard...

InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype inputMethodSubtype = inputMethodManager.getCurrentInputMethodSubtype();
Locale mLocale = new Locale(inputMethodSubtype.getLocale());
String localeDisplayName = mLocale.getDisplayName(); //e.g. "English"

How to get ID of user set default keyboard in Android

You can get currently active keyboard's id like in the following example:

String id = Settings.Secure.getString(
getContentResolver(),
Settings.Secure.DEFAULT_INPUT_METHOD)

If your soft keyboard is currently being used for input, the above code will return your keyboard's id.

But, you can do something like this:
When your input picker button is clicked, you can show the list of Input Methods, and let the user choose whatever Input Method he likes:

InputMethodManager inputManager = (InputMethodManager) this.getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showInputMethodPicker();

The above code will open a system dialog where Input Method can be picked.
Also, you can get the list of Input Methods like in the code below:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
List<InputMethodInfo> mInputMethodProperties = imm.getEnabledInputMethodList();

final int n = mInputMethodProperties.size();

for (int i = 0; i < n; i++) {
InputMethodInfo imi = mInputMethodProperties.get(i);
Log.d("TAG", "Input Method ID: "+ getApplicationContext().getPackageName(); }

How to detect keyboard language in textview?

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

InputMethodSubtype ims = imm.getCurrentInputMethodSubtype();

String locale = ims.getLocale();

//get Locale object first
Locale locale = new Locale(localeString);
//then get the display language from locale object.
String currentLanguage = locale.getDisplayLanguage();
//Also note that this method is deprecated in API 24. For API 24 or further use getLanguageTag() method.

You can try this code to get Current Keyboard Language regional code.



Related Topics



Leave a reply



Submit