androidtextviewcustom-controls

TextView - setCustomSelectionActionModeCallback how create ActionMode.Callback for when the text is selected to many TextView


I have several TextView in my systems and most of them I have to call my custom ActionMode.Callback.

The question is how do I create a TextView with custom ActionMode.Callback?

today my code is that way

mTxOne.setCustomSelectionActionModeCallback(new MarkTextSelectionActionModeCallback());
 mTxTwo.setCustomSelectionActionModeCallback(new MarkTextSelectionActionModeCallback());
...

Solution

  • public class TextViewA extends TextView {
    
        @Override
        protected void onCreateContextMenu(ContextMenu menu) {
            super.onCreateContextMenu(menu);
            setCustomSelectionActionModeCallback(new MarkTextSelectionActionModeCallback());
        }
    
    
    
    public TextViewA(Context context) {
        super(context);
    }
    public TextViewA(Context context,AttributeSet attrs) {
        super(context,attrs);
    }
    public TextViewA(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
    }
    

    Here the xml

     <RelativeLayout      xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
      <br.com.vrbsm.textviewexample.TextViewA
          android:id="@+id/textView2" 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textIsSelectable="true"
          android:textSize="12dp"
          />
    </RelativeLayout>
    

    here main

    public class MainActivity extends Activity{
    //  
        private TextViewA textview;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_activity);
    
            textview = (TextViewA)findViewById(R.id.textView2);
            textview.setText("Android is crazy");
    
        }
    
    
    public class MarkTextSelectionActionModeCallback implements Callback {
    .
    .
    .}