Reputation: 1218
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
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
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
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
TextView
;constructor
and return it in the get
method. This would be just to avoid creating new instances on every method call.Upvotes: 0