Keypad hide/show in webview - android

I had tried this out. And it works fine with Samsung tablet.
On Page 1_4.html i have to hide keypad and on 2.html I Have to show
keypad.
Both on textbox click inside webview
NOTE: Android Activity is Same.
I'm calling this code on webView.setOnTouchListener
if (value.equals("1") || value.equals("4")) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
} else if(value.equals("2")) {
getWindow().clearFlags( WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}
But its not working in any mobile phones. It gives me Warning
W/InputMethodManager(25060): startInputInner : InputBindResult == null
I have google it. But didn't find anything useful.
What should I do now? Any help will be Appreciated.

To open keypad try this
webview.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(webview, 0);
}
},100);
To close keypad try this
InputMethodManager inputMethodManager = (InputMethodManager)MainActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), 0);

try this code : This is working on all devices.
try
{
if(isopen)
{
// to hide keyboard
InputMethodManager inputMethodManager = (InputMethodManager)context.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}else{
// to open keyboard
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(linearLayout.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
catch (NullPointerException e)
{
}
catch (Exception e)
{
}

Related

Soft keyboard on physical device doesn't show when a textbox is clicked

I'm currently using Oppo F7 to test my app that I made using Android Studio. However, when I tested it and I clicked on a textbox (or EditText), the keyboard doesn't show/pop-up. Helpppp..
Note:
- My Oppo F7 is running on Android 8.1.0 (API 27)
- It works on the emulator on my laptop but it's far to laggy for me to test
use like this
public static void showSoftKeyboard(final Context context, final EditText editText) {
try {
editText.requestFocus();
editText.postDelayed(
new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(editText, 0);
}
}
, 200);
} catch (NullPointerException npe) {
npe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
in your activity call on text click
showSoftKeyboard(this, yourEditTextToFocus);

Is it better to set an enabled state of a button only if it actually changes?

In my first ever Android project I see a lot of code like this:
final boolean lNewButtonState = SOME_CONDITION;
if (lNewButtonState != mButtonState) {
mButtonState = lNewButtonState;
mButton.setEnabled(mButtonState);
}
Does it make any difference to set the state of an ImageButton (the type of mButton) only if it does change as opposed to setting it always? The button is visible on the screen when the code is executed.
I'd prefer a straightforward (and more readable):
mButton.setEnabled(SOME_CONDITION);
The question is, would it impose any drawbacks? Does the answer depend on the Android version (ours is Jelly Bean)?
Doesn't matter that much because it will be ignored if the button is already enabled. The only cost of it is the isEnabled() method call which is pretty insignificant
From Android source code:
#Override
public void setEnabled(boolean enabled) {
if (enabled == isEnabled()) {
return;
}
if (!enabled) {
// Hide the soft input if the currently active TextView is disabled
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null && imm.isActive(this)) {
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
}
super.setEnabled(enabled);
if (enabled) {
// Make sure IME is updated with current editor info.
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null) imm.restartInput(this);
}
// Will change text color
if (mEditor != null) {
mEditor.invalidateTextDisplayList();
mEditor.prepareCursorControllers();
// start or stop the cursor blinking as appropriate
mEditor.makeBlink();
}
}
#ViewDebug.ExportedProperty
public boolean isEnabled() {
return (mViewFlags & ENABLED_MASK) == ENABLED;
}

Xamarin, Overriding the Focus Change in a custom renderer stops focus events from occurring

I have a custom entry field that prevents the soft keyboard from appearing when the entry field get focus. However this prevents the normal focus/unfocus events from occurring. Here is the code for the Android renderer
Control.FocusChange += (sender, eh) =>
{
new Handler().Post(delegate
{
if (eh.HasFocus)
if (Control != null)
{
var imm =
(InputMethodManager)Control.Context.GetSystemService(Android.Content.Context.InputMethodService);
imm.HideSoftInputFromWindow(Control.WindowToken, 0);
}
});
};
Is this a bug in Xamarin or is there a way to have the focus and unfocus events fire.
I had the same problem when trying to hide an already opened soft keyboard when focusing a DatePicker or TimePicker. (In some Android phones the soft keyboard stayed on top of the date/time picker dialogs).
When using Control.FocusChange event handler, the default behaviour of opening the picker dialogs was not being triggered.
Solution:
After hiding the keyboard, call the Focus() method on the element to trigger the normal focus events and default behaviour.
Here is my Android DatePicker renderer code:
protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
{
base.OnElementChanged(e);
if (Control == null)
{
return;
}
Control.ShowSoftInputOnFocus = false;
Control.FocusChange += (sender, args) => HideSoftKeyboard(e.NewElement, args);
}
private void HideSoftKeyboard(DatePicker e, FocusChangeEventArgs args)
{
if (args.HasFocus)
{
Device.BeginInvokeOnMainThread(() =>
{
var inputMethodManager = Context.GetSystemService(Context.InputMethodService) as InputMethodManager;
inputMethodManager?.HideSoftInputFromWindow(Control.WindowToken, HideSoftInputFlags.None);
e?.Focus();
});
}
}
Based on your code, I think you're right to try to dismiss the keyboard in UI thread, I'm not sure that happens with new Handler().Post() here, but you can use BeginInvokeOnMainThread to force the code run on UI thread:
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.FocusChange += (sender, eh) =>
{
if (eh.HasFocus)
{
Device.BeginInvokeOnMainThread(() =>
{
var imm = (InputMethodManager)Control.Context.GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(Control.WindowToken, HideSoftInputFlags.None);
});
}
};
}
}
This code works by my side and the FocusChange event can be fired normally.

Open softkeyboard when webview input text gets focus

I have a webview based app in which i want the appropriate keypad to open whenever i give focus to some input. With the code i have provided below it opens up but it always opens qwerty keypad even for numeric type inputs.
private void ShowKeyboard(final boolean show) {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (show) {
InputMethodManager mgr = (InputMethodManager) General.MainShellReference.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(General.appView, InputMethodManager.SHOW_IMPLICIT);
((InputMethodManager) General.MainShellReference.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(General.appView, 0);
} else {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
});
} catch (Exception ex) {
;
}
}
I think you have already tried to use <input type="number" /> and <input type="tel" />. Anyway try to do this.
Did you see this: Is there a way to have a masked numeric input field?
http://blog.pamelafox.org/2012/05/triggering-numeric-keyboards-with-html5.html

how to get Activity's windowToken without view?

Now, I try to hide the softkeyboard when user touch outside the keyboard:
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(editView.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
I want put the logic in my base activity class, so if it is possible to getWindowToken without View?
I faced exactly the same problem, while writing OnPageChangeListener within an Activity. You can use one of these solutions. Either:
getWindow().getDecorView().getRootView().getWindowToken()
or:
findViewById(android.R.id.content).getWind‌​owToken()
Surely you can use:
getContentView().getWindowToken()
or you can refer to SO Quest
Simply use getWindow().getDecorView().getWindowToken()
public static final String M_TOKEN = "mToken";
#Nullable
protected IBinder getToken(Activity activity) {
try {
Field mTokenField = Activity.class.getDeclaredField(M_TOKEN);
mTokenField.setAccessible(true);
IBinder mToken = (IBinder) mTokenField.get(activity);
return mToken;
} catch (NoSuchFieldException e) {
// handle
} catch (IllegalAccessException e) {
// handle
}
return null;
}
You could just get the token from the WindowManager.LayoutParams of the window directly
getWindow().getAttributes().token
In kotlin:
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(window.attributes.token, 0)
Or, If you have a view:
imm.hideSoftInputFromWindow(view.windowToken, 0)
You can try this on your manifest file activity tag to hide keyboard.
android:windowSoftInputMode="stateHidden"

Categories

Resources