Android Text Selection Listener
Asked Answered
L

1

9

I'm trying to create a graceful, distraction-free text editor.
One of the features I really would like to implement is a pop-up with simple text formatting commands, such as bold, italic, underline, font color, and font face.

To give you an idea, here is a picture of the popup in iOS(Obviously, this is just the idea. I want to create a custom design for the popup):

enter image description here

I have a few questions:
1) Is there such thing as a text selection listener, or some equivalent?
2) Will I need to override the default android action for text selection?
3) How would I display/create this popup? (Vague question, I know...)

I am just starting out in Java and Android development, but I have already created a few apps and have gotten the hang of it.

Liquate answered 30/3, 2012 at 0:57 Comment(0)
W
13

The key term you're looking for here, to help you with your research, is ActionMode, provided that your target is honeycomb or newer.

The API docs (scroll down to "using the contextual action mode) do an ok job of explaining things, once you find what you're looking for, which is the biggest barrier to their use, but basically what you're going to need to do is this:

  1. set your EditText to be selectable (android:textIsSelectable="true" or setTextIsSelectable(true);
  2. Implement the ActionMode.Callback interface and provide your own menu items.

NOTE: as mentioned above, this only works for API level 11+. If you're targeting earlier platforms, getting the events for text selection is much more complicated.

Welbie answered 30/3, 2012 at 1:21 Comment(2)
I tried your solution but it didn't work. And the linked doc doesn't say anything about setTextIsSelectableWebfooted
@Webfooted sounds like you're doing something wrong, then - the docs describe essentially exactly what the OP asked for and it seems to have worked for 10 people so far. Maybe ask a new question with what you're trying to do and how it isn't working?Welbie

© 2022 - 2024 — McMap. All rights reserved.