How to automatically enter in edit mode in a search? - android

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.

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"

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.

Showing the soft keyboard for SearchView on ActionBar

We've got a SearchView on the ActionBar which is set to be non-iconified. As we don't have any content in the view until the user's entered something to search for, it would make sense to give the SearchView initial focus, and make sure the soft keyboard is showing ready for the user to enter text — otherwise they'll always have to first tap in the SearchView.
I can give the SearchView focus by just calling
searchView.requestFocus();
but I can't get the soft keyboard to appear. In another one of our Fragments I have an EditText which we want to be focused I can get the soft keyboard to appear there by calling
InputMethodManager mgr = (InputMethodManager)getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
but this just doesn't work on the SearchView. It must surely be possible to get this to work.
Further rummaging around StackOverflow and I found this question:
Forcing the Soft Keyboard open
which contains a solution that worked for me:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).
toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
I have a similar problem where none of the proposed solutions here worked. Some just didn't make the keyboard appear at all and some show a keyboard but the key presses there just do not work.
The only thing that worked was:
// hack for making the keyboard appear
searchView.setIconified(true);
searchView.setIconified(false);
I am using a SearchView with setIconifiedByDefault(false). Testing with Android 4.4.2, the only way I could get the keyboard to actually show was to look at the source code for SearchView and mimic how it requested the keyboard to be shown. I've tried literally every other method I could find/think of and this is the only way I could get the keyboard to show reliably. Unfortunately, my method requires some reflection.
In onCreateOptionsMenu(Menu):
searchView.requestFocus();
searchView.post(new Runnable() {
#Override
public void run() {
showSoftInputUnchecked();
}
});
And then create a method to call the hidden method "showSoftInputUnchecked" in InputMethodManager:
private void showSoftInputUnchecked() {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
Method showSoftInputUnchecked = null;
try {
showSoftInputUnchecked = imm.getClass()
.getMethod("showSoftInputUnchecked", int.class, ResultReceiver.class);
} catch (NoSuchMethodException e) {
// Log something
}
if (showSoftInputUnchecked != null) {
try {
showSoftInputUnchecked.invoke(imm, 0, null);
} catch (IllegalAccessException e) {
// Log something
} catch (InvocationTargetException e) {
// Log something
}
}
}
}
As with all solutions that access methods not in the public API, I can't promise that this won't break with new versions of Android.
It worked for me.
private SearchView mSearchView;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView =
(SearchView) searchItem.getActionView();
mSearchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
mSearchView.setIconifiedByDefault(true);
mSearchView.setFocusable(true);
mSearchView.setIconified(false);
mSearchView.requestFocusFromTouch();
}
return super.onOptionsItemSelected(item);
}
If you wanna show the soft keyboard and focus on the input box, you can try
final MenuItem menuItem = menu.findItem(R.id.action_search);
menuItem.expandActionView();//expand show soft keyboard
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_actionbar_search"
liven:showAsAction="collapseActionView|always" //always
liven:actionViewClass="android.support.v7.widget.SearchView" />enter code here
This will not show keyboard every time you come on activity
searchview.clearFocus();
Use expandView method :
Your onCreateOptionsMenu could be something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
MenuItem mitem = menu.add("Search");
mitem.setIcon(ic_search_inverse)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
mitem.expandActionView();
listsearch.setOnItemClickListener(this);
return true;
}
You can try what I did. This worked well for me.
//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
* 4 = INVISIBLE
* 0 = VISIBLE
*/
searchView.setVisibility(4);
searchView.setVisibility(0);
return false;
}
});

How to hide default keyboard when click on menu?

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.

Android: Custom keypad for a collapsible action view in an action bar?

I've added a custom ActionView to my action bar, and it's meant to allow users to look something up by entering a number. However, the EditText I used for the layout always brings up a full alphanumeric keyboard even though I specified imeType of number. I don't want an alphanumeric keyboard. Heck, I don't even want the +/- type options. Just 0-9 and a "Done." How can I have my EditText for the search use a custom keypad? Is there a way to do a dropdown keyboard? I was hoping to write a custom IME just for that field, but it appears that is not permitted.
XML for menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_keypad"
android:icon="#drawable/ms_btn_keypad_sel"
android:title="Keypad"
android:showAsAction="ifRoom|collapseActionView"
android:actionLayout="#layout/keypad_actionview"/>
</menu>
XML for action layout:
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keypad_actionview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#color/White"
android:drawableLeft="#drawable/icon_channel_keypad"
android:drawablePadding="5dp"
android:inputType="number"
android:imeOptions="flagNoExtractUi"
/>
Java for setting up the action bar menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.number_menu, menu);
keypadMenuItem = menu.findItem(R.id.menu_keypad);
final EditText keypadText = (EditText) keypadMenuItem.getActionView();
keypadText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(final TextView textView, final int i, final KeyEvent keyEvent) {
keypadMenuItem.collapseActionView();
changeDisplayedNumber(Integer.valueOf(textView.getText().toString()), true);
textView.setText("");
return true;
}
});
keypadMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
startAutoHidePlayerControlsRunner();
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
stopAutoHidePlayerControlsRunner();
keypadText.setText("");
keypadText.requestFocus();
/*keypadText.setInputType(InputType.TYPE_CLASS_NUMBER);
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
}*/
return true; // Return true to expand action view
}
});
return true;
}
The commented out code at the end of the java was to try and force the keyboard to display right away, but that was causing some strange behavior - non-numeric keyboards popping up.
I realize it's that it's a default input issue with the Asus keyboard
Yes, the Transformer Prime seems to be somewhat broken on this issue. That being said, bear in mind that no IME will necessarily honor any given inputType. For example, the Graffiti IME is all pen-based and therefore probably has no real notion of "number" vs. "text". inputType is a request, not a contract.
can I forcibly swap in a different input method
No. That has some nasty malware potential if it were possible.
is there a clever way to make the actionView have the edittext in the action bar and a custom keyboard in a dropdown/at the bottom
You are welcome to get control when the EditText gets the focus (setOnFocusChangeListener()) and then do something, like make visible your in-activity custom keyboard that heretofore had been gone, or display a ListPopupWindow that you hack into a numeric keypad, or...

Categories

Resources