Text Selection in TextView - android

I know there is an attribute android:textIsSelectable="true" which enables text selection in a TextView by a long click on text and after that it shows an ActionMode for manipulating text.My question is how can I disable the default ActionMode? I tried to disable it by writing a custom ActionMode and returning false in it's onCreateActionMode method:
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
and passed it to the:
TextView.setCustomSelectionActionModeCallback(customActionMode);
It ruined text selection and disabled Start-End pointers.
I want to disable ActionMode to write my own PopupWindow after selecting text.
Do you have any suggestions?

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.

How can I tell when text is being typed into my Android SearchView on the ActionBar

I want to disable certain features of my app while the user is entering text for a search. The xml for the relevant item in my ActionBar is
<item android:id="#+id/actionbar_search"
android:orderInCategory="1"
android:showAsAction="always|withText|collapseActionView"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/earth_2508858_search_en"
android:inputType="textPostalAddress" />
and in the corresponding code that I have at present to cater for the search is
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
MenuItem DestinationTxt = menu.findItem(R.id.actionbar_search);
final SearchView mySearchView = (SearchView)DestinationTxt.getActionView();
mySearchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) { return false; }
#Override
public boolean onQueryTextSubmit(String query) {
//Hide the Keyboard
imm.hideSoftInputFromWindow(mySearchView.getWindowToken(), 0);
// CODE TO DO THE SEARCH
return true;
}
});
}
I've browsed the methods on SearchView, but I didn't see anything that would tell me whether it's active or not. I'm also worried about putting in a boolean state variable to indicate when the text is being typed into the SearchView, in case some behaviour that I haven't catered for occurs (e.g. back button pressed, activity gets suspended), and somehow the state variable gets stale so that the disabled features stay disabled. So I'm looking for a robust way of doing this, all help appreciated :-).
Update. An answer below suggests using the interface OnFocusChangeListener which is implemented by the mySearchView object, and/or the mySearchView.isFocussed() method. Both sounded promising, however I've now tested and neither seem to work. Perhaps their failure has got something to do with the fact that this SearchView is in the ActionBar? In any case, I'm still after a robust solution.
It's right there.
mySearchView.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) { return false; }
That's where you'll get updates to text changes in the SearchView.
The return value should be as such (documentation):
Returns
false if the SearchView should perform the default action of showing any suggestions if available, true if the action was handled by the listener.
If you want to know if the SearchView has been activated or deactivated, use View.setOnFocusChangeListener(View.OnFocusChangeListener);
public interface OnFocusChangeListener{
public void onFocusChange (View v, boolean hasFocus);
// The boolean will tell you if it's focused or not.
}
Since monitoring the focus didn't work, I looked at the SearchView documentation again. It's a bit convoluted, but it seems like the intended solution to this problem.
If your SearchView is inflated from a menu XML in onCreateOptionsMenu(), then you can add this line:
menu.findItem(/* your SearchView's ID here */).setOnActionExpandListener(
new OnActionExpandListener(){
#Override
public boolean onMenuItemActionCollapse (MenuItem item){
enableInteraction();
return true; // Allow the SearchView to collapse.
}
#Override
public boolean onMenuItemActionExpand(MenuItem item){
disableInteraction();
return true; // Allow the SearchView to expand.
}
}
);
Then enable and disable your Activity's views in enableInteraction() and disableInteraction(), respectively. You should retain the MenuItem in your Activity so you can query it in onResume() like so:
#Override
public void onResume(){
super.onResume();
searchViewMenuItem.isActionViewExpanded() ?
disableInteraction() : enableInteraction();
}
This part might not be needed. The SearchView might automatically get collapsed when the Activity is hidden and stay that way, so you can simply call enableInteraction() in onResume() so your user isn't locked out.
If you just need to reference the state of the SearchView, use
searchViewMenuItem.isActionViewExpanded();

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...

Disable menu button?

I have a custom menu options that I want to disable it from popping up if a button on screen is clicked..
I thought of using this code but it doesnt work:
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
if (Schedule)
menu.getItem(1).setVisible(View.GONE);
return true;
}
Is there a way to prevent the menu button from doing anything? Thanks.
According to the documentation:
You must return true for the menu to be displayed; if you return false it will not be shown.
So I'm guessing this will work:
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
.... Code .....
return !Schedule;
}
That is assuming that you want the menu to display when Schedule is equal to false.

Categories

Resources