Issue with search key on soft keyboard - android

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;
}

Related

How to hide soft keyboard without losing focus on searchview

Expand searchview when fragment is oppened , request focus on searchview but hide soft keyboard .
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.barcode,menu);
MenuItem scan = menu.findItem(R.id.scanbarcode);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem scan = menu.findItem(R.id.scanbarcode);
mBarcode = (android.support.v7.widget.SearchView) scan.getActionView();
mBarcode.setQueryHint(getString(R.string.scanb));
scan.expandActionView();
mBarcode.setOnQueryTextListener(this);
}
write this in your activity in manifest file
android:windowSoftInputMode="stateHidden|adjustResize"
and in your fragment rite this
searchTditText.requestFocus();
Call this method to hide soft keyboard.
public static void hideSoftKeyboard(Context context, View view) {
if (view != null) {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Check this code:
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

How to access SearchView children?

I am trying to access EditText field of SearchView (v7) inside my application but I am always ending up at null pointer exception.
So far I've tried a solution and tried to access the EditView using android:id/search_plate , R.id.search_plate, R.id.search_src_text and android:id/search_src_text but all in vain.
Code:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.frag_menu_items, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
searchPlate = (EditText) searchView.findViewById(searchPlateId);
searchPlate.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
searchPlate.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
ToastClass.getInstance().showCustomMsg(getActivity(),searchPlate.getText().toString());
}
return false;
}
});
//searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(menuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
// Toast.makeText(getActivity(),"collapsed",Toast.LENGTH_LONG).show();
searchMeta.setVisibility(View.GONE);
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
// Toast.makeText(getActivity(),"expanded",Toast.LENGTH_LONG).show();
searchMeta.setVisibility(View.VISIBLE);
return true; // Return true to expand action view
}
});
super.onCreateOptionsMenu(menu,inflater);
}
I am trying to access EditText field of SearchView (v7) inside my application.
You can do it with Java code.
For android.support.v7.widget.SearchView:
EditText editText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
For android.widget.SearchView:
EditText editText = (EditText) searchView.findViewById(android.R.id.search_src_text);
Try this
searchView = (SearchView) menu.findItem(R.id.search_plate).getActionView();

android actionbar sherlock collospe and expand

I have the following code:
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
Intent i=new Intent(class1.this, clas2.class);
startActivity(i);
return true;
}
return false;
}
My issues is following:
I have an search icon in the ActionBar. When I tap on the search icon an edittext is opened and cancel button shown. The search is working properly. Now, when I click the cancel button I want to hide the edittext and cancel button. How can I achieve this?
I solve my problem... using
item.collapseActionView();
Thanks for reply and hope it this will help others..
Here is my code where i have used it.....
private EditText search;
private Button search_cancle;
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
int id = item.getItemId();
if(id==R.id.action_search){
View v = (View) item.getActionView();
search = ( EditText ) v.findViewById(R.id.txt_search);
search.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
search_cancle=(Button)v.findViewById(R.id.search_cancel);
search_cancle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
item.collapseActionView(); // Here I have set it.....
search.setText("");
}
});
return true;
}
else
{
return super.onOptionsItemSelected(item);
}
}

Android: AutoCompleteTextView as Collabsible ActionItem Keyboard not shown on Focus

I have an AutoCompleteTextView as Collabsible ActionItem, and i want the keyboard to be displayed when it gets expanded and focussed. This is my code in onCreateOptionsMenu():
menu.add("Search")
.setIcon(R.drawable.ic_search)
.setActionView(R.layout.collapsible_edittext)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
final MenuItem searchMenuItem = menu.getItem(0);
searchMenuItem.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) item
.getActionView();
autoCompleteTextView
.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
Log.d(TAG, "onFocusChange: " + hasFocus);
if (hasFocus) {
mInputManager.showSoftInput(v,
InputMethodManager.SHOW_FORCED);
} else {
mInputManager.hideSoftInputFromWindow(
v.getWindowToken(), 0);
}
}
});
autoCompleteTextView.requestFocus();
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) item
.getActionView();
autoCompleteTextView.clearFocus();
autoCompleteTextView.setText("");
return true;
}
});
When I click on the ActionItem the first time, no keybord is shown. After collapsing and then clickin on the Item the keyboard gets shown. But I want the keyboard to be shown the first time the user clicks on the action item.
I'm using ActionBarSherlock if that matters.
So why is the keyboard not shown on first expansion? Any ideas?
Have you tried manually setting the focus to the edit text and manually showing the keyboard in the onOptionsItemSelected method?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_SEARCH:
// select edittext here
// show keyboard here
return true;
}
return super.onOptionsItemSelected(item);
}
EDIT: I found this code to show the keyboard from here:
https://code.google.com/p/android-batavierenrace/source/browse/trunk/BataApp/src/com/ut/bataapp/activities/TeamsActivity.java
I tested it on my app and it works...
public static void setKeyboardFocus(final EditText primaryTextField) {
(new Handler()).postDelayed(new Runnable() {
public void run() {
primaryTextField.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
primaryTextField.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
}
}, 100);
}

How to dismiss keyboard in Android SearchView?

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

Categories

Resources