android - softkeyboard keeps hiding after expanding Searchview programmatically (w/ Search Button) - android

I am using SearchView from Support Library in my Appcombat Actionbar.
On emulator it all works as intended. I can open it by clicking the menu item or using the Search Button on the device and the keyboard is shown.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_SEARCH) {
if(!MenuItemCompat.isActionViewExpanded(searchItem)) {
MenuItemCompat.expandActionView(searchItem);
} else MenuItemCompat.collapseActionView(searchItem);
}
return super.onKeyDown(keyCode, event);
}
On Expanding I set a saved text.
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener(){
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
searchView.onActionViewExpanded(); // to initialize searchview
searchView.setQuery(searchText, false);
searchView.requestFocus();
return true;
}
});
Problem:
On my hardware device (HTC Desire HD, Android 2.3), when I press the device's Search Button the searchview expands and the softkeyboard shows for a part of a second and hides again. Clicking the MenuItem works fine and shows the keyboard as intended. There seems to be a method hiding the keyboard again after focusing the searchview. I already tried several stuff (setOnQueryTextFocusChangeListener with manually showing softInput) but none seem to work on my device.
I hope someone got a solution. Thanks

It looks like your searchView has lost focus after setting the searchText.
try adding this before calling requestFocus.
searchView.setFocusable(true);

Related

How to hide ActionPopupWindow of Text Selection Handler of EditText

Actually I want to hide ActionPopupWindow (popup having SELECT ALL, CLIPBOARD options) when user click on + icon(refer to the attached image).
ActionPopupWindow appears when user click on the Text Selection Handler(bubble) (which appears when user tap on the text in the EditText).
I have tried to use setTextIsSelectable() method of EditText but it is not working consistently.
Any help or guidance will be well appreciated.
UPDATE: To hide the Popup already opened and showing on the screen, you need to clear focus of the current EditText or focus on other view when you clicked the plus button. See the
example below:
iconPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
yourEditText.clearFocus();
}
});
If you want the popup never shows up at the first place, there are many ways to do it.
The simplest way is disabling long click and selection feature:
yourEditText.setLongClickable(false);
yourEditText.setTextIsSelectable(false);
Second one is overriding action callback actions on your edittext:
yourEditText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
public void onDestroyActionMode(ActionMode mode) {
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
});
You can use them separately or together according to your case.
Also you can check other options from here

Hide CAB on EditText long press showing the Selection Markers

First of all, I have searched and tried numerous solutions posted here in StackOverflow but none of them solves my query, so this isn't a duplicate question.
I want to display a custom menu like this, when an EditText is long pressed,
As you can see that on Long Press on the EditText, this floating menu appears instead of the stock CAB, and the selection markers are also intact.
I want to achieve this kind of menu for my app. I have tried using this code, while it disabled the CAB but also prevents the selection markers from appearing,
editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
});
Also, if I try customizing the CAB menu, it won't render a menu like this, it will just alter the buttons on the existing CAB.
How can I achieve this floating menu along with the selection markers visible, when the EditText is long pressed or double tapped?
Perhaps you can use EditText's getSelectionStart and getSelectionEnd when the OS ActionMode starts (by overriding Activity's onActionModeStarted), then close the OS ActionMode and start your own while manually highlighting the appropriate text using EditText's setSelection.

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.

android onPrepareOptionsMenu doen't get second press on menu button

I notice that when implementing onPrepareOptionsMenu() in my activity - the first press works good - the callback from onPrepareOptionsMenu() starts. but when it still visible(open some dialog) and I want the second press to close it(trigger the callback to close the dialog) - the second press on the menu button doesn't triggers onPrepareOptionsMenu(). don't sure why
This is how I implement it:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (onDoneListener != null) {
onDoneListener.OnDone();
}
return false;
}
EDIT1:
I added the next function after the commenter help but still no luck. The OnKeyDown() also not receiving the next menu button press. It looks like the menu button doesn't get events until I'm pressing the back button. Here is the code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
if (onDoneListener != null)
onDoneListener.onDone();
}
return super.onKeyDown(keyCode, event);
}
I also tried to return true and false but not luck.
What you are seeing is the exact intended behavior of OnPrepareOptionsMenu. It runs before the menu is shown, not after.

How to recognize whether the Done button is clicked in ActionMode

I use ActionMode to select items in a grid. The problem is that I cannot recognize whether exactly the Done button is clicked. The only I can is to know that ActionMode is finished. But pressing Back finishes the ActionMode too.
The desired behavior is to accept selection on Done click, and exit ActionMode on Back press.
I tried to use ActionMode.setCustomView() but it doesn't affect the Done button. The Activity.onBackPressed() is not called when ActionMode is started.
The one solution I've found is to use ActionBarSherlock and get the Done button manually:
View closeButton = findViewById(R.id.abs__action_mode_close_button);
But it works on Android 2.x-3.x only, because on 4.x a native action bar is used.
Please don't do that as it's implementation specific and extremely non-standard.
You can use the onDestroyActionMode callback for when an action mode is dismissed.
Here is the solution:
ActionMode mMode = MyActivityClass.this.startActionMode(some implementation);
int doneButtonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
View doneButton = MyActivityClass.this.findViewById(doneButtonId);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do whatever you want
// in android source code it's calling mMode.finish();
}
});
Here is my implementation, and it's a proper hack but it works and I can't really find an alternative to doing something specific when the ActionMode DONE is clicked. I find it really weird that you can't capture this event more elegantly.
Any suggestions to making this slightly less ugly would be greatly appreciated...
In my activity..
boolean mActionModeIsActive = false;
boolean mBackWasPressedInActionMode = false;
#Override
public boolean dispatchKeyEvent(KeyEvent event)
{
mBackWasPressedInActionMode = mActionModeIsActive && event.getKeyCode() == KeyEvent.KEYCODE_BACK;
return super.dispatchKeyEvent(event);
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
{
mActionModeIsActive = true;
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode)
{
mActionModeIsActive = false;
if (!mBackWasPressedInActionMode)
onActionModeDoneClick();
mBackWasPressedInActionMode = false;
}
public void onActionModeDoneClick();
{
// Do something here.
}
If you are using Fragments with your Activity then some of this code will probably need to be in the Fragment, and the other bits in the Activity.
#JakeWharton (and other ActionBarSherlock users) if you see this on your travels. I'd be interested to know if the above is compatible with ABS as I have yet to integrate ABS with my current project.

Categories

Resources