I'm currently developing a Android application that could receive the user input by the soft keyboard. But there is some problem when my keyboard show up, there is only English keyboard when I use the code below:
((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
And the Google IME display without my default language (That means I could not change to other language because of no tab to select), but I search for whole article, there seems to be no way to change the IME language by code.
I use the LIBGDX, neither its ui element (TextField), nor calling it direclty from Android could show the Google IME with my default language (Chinese). But if I use system IME, it could work as expected.
For clear other problem, I could use Google IME in other app without any problem, and I also post the code that I used to call the InputMethodManager forecly.
package my.package;
import android.app.Service;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Handler;
import android.os.LocaleList;
import android.os.Looper;
import android.text.InputType;
import android.util.DisplayMetrics;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import android.widget.EditText;
import java.util.Locale;
import my.package.Debugger;
public final class AndroidKeyboardAdapter implements KeyboardAdapter{
InputMethodManager imm;
Context context;
EditText text;
public AndroidKeyboardAdapter(Context context) {
this.context = context;
imm = (InputMethodManager)context.getSystemService(Service.INPUT_METHOD_SERVICE);
}
public void showKeyboard() {
text = new EditText(context);
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = context.getResources().getConfiguration().getLocales().get(0);
for(int i = 0; i < context.getResources().getConfiguration().getLocales().size();i++)
Debugger.LogInConsole(context.getResources().getConfiguration().getLocales().get(i).getCountry()); // show TW
} else{
//noinspection deprecation
locale = context.getResources().getConfiguration().locale;
Debugger.LogInConsole(locale.getCountry()); // not doing anything
}
Debugger.LogInConsole(Locale.getDefault().toString()); //show zh_tw
((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
public void hideKeyboard() {
imm.hideSoftInputFromWindow(text.getWindowToken(), 0);
}
public void onResume() {
text.requestFocus();
text.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
imm.showSoftInput(text, 0);
}
},200);
}
@Override
public String getText() {
return text.getText().toString();
}
}
Also provide the source code that LIBGDX process on the IMM:
@Override
public void setOnscreenKeyboardVisible (final boolean visible) {
handle.post(new Runnable() {
public void run () {
InputMethodManager manager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (visible) {
View view = ((AndroidGraphics)app.getGraphics()).getView();
view.setFocusable(true);
view.setFocusableInTouchMode(true);
manager.showSoftInput(((AndroidGraphics)app.getGraphics()).getView(), 0);
} else {
manager.hideSoftInputFromWindow(((AndroidGraphics)app.getGraphics()).getView().getWindowToken(), 0);
}
}
});
}
Edit: Found the relative issue, and according to the report, LIBGDX do the specific configuration for English developer, If you are the other language developer, you should not use the TextField UI and call the IMM directly, just use the code below with the Label UI, and everything would be OK...
label.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Gdx.input.getTextInput(new Input.TextInputListener() {
@Override
public void input(String text) {
label.setText(text);
Debugger.LogInConsole(text);
}
@Override
public void canceled() {
}
},"","","");
}
});
Waste a lot of time for reading source code, and hope this could help others.