How to start contextual action bar for text view programmatically with default opions copy and select all?
Asked Answered
Y

2

9

Is there any way to programmatically start contextual action bar associated with a text view, on a button click? It should contain default options of copy/select all as well. Basically, I want to show the selection handles in my text view and the android default copy/select all option in action bar, on a button click (instead of long click/double tap).

What I tried so far: tried using the setCustomActionModeCallback() api, but problem here, is user need to long press/double click the text view for CAB to appear. Tried using startActionMode() api, but could not find a way to retain default items.. it just opens a new empty CAB.. I know, I can add my custom copy-select all code and make use of this empty CAB, but I want to use the default Android provided one instead of managing it by myself.

Edit; I can't use EditText. The view need to be TextView only and long click will be disabled. I am doing all the above changes by setting TextView as selectable

Yourself answered 21/5, 2015 at 11:28 Comment(2)
you want this in TextView or Edittext.Bibliofilm
When you set the text as selectable, long click will be enabled implicitly.Arnoldarnoldo
A
3

Select your text manually and then perform a long click:

textView.post(new Runnable() {
    @Override
    public void run() {
        Selection.selectAll((Spannable) tv.getText());
        tv.performLongClick();
    }
});

Edit:

The problem is that the built-in text selection CAB is private and uses multiple private classes. You'd need to copy a lot of code or access a bunch of methods with reflection to use it.

You could make your own CAB menu but you'd also need the controller handles which is again a lot of code.

A simple solution would be to live with what you have and use my above suggested method. If you want to avoid calling your long click listener you can remove it while calling the cab:

Selection.selectAll((Spannable) tv.getText());
tv.setOnLongClickListener(null);
tv.performLongClick();
tv.setOnLongClickListener(mLongClickListener);
Arnoldarnoldo answered 27/5, 2015 at 14:33 Comment(5)
As i said, i wont be able to use long click, as it is overridden and is used to show a menu.Yourself
@noone updated my answer to give you a bit more details on what's going on here. Also a solution to an existing OnLongClickListener.Arnoldarnoldo
This wont suits my use case, but I think this is the only way to go other than writing and managing cab lifecycle by myself. Hence marking as correct answer-Yourself
Does this still work? The text gets selected but the action menu is never shown. Also, set the text with TextView.BufferType.SPANNABLE as second parameter to escape the ClassCastException for casting Spannable.Food
*floating action menu. trying it out on Android PieFood
M
1

if you want to select text in your text View than there are several approaches that you can follow. first thing you can do, is set the xml properties of TextView.

  android:textIsSelectable = "true"

 txtView.setTextIsSelectable(true)

The textIsSelectable flag allows users to make selection gestures in the TextView, which in turn triggers the system's built-in copy/paste controls.

or

android:selectAllOnFocus = true;
setSelectAllOnFocus(boolean)

If the text is selectable, select it all when the view takes focus. and you can set the focus on click of your Button. using RequestFocus() method.

for more detail study you can reference to this Link. you will find all your Required task done. http://developer.android.com/reference/android/widget/TextView.html[http://developer.android.com/reference/android/widget/TextView.html][1]

thank you

Miyokomizar answered 1/6, 2015 at 7:13 Comment(4)
i'm sorry i did not get your answer either. :)Yourself
the above answer help you to select or copy text from edittextMiyokomizar
Please read question. Its TextView, not edittext and my use case I have clearly mentioned which is not the one you have addressedYourself
okay Noone I updated my answer see if it can help you out If not we go for some other alternativesMiyokomizar

© 2022 - 2024 — McMap. All rights reserved.