vrbsm
vrbsm

Reputation: 1218

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());
...

Upvotes: 1

Views: 5108

Answers (3)

Prateek Yadav
Prateek Yadav

Reputation: 942

one hack could be to hide the action mode asap like

 @Override
    public void onActionModeStarted(ActionMode mode) {
        super.onActionModeStarted(mode);
        mode.finish();
    }

Upvotes: 0

vrbsm
vrbsm

Reputation: 1218

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

Upvotes: 1

vinitius
vinitius

Reputation: 3274

Try this:

 public class CustomActionModeTextView extends TextView{

       //...implement your constructors as you may want to use...//
       @Override
       public ActionMode.Callback getCustomSelectionActionModeCallback (){
          return new MarkTextSelectionActionModeCallback();
        }


 }

Create your own TextView and override the method getCustomSelectionActioModeCallback to return always an instance of your custom action mode callback. This way you don't have to set it everytime in your views.

Remember

  • Use your custom class in your XML layout files;
  • When casting it, do the cast to your custom class and not TextView;
  • You may wanna consider keeping a reference to your custom callback in your class. You could initialize it during some constructor and return it in the get method. This would be just to avoid creating new instances on every method call.

Upvotes: 0

Related Questions