In my xml, I am having TextInputLayout edittext inside NestedRecyclerView, when focus is on first edittext and I am going to focus on second edittext then keyboard is hiding and showing, on scrolling also keyboard is hiding but focus is still there.
Any help will be appreciated.
implement OnTouchListener in your project code
MyRecycleView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return false;
}
});
This might help you Implementation of RecyclerView that will dismiss keyboard
Related
I want to mimic android keyboard behavior when handling touch event. The android keyboard always hide every time I click somewhere else other the edit text and the keyboard. So far, I could mimic that's behavior using dispatchTouchEvent
Since it will detect for any dispatch touch even, whenever I click my keyboard button then it will close the keyboard it self. I wanna check if I click somewhere else other than my keyboard then hide the keyboard, else keep the keyboard on. I think I just missed piece of code and I can't find it what i missed here. It just need one single step to accomplish this. Please help.
Here my keyboard looks like
and here what i did so far
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (getCurrentFocus() != null) {
View view = getCurrentFocus();
//im not sure this condition would help
if (view == null) {
keyboard.setVisibility(View.GONE);
}
}
return super.dispatchTouchEvent(ev);
}
I can get the view object but i cant determine which view that i need to keep my keyboard on.
The other code (variable declaration)
PhoneKeyboard pk;
InputConnection ic;
LinearLayout keyboard;
pk = findViewById(R.id.phone_keyboard);
ic = phone_no.onCreateInputConnection(new EditorInfo());
pk.setInputConnection(ic);
Handling back pressed (mimic the android keyboard behavior)
#Override
public void onBackPressed(){
if(keyboard.getVisibility() == View.GONE)
super.onBackPressed();
else
keyboard.setVisibility(View.GONE);
}
How I handle request on the editText to make my keyboard visible
phone_no.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(
android.content.Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
if(view.equals(phone_no) && keyboard.getVisibility() == View.GONE) {
phone_no.requestFocus();
keyboard.setVisibility(View.VISIBLE);
}
return false;
}
});
How I handle submit / enter event at my custom keyboard (for who need the solution for custom keyboard, maybe this post can help other:) )
phone_no.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(
android.content.Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
if(view.equals(phone_no) && keyboard.getVisibility() == View.GONE) {
phone_no.requestFocus();
keyboard.setVisibility(View.VISIBLE);
}
return false;
}
});
Here is my snippet code for the keyboard layout view
<include
layout="#layout/layout_keyboard_phone_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Ps. I also have base activity and it extends to all my activity (if your solution required the base activity)
Update 1
I have try this method
phone_no.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
keyboard.setVisibility(View.GONE);
}
}
});
Is doing the same thing with the onDispatch event, whenever I click number on my custom keyboard, it also hide the keyboard. I need to check if the view others than the edit text and my custom keyboard then hide the keyboard else keep the keyboard on. Thank you
Inside the .xml layout I have 3 components in following order:
A Textview
A fragment (extends ListFragment)
A LinearLayout storing 2 buttons
Whenever user uses the search bar to query, the keyboard appears. Content of the fragment will be updated depending on the query.
What I'm trying to do is whenever user taps the screen (outside the keyboard), the keyboard needs to be hidden. So far, I could only do so if I tap anywhere but the fragment region.
I tried setOnTouchListener inside onActivityCreated() inside the fragment but it doesnt seem to work.
I got tapping outside the fragment = hidding keyboard to work by using the following inside onCreate():
layout.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View view, MotionEvent ev)
{
hideKeyboard(view);
return false;
}
});
hideKeyboard(View view)
public void hideKeyboard(View view)
{
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
But not inside the fragment...
I made a silly mistake....
The following code would solve the issue:
Inside onActivityCreated()
getActivity().findViewById(R.id.mid).setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View dview, MotionEvent ev)
{
f_hideKeyboard(dview);
return false;
}
});
Edited: Nevermind.... The above works only if the query shows zero result. If it shows at least 1 result, touching the fragment region still will not hide keyboard
I know that it's a bit late. From what I have understand from your question, here is the solution below. Use the following code inside onCreateView
//hides keyboard when tap on the screen
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_MOVE){
//do something
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}
return true;
}
});
I have 2 EditText fields which I have the user fill out, followed by a DatePicker. I am trying to hide the keyboard as soon as the user touches the DatePicker (as the keyboard is covering the save/cancel buttons.)
dpDate.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard();
return false;
}
});
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
This is currently what I have, however, it does not appear to be working. When I touch the DatePicker, click it, anything.. the keyboard will not go away.
Any help is appreciated!
EDIT:
The layout is as follows
Amount: (EditText)
Memo: (EditText)
Date: (DatePicker)
--------------------
Cancel OK
When user touches that datepicker to change the date, I immediately want the keyboard to go away.
So you may need to just do Context.INPUT_METHOD_SERVICE instead of just INPUT_METHOD_SERVICE.
I would like to know the best way to hide keyboard after entering the text to EditText.
1) setonfocuschangelistener : Does this listener is fired only, when the done button is pressed or when the focus changes from one EditText to other? When I used this method, I couldn't hide the keyboard.
2) setOnTouchListener : When I used this, I could hide the keyboard, but i doubt there might be an issue with this. In this case, I add the touch listener to the root LinearLayout. Following code I had used:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
txtUserName = (EditText)findViewById(R.id.txtUserName);
btnLogin = (Button)findViewById(R.id.btnLogin);
layoutView = (LinearLayout)findViewById(R.id.li);
layoutView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(txtUserName
.getWindowToken(), 0);
return true;
}
});
}
Inside the main LinearLayout, I am using other two LinearLayouts. The issue that i faced with the above code is that at some points when I pressed, the keyboard doesn't hides.
My doubt is that I am adding touch listener only with root layout, not giving touch listener with other inner layouts or other controls(TextView). When I touch over other controls or some points around the TextView(ie, inner layouts), keyboard doesn't hides.
That means do i need to add touchListener to all layouts or controls inside the root layout?
How this situation can be handled in a better way?
You can use this code
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditView.getWindowToken(), 0);
My answer on this question:
Add this method:
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
If you want to hide keyboard when you touch screen, you can do by this way:
#Override
public boolean onTouchEvent(MotionEvent event) {
hideSoftKeyboard(LoginActivity.this);
return false;
}
Hope this will help you.
Try this :
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
it can be used to suppress the keyboard until the user actually touched the edittext view.
OR
inputManager.hideSoftInputFromWindow(editView.getWindowToken(), 0);
A simple check for null or a try-catch avoids the NullPointerException if no view is focused.
public void hideKeyboard(Activity activity) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (activity.getCurrentFocus() != null) {
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
Best way to hide keyboard, just dispatchDoneKey in your activity. Keyboard will be hidden if it is in visible state.
public void dispatchDoneKey() {
dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
}
What I want to do is this: I have an EditText and when the user click on it, the user can move the caret position, same as the EditText, but without showing the keyboard.
I've tried with setInputType(0); and it hides completly the keyboard but the cursor doesn't appears.
Is there any way to do this?
Thank's
The following tricks works for me. Caret and soft keyboard both active onTouch event of editText. So Call touch event and then hide the keyboard manually.
myEditText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
final boolean ret = dialerNumber.onTouchEvent(event);
final InputMethodManager imm = ((InputMethodManager) myContext
.getSystemService(Context.INPUT_METHOD_SERVICE));
try{
imm.hideSoftInputFromWindow(myEditText.getApplicationWindowToken(), 0);
}catch(Exception e){
e.printStackTrace();
}
return ret;
});
I haven't tried suppressing the keyboard altogether, but I have hidden the soft keyboard manually before using the InputMethodManager.hideSoftInputFromWindow method.
Does this work?
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)