Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8735195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:59:48+00:00 2026-06-13T09:59:48+00:00

I have an app where I want to be able to show a TextView

  • 0

I have an app where I want to be able to show a TextView (or EditText) that allows the user to select some text, then press a button to have something done with that text. Implementing this on Android versions prior to Honeycomb is no problem but on Honeycomb and above the default long-press action is to show an action bar with Copy/Cut/Paste options. I can intercept long-press to show my own action bar, but then I do not get the text selection handles displayed.

Once I have started my own ActionMode how do I get the text selection handles displayed?

Here is the code I’m using to start the ActionMode, which works except there are no text selection handles displayed:

public boolean onLongClick(View v) {
    if(actionMode == null)
        actionMode = startActionMode(new QuoteCallback());
    return true;
}

class QuoteCallback implements ActionMode.Callback {

    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.quote, menu);
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch(item.getItemId()) {

        case R.id.quote:
            Log.d(TAG, "Selected menu");
            mode.finish();
            // here is where I would grab the selected text
            return true;
        }
        return false;
    }

    public void onDestroyActionMode(ActionMode mode) {
        actionMode = null;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:59:49+00:00Added an answer on June 13, 2026 at 9:59 am

    I figured out the answer to my own question; TextView (and therefore EditText) has a method setCustomSelectionActionModeCallback() which should be used instead of startActionMode(). Using this enables customisation of the menu used by TextView for text selection. Sample code:

    bodyView.setCustomSelectionActionModeCallback(new StyleCallback());
    

    where StyleCallback customises the text selection menu by removing Select All and adding some styling actions:

    class StyleCallback implements ActionMode.Callback {
    
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            Log.d(TAG, "onCreateActionMode");
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.style, menu);
            menu.removeItem(android.R.id.selectAll);
            return true;
        }
    
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }
    
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            Log.d(TAG, String.format("onActionItemClicked item=%s/%d", item.toString(), item.getItemId()));
            CharacterStyle cs;
            int start = bodyView.getSelectionStart();
            int end = bodyView.getSelectionEnd();
            SpannableStringBuilder ssb = new SpannableStringBuilder(bodyView.getText());
    
            switch(item.getItemId()) {
    
            case R.id.bold:
                cs = new StyleSpan(Typeface.BOLD);
                ssb.setSpan(cs, start, end, 1);
                bodyView.setText(ssb);
                return true;
    
            case R.id.italic:
                cs = new StyleSpan(Typeface.ITALIC);
                ssb.setSpan(cs, start, end, 1);
                bodyView.setText(ssb);
                return true;
    
            case R.id.underline:
                cs = new UnderlineSpan();
                ssb.setSpan(cs, start, end, 1);
                bodyView.setText(ssb);
                return true;
            }
            return false;
        }
    
        public void onDestroyActionMode(ActionMode mode) {
        }
    }
    

    The XML for the menu additions is:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/italic"
              android:showAsAction="always"
              android:icon="@drawable/italic"
              android:title="Italic"/>
        <item android:id="@+id/bold"
              android:showAsAction="always"
              android:icon="@drawable/bold"
              android:title="Bold"/>
        <item android:id="@+id/underline"
              android:showAsAction="always"
              android:icon="@drawable/underline"
              android:title="Underline"/>
    </menu>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a GridView in my WinRT app that I want to be able
In my app, I want to be able to have the user enter their
I have an app that I want to be able to build two different
I have an app where we want users to be able to send us
I am building an android app, and I want to be able to have
I have an iphone app i want that befor moving to next screen it
I have an app that I want to be launchable either explicitly when tapped
I have an App that I want to have two version in the App
I have an app that I want to develop for Android 2.1, 2.2, 2.3.3,
I have app in which i have recorded sound files i want that i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.