I am trying to show and hide the menu and toolbar in the adapter class. The elements show fine but they don't hide as expected. This is my implementation
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater menuInflater = mode.getMenuInflater();
toolbar.setVisibility(View.GONE);
menuInflater.inflate(R.menu.menu, menu);
return true;
}
public void onDestroyActionMode(ActionMode mode) {
mode.getMenu().clear();
toolbar.setVisibility(View.VISIBLE);
mode.finish();
}
I want to hide that menu which inflates in onCreateActionMode() method.The "menu" does not disappear as it says that mode.getMenu().clear() is implemented on null object. How do I hide menu in onDestroyActionMode ? Any suggestion would be helpful Thanks.
Put this code in your button click listener. it will definitely work.if (getSupportActionBar() != null) {getSupportActionBar().hide();} Just like this.
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
return false;
}
});
I have the same question which I found here which I will re-iterate because the solution is not 100% exactly what I need:
I currently have a SearchView in the action bar of my app. When I click the search icon, the SearchView expands and the keyboard pops up as expected. Clicking the "X" in the SearchView box closes the SearchView as expected. However, when the SearchView is activated and I press the "back" button, my app is exited. This is the correct behavior, but what I am trying to do now is to capture back button press and just have it close the SearchView (not my app) when the SearchView is visible. Is there a way to invoke the SearchView OnCloseListener() programmatically on a back button press? For example, something like this:
Here is the solution:
#Override
public void onBackPressed() {
if (!searchView.isIconified()) {
searchView.setIconified(true);
} else {
super.onBackPressed();
}
}
The problem is the solution requires the user to press back not once, but twice: once* to exit the keyboard and **once to close the SearchView.
How can I close the keyboard AND the SearchView at the same time by pressing back once?
edit:
Somone had a similar problem with EditText and the solution was to subclass the EditText view.
#Override
public void onBackPressed() {
if (!searchView.isIconified()) {
searchView.setIconified(true);
searchView.onActionViewCollapsed()
} else {
super.onBackPressed();
}
}
below method is Clear your text Only
searchView.setIconified(true);
below This methods is close your search view
searchView.onActionViewCollapsed()
try
#Override
public void onBackPressed() {
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
if (!searchView.isIconified()) {
searchView.setIconified(true);
} else {
super.onBackPressed();
}
}
Add a method to close the keyboard within onBackPressed()
here is the code to hide keyboard.
private void hideKeyboard() {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mInputField.getWindowToken(), 0);
}
Based on #Archana answer, the onBackPressed() should be like :
#Override
public void onBackPressed() {
hideSoftKeyboard();
if (!searchView.isIconified()) {
searchView.setIconified(true);
} else {
super.onBackPressed();
}
}
private void hideSoftKeyboard(){
View view = activity.getCurrentFocus ();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService (Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow (view.getWindowToken (), 0);
}
}
or you can also override the onKeyDown()
#Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
hideSoftKeyboard ();
if (! searchView.isIconified ()) {
searchView.setIconified (true);
}
return true;
}
return super.onKeyDown (keyCode, event);
}
private void hideSoftKeyboard() {
View view = activity.getCurrentFocus ();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService (Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow (view.getWindowToken (), 0);
}
}
An interesting answer!
#Override
public void onBackPressed() {
if (!searchView.isIconified()) {
searchView.setIconified(true);
searchView.setIconified(true);
} else {
super.onBackPressed();
}
}
public class MainActivity extends AppCompatActivity {
MenuItem menuItemSearch;
#Override
protected void onResume() {
if(menuItemSearch!=null)
MenuItemCompat.collapseActionView(menuItemSearch); // while activity begins, searchView is closed if remained open.
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menuItemSearch = menu.findItem(R.id.menuSearch);
}
}
I did it like this in the activity where the SearchView is located
#Override
public void onBackPressed() {
super.onBackPressed();
if (!searchView.isIconified()) {
searchView.setIconified(true);
}
}
Instead of hide soft keyboard manually, try clearFocus() method
mSearchView.setIconified(true);
mSearchView.clearFocus();
I am using a search field in the actionbar by reffering it to an xml in the layout folder called search_layout.xml.In the 2.3.3 version of android, there is an issue like the search icon is not shown in the keypad for the first time after I enter the text. Instead I have to click to the "enter" key, then on the textfield and then only the "search" icon appears. This does not happen in higher versions like 4.0.4 and 4.4.2 of android.
Here is the search_layout.xml:
And this xml is reffererd to in res>>menu>>menu.xml
<item
android:id="#+id/action_notification"
android:actionLayout="#layout/search_layout"
android:icon="#drawable/ic_searchicon"
android:orderInCategory="0"
android:showAsAction="always|collapseActionView"
android:title="Search"/>
This is the code snippet I am using for this in my activity called Activity_HomeScreen class.
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem itemListMap,itemRefresh;
editsearch = (EditText) menu.findItem(R.id.action_notification).getActionView();
editsearch.addTextChangedListener(textWatcher);
editsearch.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (actionId == EditorInfo.IME_ACTION_SEARCH)
{
editsearch.clearFocus();
InputMethodManager imm = (InputMethodManager)Activity_HomeScreen.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editsearch.getWindowToken(), 0);
captureViepagerFragments(mVpContainer.getCurrentItem());
}
return false;
}
});
MenuItem menuSearch = menu.findItem(R.id.action_notification);
menuSearch.setOnActionExpandListener(new OnActionExpandListener()
{
// Menu Action Collapse
#Override
public boolean onMenuItemActionCollapse(MenuItem item)
{
// Empty EditText to remove text filtering
editsearch.setText("");
editsearch.clearFocus();
InputMethodManager imm = (InputMethodManager)Activity_HomeScreen.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editsearch.getWindowToken(), 0);
captureViepagerFragments(mVpContainer.getCurrentItem());
return true;
}
// Menu Action Expand
#Override
public boolean onMenuItemActionExpand(MenuItem item)
{
// Focus on EditText
editsearch.requestFocus();
// Force the keyboard to show on EditText focus
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
Solved this issue by doing like this:
public boolean onMenuItemActionExpand(MenuItem item)
{
editsearch.clearFocus();
editsearch.post(new Runnable() {
#Override
public void run() {
editsearch.requestFocus();
final InputMethodManager imm = (InputMethodManager) Activity_Cat_Products.this
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editsearch, InputMethodManager.SHOW_IMPLICIT);
}
});
return true;
}
I have a collapsed search EditView menu within my Actionbar (ActionbarSherlock). When setShowAsAction method is set to SHOW_AS_ACTION_NEVER, the keyboard displays and the user can start typing, as expected.
However when I set the value to SHOW_AS_ACTION_ALWAYS, the user has to touch the EditText before they start typing as it doesn't appear to have focus!
Is there something wrong with my code or a way to work around this problem?
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
final MenuItem search = menu.add(0, MENU_SEARCH, MENU_SEARCH, getString(R.string.search));
search.setIcon(R.drawable.ic_action_search);
search.setActionView(R.layout.action_search);
search.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_SEARCH:
mSearch = (EditText) item.getActionView();
mSearch.addTextChangedListener(mFilterTextWatcher);
mSearch.requestFocus();
mSearch.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}, 200);
break;
}
return true;
}
action_search.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="#string/search" />
The solution was to nest the fragment within the Activity.
public class ProductListActivity extends SherlockFragmentActivity {
…
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
ProductListFragment list = new ProductListFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
}
}
public static class ProductListFragment extends SherlockListFragment {
…
}
}
I have a searchView in the ActionBar. I want to dismiss the keyboard when the user is done with input. I have the following queryTextListener on the searchView
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
// Do something
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
showProgress();
// Do stuff, make async call
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
return true;
}
};
Based on similar questions, the following code should dismiss the keyboard, but it doesn't work in this case:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
I've also tried:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
Neither one works. I'm not sure if this is a Honeycomb specific problem or if it's related to the searchView in the ActionBar, or both. Has anyone gotten this working or know why it does not work?
I was trying to do something similar. I needed to launch the SearchActivity from another Activity and have the search term appear on the opened search field when it loaded. I tried all the approaches above but finally (similar to Ridcully's answer above) I set a variable to SearchView in onCreateOptionsMenu() and then in onQueryTextSubmit() called clearFocus() on the SearchView when the user submitted a new search:
private SearchView searchView;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
searchView = (SearchView) menu.findItem(R.id.menu_search)
.getActionView(); // set the reference to the searchView
searchView.setOnQueryTextListener(this);
searchMenuItem = (MenuItem) menu.findItem(R.id.menu_search);
searchMenuItem.expandActionView(); // expand the search action item automatically
searchView.setQuery("<put your search term here>", false); // fill in the search term by default
searchView.clearFocus(); // close the keyboard on load
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
performNewSearch(query);
searchView.clearFocus();
return true;
}
Simple, straight to the point and clean:
#Override
public boolean onQueryTextSubmit(String query) {
// your search methods
searchView.clearFocus();
return true;
}
just return false on onQueryTextSubmit just like below
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
Somehow it works if you call
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
and then
otherWidget.requestFocus();
For me, none of the above was working on the first submit. It was hiding and then immediately re-showing the keyboard. I had to post the clearFocus() on the view's handler to make it happen after it was done with everything else.
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
if (!"".equals(query)) {
mSearchView.post(new Runnable() {
#Override
public void run() {
mSearchView.clearFocus();
}
});
}
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
For me, the following works:
In my activity I have a member variable
private SearchView mSearchView;
In onCreateOptionsMenu() I set that variable like so:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.library, menu);
mSearchView = (SearchView)menu.findItem(R.id.miSearch).getActionView();
mSearchView.setOnQueryTextListener(this);
return true;
}
In the QueryTextListener at last, I do this:
mSearchView.setQuery("", false);
mSearchView.setIconified(true);
I had a look at the source code of SearchView, and if you do not reset the query text to an empty string, the SearchView just does that, and does not remove the keyboard neither. Actually, drilling down deep enough in the source code, it comes to the same, yuku suggested, but still I like my solution better, as I do not have to mess around with those low level stuff.
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;
}
});
if someone is looking how to collpase searchView/keyboard, use below code
/*
setup close button listener
*/
searchView.findViewById<ImageView>(R.id.search_close_btn).setOnClickListener {
adapter.filter(null)//reset default list
searchView.onActionViewCollapsed() //collapse SearchView/Keyboard
true
}
/*
setup text change listener
*/
searchView.setOnQueryTextListener(object:SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
adapter.filter(if(query.isNullOrEmpty()) "" else query)
searchView.onActionViewCollapsed() //collapse SearchView/Keyboard
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
adapter.filter(if(newText.isNullOrEmpty()) "" else newText)
return true
}
})
In a tablet app I'm working on with a dual pane activity, I've wrote only
f.getView().requestFocus(); // f is the target fragment for the search
and that was enough to dismiss the soft keyboard after a search. No need to use InputMethodManager
I used ActionBarSherlock 4.3 and I have a ProgressDialog. When I dismiss it in postExecute method, Searchview gain focus. To fix that:
//Handle intent and hide soft keyboard
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
searchView.setQuery("", false);
searchView.setIconified(true);
searchView.clearFocus();
}
/*When dismiss ProgressDialog, searchview gain focus again and shows the keyboard. I call clearFocus() to avoid it.*/
AsyncTask.onPostExecute(){
if(pd!=null)
pd.dismiss();
if(searchView!=null)
searchView.clearFocus();
}
Hope it helps.
You can use it.
if (isKeybordShowing(MainActivity.this, MainActivity.this.getCurrentFocus())) {
onBackPressed();
}
public boolean isKeybordShowing(Context context, View view) {
try {
InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(view.getWindowToken(), 0);
return keyboard.isActive();
} catch (Exception ex) {
Log.e("keyboardHide", "cannot hide keyboard", ex);
return false;
}
}
Two solutions that worked for me, the first one using the SearchView instance:
private void hideKeyboard(){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
}
Second solution:
private void hideKeyboard(){
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
Below Code is working for me to hide keyboard in Searchview
MenuItem searchItem = baseMenu.findItem(R.id.menuSearch);
edtSearch= (EditText) searchItem.getActionView().findViewById(R.id.search_src_text);
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
edtSearch.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
}
});
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
});
Why dont you try this? When you touch the X button, you trigger a focus on the searchview no matter what.
ImageView closeButton = (ImageView)searchView.findViewById(R.id.search_close_btn);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText et = (EditText) findViewById(R.id.search_src_text);
et.setText("");`enter code here`
searchView.setQuery("", false);
searchView.onActionViewCollapsed();
menuSearch.collapseActionView();
}
});
fun View.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
#Override
public void onResume() {
super.onResume();
searchView.clearFocus();
}
I tried all the answers above but none worked, clearing focus onResume worked for me