I just received fabric mail that my app crashed and I test it on my mobile but it's working fine but I don't why it crashes on OS 9.
context = this;
RelativeLayout parentView = findViewById(R.id.relative_cusweb_parent);
setupParent(parentView);
Above is my onCreate method & relative_cusweb_parent is main relative layout of CustomWeb Class.
private void setupParent(View view) {
if (!(view instanceof EditText)) {
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideKeyboard();
return false;
}
});
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupParent(innerView);
}
}
}
private void hideKeyboard() {
input.clearFocus();
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
imm.hideSoftInputFromWindow(Objects.requireNonNull(getCurrentFocus()).getWindowToken(), 0); // here a is crashing
}
}
App is crashing on this line
imm.hideSoftInputFromWindow(Objects.requireNonNull(getCurrentFocus()).getWindowToken(), 0);
and below is the log
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at codeline.onlinebills.activities.CustomWeb.hideKeyboard(CustomWeb.java:222)
at codeline.onlinebills.activities.CustomWeb.access$200(CustomWeb.java:38)
at codeline.onlinebills.activities.CustomWeb$4.onTouch(CustomWeb.java:231)
at android.view.View.dispatchTouchEvent(View.java:12611)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3035)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2714)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3041)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2671)
It's crashing because it doesn't find any view in focus and getCurrentFocus() returns null.
added in API level 1
public abstract View getCurrentFocus ()
Return the view in this Window that currently has focus, or null if there are none. Note that this does not look in any containing Window.
(source)
Replace this:
imm.hideSoftInputFromWindow(Objects.requireNonNull(getCurrentFocus()).getWindowToken(), 0);
with this:
imm.hideSoftInputFromWindow(getWindow().getDecorView().getRootView().getWindowToken(), 0);
You can hide keyboard with this method :
private void hideKeyBoard()
{
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if( imm != null )
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Try to replace your hideKeyboard method with the code below, I am using this is my app to hide the Keyboard and it works perfectly:
private void hideKeyboard() {
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
Related
I have an edittext with recyclerview where a list of places will be displayed on searching.On clicking any item from list, the keyboard should hide. Please help
call this method when clicked in searched list click
void hideKeybord() {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Use below code to hide the keyboard inside onClick() of Item.
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Solution: Simply set an onTouchListener() to your RecyclerView and hide your keyboard:
yourRecycleView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager input = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
input.hideSoftInputFromWindow(v.getWindowToken(), 0);
return false;
}
});
Hope this will come in handy.
Try this - call this method inside the onClick of recycler ViewHolder
public void hideKeyboard(){
edittext.clearFocus()
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)
if (imm != null) {
imm.hideSoftInputFromWindow(edittext.windowToken, 0)
}
}
I'm writing an activity, which has lots of EditText.
Their inputType are numericDecimal. Like this: Before I click
Now, I want to hide the soft keyboard when clicking somewhere other than the EditTexts, so I put:
public void hideKeyboard(View mView) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService
(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
After I click somewhere else, the numericDecimal soft keyboard do disappear. HOWEVER, there is still a common soft keyboard without autocompletion left on the screen, and I have absolutely no clue where does this come from. Showing here: After I click
So how to hide them all? Common ways on Internet don't work, I tried them all.
Thanks in advance!
Try this code :
public static void setupUI(final View view, final Activity activity) {
if (view instanceof ViewGroup) {
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Utils.hideSoftKeyboard(activity, view);
return false;
}
});
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView, activity);
}
}
}
public static void hideSoftKeyboard(Activity activity, View searchET) {
try {
InputMethodManager mgr = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(searchET.getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
Call the setupUI function and pass the parent Layout in your activity. It will make sure that whenever you click out of your editText it will close the keyboard.
Hope this helps.
So I find this really cool piece of code for dismissing the keyboard when I click non-EditText views. And it works very well, except for Fragments and DialogFragments started using getChildFragmentManager(). Will someone please shed some light on why the exception and how I might fix it?
public static void setupUI(View view, final Activity activity) {
// Set up touch listener for non-text box views to hide keyboard.
if (!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
LayoutUtils.hideSoftKeyboard(activity);
return false;
}
});
}
// If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView, activity);
}
}
}
private static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
I keep the code in a utility class and use it throughout my app. Essentially, for the problem case, I use it on FragmentB is started by FragmentA using getChildFragmentManager(), then the code does not affect the views of FragmentB.
try this for hide keyboard,
public void hideSoftKeyboard() {
try {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
e.printStackTrace();
}
}
try the below code:
#Override
public boolean onTouchEvent(MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
return true;
}
If you are using fragment use FragmentActivity not Activity. I suggest use try catch.
like
try{
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
}catch(Exception){
InputMethodManager inputMethodManager = (InputMethodManager) fragmentactivity.getSystemService(Activity.INPUT_METHOD_SERVICE);
}
I found the solution. Maybe someone else will find it useful
public static void setupUI(View view, final Activity activity) {
// Set up touch listener for non-text box views to hide keyboard.
if (!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
LayoutUtils.hideSoftKeyboard(activity,v);
return false;
}
});
}
// If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView, activity);
}
}
}
private static void hideSoftKeyboard(Activity activity, View v) {
InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
See how I replace activity.getCurrentFocus() with v. And that's it. It works in all situations now.
I know that the code for dismiss tyhe keypad in android is
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Can anyone suggest me a method to hide the keypad if we touch the area outside the text area other than the keypad in the screen.
Code to dismiss Softkeyboard is below:
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
You can put it in Utility Class or if you are defining it within an activity, avoid the activity parameter, or call hideSoftKeyboard(this).
You can write a method that iterates through every View in your activity, and check if it is an instanceof EditText if it is not register a setOnTouchListener to that component and everything will fall in place. In case you are wondering how to do that, it is in fact quite simple. Here is what you do, you write a recursive method like the following.
public void setupUI(View view) {
//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard();
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
}
Call this method after SetcontentView() with paramet as id of your view like:
RelativeLayoutPanel android:id="#+id/parent"> ... </RelativeLayout>
Then call setupUI(findViewById(R.id.parent))
Best way you can use is DONE button besides EditText make your onClickListener to do like,
done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
}
});
This may be old but I got this working by implenting a custom class
public class DismissKeyboardListener implements OnClickListener {
Activity mAct;
public DismissKeyboardListener(Activity act) {
this.mAct = act;
}
#Override
public void onClick(View v) {
if ( v instanceof ViewGroup ) {
hideSoftKeyboard( this.mAct );
}
}
}
public void hideSoftKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
the best practice here is to create a Helper class and every container Relative / Linear Layouts should implement this.
**** Take note only the main Container should implement this class (For optimization) ****
and implement it like this :
Parent.setOnClickListener( new DismissKeyboardListener(this) );
the keyword this is for Activity. so if you are on fragment you use it like getActivity();
---thumbs up if it help you... --- cheers Ralph ---
I have a EditText and button aligned to parent's bottom.
When I enter text in it and press the button to save data, the virtual keyboard does not disappear.
Can any one guide me how to hide the keyboard?
You might also want to define the imeOptions within the EditText. This way, the keyboard will go away once you press on Done:
<EditText
android:id="#+id/editText1"
android:inputType="text"
android:imeOptions="actionDone"/>
This should work.
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
Just make sure that this.getCurrentFocus() does not return null, which it would if nothing has focus.
mEtNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do something, e.g. set your TextView here via .setText()
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
});
and in xml
android:imeOptions="actionDone"
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I did not see anyone using this method:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean focused) {
InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (focused)
keyboard.showSoftInput(editText, 0);
else
keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
});
And then just request focus to the editText:
editText.requestFocus();
Solution included in the EditText action listenner:
public void onCreate(Bundle savedInstanceState) {
...
...
edittext = (EditText) findViewById(R.id.EditText01);
edittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
...
...
}
I found this because my EditText wasn't automatically getting dismissed on enter.
This was my original code.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))) {
// Do stuff when user presses enter
return true;
}
return false;
}
});
I solved it by removing the line
return true;
after doing stuff when user presses enter.
Hope this helps someone.
Just write down these two lines of code where enter option will work.
InputMethodManager inputManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
Been struggling with this for the past days and found a solution that works really well. The soft keyboard is hidden when a touch is done anywhere outside the EditText.
Code posted here: hide default keyboard on click in android
you can create a singleton class for call easily like this:
public class KeyboardUtils {
private static KeyboardUtils instance;
private InputMethodManager inputMethodManager;
private KeyboardUtils() {
}
public static KeyboardUtils getInstance() {
if (instance == null)
instance = new KeyboardUtils();
return instance;
}
private InputMethodManager getInputMethodManager() {
if (inputMethodManager == null)
inputMethodManager = (InputMethodManager) Application.getInstance().getSystemService(Activity.INPUT_METHOD_SERVICE);
return inputMethodManager;
}
#SuppressWarnings("ConstantConditions")
public void hide(final Activity activity) {
new Handler().post(new Runnable() {
#Override
public void run() {
try {
getInputMethodManager().hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
}
}
so, after can call in the activity how the next form:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
KeyboardUtils.getInstance().hide(this);
}
}
You can see marked answer on top. But i used getDialog().getCurrentFocus() and working well. I post this answer cause i cant type "this" in my oncreatedialog.
So this is my answer. If you tried marked answer and not worked , you can simply try this:
InputMethodManager inputManager = (InputMethodManager) getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
I use this method to remove keyboard from edit text:
public static void hideKeyboard(Activity activity, IBinder binder) {
if (activity != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (binder != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(binder, 0);//HIDE_NOT_ALWAYS
inputManager.showSoftInputFromInputMethod(binder, 0);
}
}
}
And this method to remove keyboard from activity (not work in some cases - for example, when edittext, to wich is binded keyboard, lost focus, it won't work. But for other situations, it works great, and you do not have to care about element that holds the keyboard).
public static void hideKeyboard(Activity activity) {
if (activity != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (activity.getCurrentFocus() != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
inputManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
}
}
}
int klavStat = 1; // for keyboard soft/hide button
int inType; // to remeber your default keybort Type
editor - is EditText field
/// metod for onclick button ///
public void keyboard(View view) {
if (klavStat == 1) {
klavStat = 0;
inType = editor.getInputType();
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
editor.setInputType(InputType.TYPE_NULL);
editor.setTextIsSelectable(true);
} else {
klavStat = 1;
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
editor.setInputType(inType);
}
}
If you have another EditText Field, you need to watch for focus change.
In my case, in order to hide the keyboard when pressing the "send button", I used the accepted answer but changed context to getApplication and added getWindow().
InputMethodManager inputManager = (InputMethodManager) getApplication().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
editText.setInputType(InputType.TYPE_NULL);