How to hide default keyboard when click on menu? - android

I have tried several methods for this website by inserting the code in the onCreateOptionsMenu (Menu menu) without success. I want to hide the keyboard when I click the menu button.
I have three EditText where I write some data, and options to insert / delete / modify a database, are on the menu, but if I click, the keyboard does not hide automatically.
I have something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
if(this.getCurrentFocus() != null && this.getCurrentFocus() instanceof EditText){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
return true;
}
It only works the first time I press the menu button.
Thanks!

Move the code to onOptionsItemSelected instead
public boolean onOptionsItemSelected(MenuItem item) {
.....
if(this.getCurrentFocus() != null && this.getCurrentFocus() instanceof EditText){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
return super.onOptionsItemSelected(item);
}

Another place you can put the InputMethodManager code is in the onPrepareOptionsMenu() callback like this:
public boolean onPrepareOptionsMenu (Menu menu) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
return true;
}
You may prefer this if you want the keyboard hidden regardless of whether the user actually subsequently clicks on any of the menu items.

Related

How to close searchView in onOptionsItemSelected(MenuItem item) when onBackPressed don't work?

Need code to close the searchView when BACK is pressed. So far only have code that closes the keyboard when BACK is pressed.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (item.getItemId() == android.R.id.home) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
View view = this.getCurrentFocus();
if (view != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
return super.onOptionsItemSelected(item);
}
I tried the following line but this line closes the entire app for some reason when "back" clicked on...
onBackPressed();
The following line is deprecated...
MenuItemCompat.collapseActionView(menuItem);
I've seen some answers on stackOverflow but most of them have to do with onBackPressed() or adding searchView.collapseActionView().
But I can't add searchView in onOptionsItemSelected(MenuItem item) unless I redeclare it with SearchView searchView = (SearchView) item.getActionView(); and then add searchView.collapseActionView() but then the app crashes when BACK pressed.
I got the keyboard to close but how do I close the searchView in onOptionsItemSelected(MenuItem item) ?
EDIT:
When I use onBackPressed() or super.onBackPressed() or this.onBackPressed(), the first time I click on "back" button, searchView and keyboard close, but when I click on search Icon again to open searchView and keyboard pops up, if I click on "back" again, the entire app closes, not crashes, just closes and takes me to Android Phone home screen. Why is this happening?
Update
#Override
public void onBackPressed() {
if (!yourSearhView.isIconified()) {
yourSearhView.onActionViewCollapsed();
} else {
super.onBackPressed();
}
}
This won't work if the you have set app:showAsAction="always"

Cannot gain focus on edittext programmatically in Android (even after trying previous answers on Stack Overflow)

Note: MY question is not about losing focus, it's about gaining focus, hence it is not a duplicate of the marked question
I have a search menuItem on my toolbar which, on click will open up a search layout ontop of it and allow user to search. I have tried the following solutions mentioned elsewhere in Stack Overflow but none of them seems to work. (I should mention that due to it being used elsewhere I can't edit the XML)
from here
searchText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(_searchText, InputMethodManager.SHOW_IMPLICIT);
This just pops up the keyboard, nothing happens I have tried this + editText.setCursorVisible(true);
With various combinations of setFocusable(true) and setFocusableInTouchMode(true); before the aformentioned
searchText.setFocusable(true);
searchText.setFocusableInTouchMode(true);
searchText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(_searchText, InputMethodManager.SHOW_IMPLICIT);
Doing (1) in separate thread (using Handler.post)
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
},100);
Calling editText.performClick()
Clearing previous focus like so:
private void clearFocus() {
if (activity.getCurrentFocus() != null) {
InputMethodManager imm = (InputMethodManager) activity.getApplication().getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
activity.getCurrentFocus().clearFocus();
}
}
My code sample:
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.action_search) {
menuItem.setVisible(false);
onClickForSearchMenuItem();
return true;
}
return super.onOptionsItemSelected(menuItem);
}
private void onClickForSearchMenuItem() {
if (searchLayoutInToolbar != null) {
searchLayoutInToolbar.setVisibility(View.VISIBLE);
final EditText editText = (EditText) searchLayoutInToolbar.findViewById(R.id.custom_Search_Layout_Search_EditText);
//clearFocus();
//editText.setFocusableInTouchMode(true);
//editText.setFocusable(true);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
activity.invalidateOptionsMenu();
}
}
The developer pages at android point out that
A View will not take focus if one of its parents has getDescendantFocusability() equal to FOCUS_BLOCK_DESCENDANTS
but none of the parents of my view are set to that
Try This.... I hope, this may help you.
searchText.setFocusableInTouchMode(true);
searchText.requestFocus();
this might be required on some phones (some of the older devices):
final InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);
If not worked, try field.requestFocus() in the onResume() method of the activity (instead of the onCreate()).
Thanks.

How to automatically enter in edit mode in a search?

I've implemented a classic search feature in my action bar but when I click on the search menu button, the search editText appears but I have to click on it to enter in edit mode whereas most of application directly enter in edit mode after displaying the editText.
How to fix it?
My code is just:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Place an action bar item for searching.
MenuItem item = menu.add("Search");
item.setIcon(android.R.drawable.ic_menu_search);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
mSearchView = new Fragment_siteManager.MySearchView(getActivity());
mSearchView.setOnQueryTextListener(this);
mSearchView.setOnCloseListener(this);
mSearchView.setIconifiedByDefault(true);
mSearchView.setQueryHint(getString(R.string.label_tvGivePosition));
item.setActionView(mSearchView);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
EDIT
I tried the following code but it doesn't work:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
View view = mSearchView.findFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
return super.onOptionsItemSelected(item);
}
Regards,
According to the Android docs, SearchView has Edittext behavior so you shouldn't need to force the softkey. It seems your issue is that it is in the onOptionItemSelected() when it should be in the onCreateOptionMenu(). If you MUST, the code to force is below. Take a careful long look at the links at the bottom of this post.
Get a the InputMethodManager
Call the method to show the soft keys and pass it the search view and a flag, SHOW_FORCED
Done!
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mSearchView, InputMethodManager.SHOW_FORCED);
Look at the class to get more info. Also look at the Android Docs on SearchView.

How to I recognize that mobile keyboard is active?

I want when mobile keyboard is active hide the button on the activity.
I search and found this code, but don't worked.
private boolean is_active_keyboard() {
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText())
return true;
else
return false;
}

Unable to hide the virtual keyboard of SearchView iconfiedbydefault(false)

I have a search view which is set as expanded by default with default search query but i don't want the virtual keyboard.In below code i tried to hide keyboard in onCreateOptionsMenu but still keyboard is visible.
imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
MenuItem item = menu.findItem(R.id.menu_search);
item.expandActionView();
mSearchView = (SearchView) item.getActionView();
mSearchView.setIconifiedByDefault(false);
mSearchView.setQuery(query, true);
imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
I am using sherlock search view widget. any suggestion to hide the virtual keyboard.What i am doing wrong?
Inspired by Parnit's answer, I've found a better method, which also works and is more beautiful:
mSearchView.clearFocus();
Edit: I added the better solution on top, but also kept the old answer as a reference.
#Override
public boolean onQueryTextSubmit(String query) {
searchView.clearFocus();
return false;
}
Original Answer: I programmed using a setOnQueryTextListener. When the searchview is hidden the keyboard goes away and then when it is visible again the keyboard does not pop back up.
//set query change listener
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
/**
* hides and then unhides search tab to make sure keyboard disappears when query is submitted
*/
searchView.setVisibility(View.INVISIBLE);
searchView.setVisibility(View.VISIBLE);
return false;
}
});
try
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
add the below line in the manifest for particular Activity.
android:windowSoftInputMode="adjustPan|stateHidden"
simple solution its work for my
add to XML:
android:focusable="false"
In Android Manifest:
android:windowSoftInputMode="adjustPan|stateHidden"
In class open and close the keyboard:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action buttons
switch(item.getItemId()) {
case R.id.search:
//TODO Whatever
search.clearFocus();
//Open and close the keyboard
InputMethodManager imm = (InputMethodManager)MyApplication.getAppContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
return true;
u just have to use:
"object(edittext, searchview, etc)".clearfocus() ;
use it after u generate a search or an action. Example: in the method OnQueryTextListener, after that i use a search. For searchview.

Categories

Resources