How to configure selected text listener in TextView

I am using ActionMode.Callback, but I need to know when the text was selected ... for example

enter image description here

+7
source share
3 answers

I think you can find your answer here:

Android text listener

The key term you are looking for here to help you with your research is ActionMode , provided your goal is cellular or newer.

API docs (scroll down to "using contextual action mode) do a good job of clarifying things as soon as you find what you are looking for, which is the biggest obstacle to using them, but basically what you will need to do the following:

  • set your EditText to select ( android:textIsSelectable="true" or setTextIsSelectable(true);
  • Implement the ActionMode.Callback interface and specify your own menu items.

NOTE. As mentioned above, this only works for API level 11+. If you focus on earlier platforms, getting events for the text is much more difficult to select.

+2
source

in .xml:

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textIsSelectable="true" /> 

in .class:

 textview.setCustomSelectionActionModeCallback(new callback(textview)); ... public class callback implements Callback { private TextView mTextView; public callback(TextView text) { this.mTextView = text; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { int start = mTextView.getSelectionStart(); int end = mTextView.getSelectionEnd(); Spannable wordtoSpan = (Spannable) mTextView.getText(); switch (item.getItemId()) { case R.id.item_blue: wordtoSpan.setSpan(new BackgroundColorSpan(Color.BLUE), start , end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.BLUE); return true; case R.id.item_green: wordtoSpan.setSpan(new BackgroundColorSpan(Color.GREEN), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.GREEN); return true; case R.id.item_red: wordtoSpan.setSpan(new BackgroundColorSpan(Color.RED), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.RED); return true; case R.id.item_yellow: wordtoSpan.setSpan(new BackgroundColorSpan(Color.YELLOW), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.YELLOW); return true; case R.id.item_erase: wordtoSpan.setSpan(new BackgroundColorSpan(Color.TRANSPARENT), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); singlenton.getInstance().getDatabase().createMarkText(mTextView,Color.TRANSPARENT); return true; } return false; } @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { mode.setTitle("Selecione a cor"); mode.getMenuInflater().inflate(R.menu.menu_text_context, menu); return true; } @Override public void onDestroyActionMode(ActionMode mode) { } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { menu.removeItem(android.R.id.selectAll); // Remove the "cut" option menu.removeItem(android.R.id.cut); // Remove the "copy all" option menu.removeItem(android.R.id.copy); return true; } } 
+1
source

If you need a listener without an ActionMode:

 import android.content.Context import android.util.AttributeSet import android.widget.TextView class TextViewWithObservableSelection @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0 ) : TextView( context, attrs, defStyleAttr, defStyleRes ) { private var selectedText: String = "" set(value) { if (field != value) { field = value observer?.invoke(value) } } private var observer: ((String) -> Unit)? = null set(value) { field = value field?.invoke(selectedText) } override fun onSelectionChanged(selStart: Int, selEnd: Int) { super.onSelectionChanged(selStart, selEnd) val startIndex = minOf(selStart, selEnd) val endIndex = maxOf(selStart, selEnd) selectedText = text.toString().substring(startIndex, endIndex) } fun observeSelectedText(observer: ((String) -> Unit)?) { this.observer = observer } } 
0
source

All Articles