Customize the ActionMenu of an EditText - android

I want to provide simple formatting buttons for an EditText, for example, a button that inserts <strong></strong> if no text was selected and wraps the selected text in <strong> tags otherwise.
I am using the Support Library v7 with ActionBarCompat. I managed to start a custom ActionMode upon focus change of my EditText. However, when I long-touch on text in that EditText, another ActionMode menu opens with copy/paste/... buttons, which presumably is Android's default behaviour. How can I suppress this second menu, but still let the user select portions of the EditText's content?
Alternatively I would like to be able to customize the default menu using setCustomSelectionActionModeCallback(...) but using the Support library. How could I do that?

Unfortunately, TextView and EditText don't provide an API for you to use ActionBarCompat classes. But, you should be able to override this behavior.
Set a custom onLongClickListener in which you set your own state information in your activity and call supportInvalidateOptionsMenu(). Be sure to return 'true' from your OnLongClickListener.onLongClick() method, indicating you consumed the click. Then, in your onPrepareOptionsMenu you can add whatever new menu items you wish for your custom action bar. I would recommend not using TextView context menu items IDs 'selectAll', 'cut', 'copy' and 'paste', just to be on the safe side.

Related

How to show built-in horizontal edit menu on custom EditText view

Is it possible to easily show a simple menu, similar to this one in a screenshot from Chrome? I want a menu to show when there is a selection to manipulate to allow the user to Slect All, Cut, Copy, Paste.
Is this a standard control or something that should be easy to get working? What is the name of this control. This control is not showing automatically for our custom EditText view, which is fully functional in every other way.
Will I have to create my own custom control and display it in the correct location, manually? I have done similar work for an equivalent custom control in iOS, where the menu I want is called an Edit Menu, and it is trivial to show and hide that menu.

How to start contextual action bar for text view programmatically with default opions copy and select all?

Is there any way to programmatically start contextual action bar associated with a text view, on a button click? It should contain default options of copy/select all as well.
Basically, I want to show the selection handles in my text view and the android default copy/select all option in action bar, on a button click (instead of long click/double tap).
What I tried so far:
tried using the setCustomActionModeCallback() api, but problem here, is user need to long press/double click the text view for CAB to appear.
Tried using startActionMode() api, but could not find a way to retain default items.. it just opens a new empty CAB.. I know, I can add my custom copy-select all code and make use of this empty CAB, but I want to use the default Android provided one instead of managing it by myself.
Edit; I can't use EditText. The view need to be TextView only and long click will be disabled. I am doing all the above changes by setting TextView as selectable
Select your text manually and then perform a long click:
textView.post(new Runnable() {
#Override
public void run() {
Selection.selectAll((Spannable) tv.getText());
tv.performLongClick();
}
});
Edit:
The problem is that the built-in text selection CAB is private and uses multiple private classes. You'd need to copy a lot of code or access a bunch of methods with reflection to use it.
You could make your own CAB menu but you'd also need the controller handles which is again a lot of code.
A simple solution would be to live with what you have and use my above suggested method. If you want to avoid calling your long click listener you can remove it while calling the cab:
Selection.selectAll((Spannable) tv.getText());
tv.setOnLongClickListener(null);
tv.performLongClick();
tv.setOnLongClickListener(mLongClickListener);
if you want to select text in your text View than there are several approaches that you can follow.
first thing you can do, is set the xml properties of TextView.
android:textIsSelectable = "true"
txtView.setTextIsSelectable(true)
The textIsSelectable flag allows users to make selection gestures in the TextView, which in turn triggers the system's built-in copy/paste controls.
or
android:selectAllOnFocus = true;
setSelectAllOnFocus(boolean)
If the text is selectable, select it all when the view takes focus. and you can set the focus on click of your Button. using RequestFocus() method.
for more detail study you can reference to this Link. you will find all your Required task done.
http://developer.android.com/reference/android/widget/TextView.html[http://developer.android.com/reference/android/widget/TextView.html][1]
thank you

Default text in the serach bar with AutoCompleteTextView

I'm using the AutoCompleteTextView to allow users to search a from a list of countries. I want to have some default text preloaded in the search bar before the user clicks on it, such as "Search" or "Find Nearest Country". Is it possible to do this? If so how?
Right now, the app opens directly into the search box with the keyboard pulled up. Rather, I want the users to click on the bar before searching.
http://developer.android.com/reference/android/widget/AutoCompleteTextView.html
A TextView's hint seems to be exactly what you need. See android:hint for the XML attribute or setHint() for the corresponding Java method.
To prevent the keyboard from popping as soon as your activity is entered, you should set the android:windowSoftInputMode attribute in the activity declaration to stateHidden.

Disabling items in a Navigation Drawer in Android

I'm using the Navigation Drawer Example downloadable here.
I'd like to extend this example to disable the selection of one of the planets. For simplicity, lets say I want to permanently disable the selection of Saturn, and change the text of Saturn to dark gray, and have it not highlight when a user selects it. (in reality, I would like to disable navigation programmatically when a user has changed certain values on the screen not yet saved to the device).
The closest thing I've gotten to this is to stop the selectItem() method from being called from within the onItemClick click listener, but an issue remains even if I do this - the text of "Saturn" still appears selectable and highlights when a user clicks it.
What portion of the widgets do I need to change to prevent the text of Saturn from being highlighted?
I've tried changing
mDrawerLayout.setClickable(false);
mDrawerList.setClickable(false);
But neither of these options appear to have any affect.
Any suggestions or ideas as to how to approach this problem?
A couple helpful notes:
I'm aware that alternatively I can Set the Drawer Lock Mode to prevent the navigation drawer from being opened, but this is much less intuitive than having grayed out nav actions
Is the more appropriate thing to do here to remove the items from the list that cannot be accessed at this time? Similar to as described in the Remove ListView implementation here? The biggest disadvantage to this is that I would probably have to change the listview to describe why items have been removed from this list temporarily.
I'd like to extend this example to disable the selection of one of the planets
Create your own subclass of ArrayAdapter where you override areAllItemsEnabled() to return false and isEnabled() to return true or false as needed.

What is the best UI design for picking object icon in Android?

I have an Activity which provides UI for editing entity properties such as name, description an so on. This entity can also have an icon but (this is the main problem) can have no icon. I have created an icon picker activity which provides UI for icon selection. But I can not think of a simple and pretty way to provide access to this picker from parent activity. It could be a simple image button if the task was just to select an icon but I have also to make it possible to remove already set icon. Having two buttons with 'choose' and 'remove' seems ugly. Any smart ideas?
UPDATE
I've ended with ImageButton showing selected icon or special 'No' icon if object has no icon. On button click I show PopupMenu on 3.0+ and ContextMenu on older versions. Menu contains 'Change' and 'Remove' items.
You could have a border that surrounds an empty space or the image if there is one already.
If the image exists, clicking on it brings up a dialog where you can choose to change it or remove it.
If it doesn't exist, clicking on it brings up a dialog where you can add it.
how about a gridview within a custom dialog fired from your parent activity??
change the icon onItemSelected() of the gridview??

Categories

Resources