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

  • Home
  • SEARCH
  • 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 6982701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:19:24+00:00 2026-05-27T18:19:24+00:00

How to select a word on a tap in TextView / EditText in android.

  • 0

How to select a word on a tap in TextView/EditText in android. I have some text in a TextView/EditText and when user taps on a word, I want that word to be selected and after that when I call getSelectedText() like method, it should return me the selected word.
Any Help would be appreciated.

My goal is to perform some action when user taps on a particular word in TextView/EditText.

  • 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-05-27T18:19:25+00:00Added an answer on May 27, 2026 at 6:19 pm

    UPDATE: Another better approach is to use BreakIterator:

    private void init() {
        String definition = "Clickable words in text view ".trim();
        TextView definitionView = (TextView) findViewById(R.id.text);
        definitionView.setMovementMethod(LinkMovementMethod.getInstance());
        definitionView.setText(definition, BufferType.SPANNABLE);
        Spannable spans = (Spannable) definitionView.getText();
        BreakIterator iterator = BreakIterator.getWordInstance(Locale.US);
        iterator.setText(definition);
        int start = iterator.first();
        for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator
                .next()) {
            String possibleWord = definition.substring(start, end);
            if (Character.isLetterOrDigit(possibleWord.charAt(0))) {
                ClickableSpan clickSpan = getClickableSpan(possibleWord);
                spans.setSpan(clickSpan, start, end,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
    
    private ClickableSpan getClickableSpan(final String word) {
        return new ClickableSpan() {
            final String mWord;
            {
                mWord = word;
            }
    
            @Override
            public void onClick(View widget) {
                Log.d("tapped on:", mWord);
                Toast.makeText(widget.getContext(), mWord, Toast.LENGTH_SHORT)
                        .show();
            }
    
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
            }
        };
    }
    

    OLD ANSWER

    I wanted to handle click in my own Activity. I solved it by following code:

    private void init(){
        String definition = "Clickable words in text view ".trim();
        TextView definitionView = (TextView) findViewById(R.id.definition);
        definitionView.setMovementMethod(LinkMovementMethod.getInstance());
        definitionView.setText(definition, BufferType.SPANNABLE);
    
        Spannable spans = (Spannable) definitionView.getText();
        Integer[] indices = getIndices(
                definitionView.getText().toString(), ' ');
        int start = 0;
        int end = 0;
          // to cater last/only word loop will run equal to the length of indices.length
        for (int i = 0; i <= indices.length; i++) {
            ClickableSpan clickSpan = getClickableSpan();
           // to cater last/only word
            end = (i < indices.length ? indices[i] : spans.length());
            spans.setSpan(clickSpan, start, end,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            start = end + 1;
        }
    }
    private ClickableSpan getClickableSpan(){
         return new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    TextView tv = (TextView) widget;
                    String s = tv
                            .getText()
                            .subSequence(tv.getSelectionStart(),
                                    tv.getSelectionEnd()).toString();
                    Log.d("tapped on:", s);
                }
    
                public void updateDrawState(TextPaint ds) {
                     super.updateDrawState(ds);
                }
            };
    }
    public static Integer[] getIndices(String s, char c) {
        int pos = s.indexOf(c, 0);
        List<Integer> indices = new ArrayList<Integer>();
        while (pos != -1) {
            indices.add(pos);
            pos = s.indexOf(c, pos + 1);
        }
        return (Integer[]) indices.toArray(new Integer[0]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Almost any mature program that involves text implements double click to select the word
I want to select every word except the word: 'Lorem'. I have tried: ^((?!Lorem).*)$
Notepad++ has a convenient feature: if you select a word in your text (not
SELECT GETDATE() Returns: 2008-09-22 15:24:13.790 I want that date part without the time part:
I have a sqlite3 query like: SELECT word FROM table WHERE word NOT LIKE
I have an method and it converts pdf text into a list. After the
hi i have problem with my first addons.. i try to select the word
I have reference the answer here Select whole word with getSelection . I would
I'm running a grep to find any *.sql file that has the word select
I have a dropdown list of items a user can select from (the view

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.