Hide keyboard when down arrow is pressed - android

The picture shows a part of my app, an AutoCompleteTextView with an attached adapter.
When the user enters something into that view, autocomplete suggestions are shown.
The problem I have is: when the suggestions are shown and the device's down arrow is pressed, only the suggestions from the AutoCompleteTextView are closed, the keyboard stays open and needs a second tap on the down arrow to disappear.
I do want the suggestions and the keyboard to disappear on the first tap on the down arrow.
I tried overriding onBackPressed but it is not called when the down arrow is tapped, presumably because it's not considered 'back'.
How could I do this?
EDIT: I know how to programmatically hide the keyboard, I guess my problem is to detect the 'down arrow' tap.

Try to override onKeyPreIme() method in your AutoCompleteTextView as follows:
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
super.onKeyPreIme(keyCode, event);
hideKeyboard()
return true;
}
return super.onKeyPreIme(keyCode, event);
}

You can try something like this:
private boolean mIsKeyboardShown;
private EditText mSearchTextView;
#Override
protected void onCreate(Bundle bundle)
...
mSearchTextView = (EditText) findViewById(R.id.search);
View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
// if more than 100 pixels, its probably a keyboard...
mIsKeyboardShown = (heightDiff > 100);
}
});
}
public void onBackPressed() {
if(mIsKeyboardShown) {
// close the keyboard
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(mSearchTextView.getWindowToken(), 0);
} else {
super.onBackPressed();
}
}
I haven't tried the code but I think this is the right approach.

InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
you need to import android.view.inputmethod.InputMethodManager;

Related

Custom keyboard set visibility gone when touch event dispatch from editText

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

Can not open soft keyboard in EditText control

I want to create 1 edit text with the below condition:
- User can not focus on this control in normal.
- When user click on this control, soft-keyborad is displayed and user can input into this control
- When user press enter on this soft-keyborad or back on device, it is closed and back to normal view with control is not focus.
I tried the below code but not work :(
When starting, control is not focus: ok
When click on control, at the first click, control is focus but not display soft-keyborad
In the second click, display soft-keyborad
When press enter button in soft-keyborad: back to screen with control is not focus: OK
When press back button device, back to screen with control is still focus : not ok
public void onCreate(Bundle savedInstanceState)
{
final EditText txtSearch = (EditText)this.findViewById(R.id.p60004_txt_search_str);
txtSearch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txtSearch.setFocusable(true);//(false);
txtSearch.setFocusableInTouchMode(true);
txtSearch.requestFocus();
}
});
txtSearch.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
String strSearch = txtSearch.getText().toString();
if (strSearch != null && strSearch != ""){
searchFriend(UserAPIConstants.FRIEND_SEARCH_TYPE_SC, strSearch);
}
hideSoftKeyboard(v);
txtSearch.setFocusable(false);
txtSearch.setFocusableInTouchMode(false);
}
return false;
}
});
public void hideSoftKeyboard (View view) {
InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
In your xml which has edittext put these values for the layout node
android:focusable="true"
android:focusableInTouchMode="true"
and dont put any focusable or focusable in touch mode attributes for your edittext..
Then in your code in onKey method remove thse lines..
txtSearch.setFocusable(false);
txtSearch.setFocusableInTouchMode(false);
and put
txtSearch.clearFocus();
And You should override this method
onBackPressed()
like this..
#Override
public void onBackPressed() {
txtSearch.clearFocus();
//hide the soft keyboard..
}
try commenting
hidekeyboard(v);
and the changes that Alex Lockwood suggested.
use this in onClick()
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
to close the keypad use
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);

Hide keyboard after user searches?

I have an activity where there is an EditText and on enter key search results are shown, so what I simply want to do is to close the keyboard when search results are about to show to prevent the user from having to do it. However if the user wants to refine his search the keyboard should open back up if he taps into the EditText again.
This has been more difficult than I imagined, I've been search and tried a few things most don't even close the keyboard on my HTC, one method where the InputType is set to INPUT_NULL closes the keyboard but it doesn't open afterwards.
Any suggestions on how to do this?
#Override
public boolean onQueryTextSubmit(String query) {
// Your search methods
searchView.clearFocus();
return true;
}
Straight to the point and clean.
The right way to do this:
set imeOptions to "actionSearch"
initialize listeners for input and search button(if provided)
searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
}
});
view.findViewById(R.id.bigSearchBar_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
performSearch();
}
});
Hide keyboard when user clicks search. To ensure that keyboard won't show when user minimizes and restores Activity you have to remove focus from EditText
private void performSearch() {
searchEditText.clearFocus();
InputMethodManager in = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
... perform search ...
}
I belive this code snippet will close the keyboard:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
if not try this one:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
let me know if these work

Tap outside edittext to lose focus

I just want when click outside the "edittext" to automatically lose focus and hide keyboard. At the moment, if I click on the "edittext" it focuses but i need to hit the back button to unfocus.
To solve this problem what you have to do is first use setOnFocusChangeListener of that Edittext
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
Log.d("focus", "focus lost");
// Do whatever you want here
} else {
Log.d("focus", "focused");
}
}
});
and then what you need to do is override dispatchTouchEvent in the activity which contains that Edittext see below code
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if ( v instanceof EditText) {
Rect outRect = new Rect();
v.getGlobalVisibleRect(outRect);
if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
Log.d("focus", "touchevent");
v.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return super.dispatchTouchEvent(event);
}
Now what will happen is when a user click outside then, firstly this dispatchTouchEvent will get called which then will clear focus from the editext, now your OnFocusChangeListener will get called that focus has been changed, now here you can do anything which you wanted to do, hope it works :)
#woodshy has the answer for hiding the keyboard, but maybe putting the code in onClick is not as good as putting it in onFocusChanged. As for forcing it to lose focus, you need to set the object you want to transfer the focus to, in its XML file:
android:focusable="true"
android:focusableInTouchMode ="true"
Suppose your EditText is placed on Linear layout or other ViewGroup. Then you should make this container clickable, focusable and focusableInTouchMode. After that set onClickListener to this container with following code in onClick Override method:
#Override
public void onClick(View view) {
InputMethodManager imm = (InputMethodManager) view.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
You can achieve this by doing the following steps:
1.Make the parent view(content view of your activity) clickable and focusable by adding the following attributes
android:clickable="true"
android:focusableInTouchMode="true"
2.Implement a hideKeyboard() method
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
3.Lastly, set the onFocusChangeListener of your edittext.
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
The following code works perfectly for me, and I regularly use this for closing the SoftKeyboard onTouch anywhere outside the EditText.
public void hideSoftKeyboard()
{
//Hides the SoftKeyboard
InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}
public void setupUI(View view)
{
String s = "inside";
//Set up touch listener for non-text box views to hide keyboard.
if (!(view instanceof EditText)) {
view.setOnTouchListener(new View.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);
}
}
}
So you just need to call the setupUI() function once in your onCreate() method and voilla, your hidingSoftKeyboard is setup!
For Losing complete focus of the EditText, simply do editText.clearFocus() in your hideSoftKeyboard() function.
Here universal method for all screens. Put it in your activity
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
clearFocusOnOutsideClick()
return super.dispatchTouchEvent(ev)
}
/*
* Clear focus on outside click
* */
private fun clearFocusOnOutsideClick() {
currentFocus?.apply {
if (this is EditText) {
clearFocus()
}
//Hide keyboard
val imm: InputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}
kotlin clearfocus android outside
So I searched around a little bit, and no other solutions was exactly what I was looking for. In my opinion the focus behave strangely on EditText views.
What I did was...
Make sure the root view is a RelativeLayout
Add an overlay layout that is OVER the area that will make the keyboard disapear, but not the EditText. Something like below:
In my case, the EditText was in a container at the bottom of the screen. so it covered everyhting else.
Have a method that looks a bit like this one :
private void hideKeyboard() {
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
keyboardOverlay.setVisibility(View.GONE);
editText.clearFocus();
}
Now call this method on the onClick of the overlay you created.
What we want now is to make the overlay visible when you press on the editText. You cannot use the onFocus event (at least I did not get it to work...) So what i did is I managed the onTouch event instead.
editText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(final View v, final MotionEvent event) {
keyboardOverlay.setVisibility(View.VISIBLE);
editText.requestFocus();
return false;
}
});
The requestFocus() here is to not override the focus event with the onTouch.
Quick advice, when you try this out, you can add a background color to the overlay to see exactly what is happening.
Hope it works for you!
set in a button or any view: this.getCurrentFocus().clearFocus();
For example:
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
this.getCurrentFocus().clearFocus();
return super.onMenuOpened(featureId, menu);
}
I had the same requirement as I had to hide the keyboard once I touch anywhere outside my EditText box. The setOnFocusChangeListener does not do the job as even if you touch outside the edit text box is still selected.
For this I used the solution edc598 here.
I first got the MainLayout containing the whole view and add touch listener to it.
When onTouch event was triggered I check if the EditText box has focus.
If the EditText box has focus then I check the event's X-Y co-ordinates.
Based on the placement of my EditText box I hide the key board if touched anywhere outside the box
Code sample modified from here:
LinearLayout MainLayout = (LinearLayout) findViewById(R.id.MainLayout);
EditText textBox = (EditText) findViewById(R.id.textBox);
int X_TOP_LEFT = 157;
int Y_TOP_LEFT = 388;
int X_BOTTOM_RIGHT = 473;
int Y_BOTTOM_RIGHT = 570;
MainLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(textBox.isFocused()){
Log.i("Focussed", "--" + event.getX() + " : " + event.getY() + "--");
if(event.getX() <= 157 || event.getY() <= 388 || event.getX() >= 473 || event.getY() >= 569){
//Will only enter this if the EditText already has focus
//And if a touch event happens outside of the EditText
textBox.clearFocus();
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
//Do something else
}
}
Log.i("X-Y coordinate", "--" + event.getX() + " : " + event.getY() + "--");
//Toast.makeText(getBaseContext(), "Clicked", Toast.LENGTH_SHORT).show();
return false;
}
});

EditText with soft keyboard and "Back" button

When I'm using "EditText" I have the virtual keyboard.
Pressing first time "Back" button hides the keyboard. Second press invokes "onBackPressed" callback in my activity. OK, but...
I have no idea how to hook the very first press. I need to process input data as soon as the virtual keyboard dismissed.
Any ideas are welcome.
Thanks.
You can override when the keyboard disappears using this method:
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK &&
event.getAction() == KeyEvent.ACTION_UP) {
// Do your thing here
return false;
}
return super.dispatchKeyEvent(event);
}
Taken from my other answer # : Android: Error popup on EditText doesn't move down when keyboard goes away
Custom Back Button:-
final RelativeLayout rrBack = (RelativeLayout) mCustomView.findViewById(R.id.rr_back);
rrBack.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MyApplication.getInstance().getRequestQueue().cancelAll(FEED_DETAIL_TAG_REQUEST);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(rrBack.getWindowToken(), 0);
}
});

Categories

Resources