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]);
}
These slick Android apps let you add more options to the text selection menu—unlock translation, definitions, and more actions.
While Google has made commendable efforts to supercharge text selection on Android with more contextually aware features, there’s still a lot you can add with third-party apps. Here are seven apps that make selecting text even better on Android.
1. Multi Copy
Say you’re looking to copy multiple excerpts from a lengthy page. Normally, you would have to grab each individually and paste them in a temporary file before moving on to the next. A free app called Multi Copy makes this process much less cumbersome.
As the name suggests, Multi Copy essentially allows you to copy several text portions from an article without having to paste them elsewhere. The app adds a new option called Multi Copy in Android’s default long-press contextual menu. Selecting that instead of the usual Copy option will let you easily grab all those snippets like you would do with a single chunk.
Multi Copy also offers a bunch of other tools relevant to its core objective. For instance, it shows a popup every time you choose the Multi Copy option that lets you start another clip or save the existing one in a new note. In addition, there’s a feature called Smart Copy that enables Multi Copy inside other apps like WhatsApp. With this, you can even copy messages from different chat windows and paste them as one.
2. Look Up
Another handy app for augmenting your phone’s text selection menu is Look Up. Mac users who regularly employ the OS’s built-in dictionary might be familiar with how this app functions.
Look Up’s pitch is quite straightforward. You select a word, tap its entry in the menu, and the word’s definition appears. But that’s not all. The free app also comes with the ability to bookmark these words and lets you add notes to them.
What’s more, if you hit the little menu button on the popup, there are a few quick actions for checking the word’s synonyms, dictating it, and even translating to another language (the latter requires the premium version). Thankfully, it works offline so you don’t need an internet connection to use all these features.
If you find Look Up too limited, try Merriam Webster’s great dictionary app for Android.
Download: Look Up (Free) | Look Up Pro ($1)
3. Text Infinity
Text Infinity, unlike the rest on this list, is not designed for just one purpose. The app brings a host of utilities to the text selection menu on Android. This includes dictation of the selected word, calling or texting a highlighted number, searching the text on YouTube or Google Maps, and translating to a different language.
Text Infinity thus adds five new options to your context menu. It would be nice if the developer allowed you to hide the ones you don’t use, though.
Download: Text Infinity (Free)
4. Clipboard Actions
If you find your text selection menu cluttered with all these new options, try Clipboard Actions. It’s nearly identical to Text Infinity, with one key difference. Clipboard Actions places all its options in the notification shade instead of the long-press list.
Once you’ve copied a piece of text, you can simply swipe down and find a bunch of nifty shortcuts to web search, share, translate, and more. The app also behaves as a clipboard manager, so you can revisit every one of your clippings later down the road. For more like this, check out how to improve the copy-paste functionality on Android.
Download: Clipboard Actions (Free)
5. Universal Copy
Universal Copy goes where no other text selection feature has gone before. The app makes it possible for you to copy text that you wouldn’t be able to otherwise. Once invoked for a particular page, Universal Copy scans the entire screen and lets you select any text without hassle. For instance, you normally can’t directly copy titles of YouTube videos in the app—but with Universal Copy, you can.
There’s even a Select All button when you need it. It’s similar to the old Now on Tap feature that scanned your screen to find relevant information, but instead of offering relevant web results, this app uses it for selecting text.
Download: Universal Copy (Free)
6. Copy to Read
As the name suggests, Copy to Read lets you simply select some text on a web page or anywhere else and have it read aloud. The app makes use of the Google Text-to-Speech engine, so ensure you have that installed and updated.
Too basic? Try the best text-to-speech Android apps instead.
Download: Copy to Read (Free)
7. Screen Translator
Similarly, Screen Translator is a single-purpose app for translating highlighted text. You can choose from a wide array of languages, including regional ones. In addition, once you’ve translated something, Screen Translator adds a bunch of other handy features in the popup.
You can enlarge the translation to show it someone in a foreign city, share it, or edit the input text manually. If you’re looking for a more capable translation app, though, you can use Google Translate in any Android app.
Download: Screen Translator (Free)
Android Text Selection’s Future
In addition to these third-party apps, text selection on Android is natively quite powerful as well. With a few recent updates, Google has made it contextual.
For instance, if you’re copying a location, the menu will have a Google Maps link. But for these features, you’ll have to be using the latest Android version. Unfortunately, this continues to remain a bottleneck for Android.
If you’re fed with your current update scenario, see what Android manufacturers are the best for timely updates.
What’s new in Android Marshmallow text selection?
In addition to the “top” new features in Android Marshmallow, there are many productivity and usability improvements in Android Marshmallow as well. One of the them is the improved Android Marshmallow text selection feature.
Text selection with finger on a touch screen is always tricky.
Android Marshmallow does not offer a perfect solution. But it makes text selection a lot easier than in previous Android versions.
You now also can get contextual assistance on the selected text in Android Marshmallow.
In Android Marshmallow, there are a couple of new features on text selections, including:
- Floating toolbar for section. The selection floating toolbar is a big improvements over that in Lollipop.
- Information (web search) on selected text. You can access relevant info on the selected text without leaving current app/window.
- Contextual assistance. You can access Google Now cards for selected text on the action menu. It is similar to Now on Tap.
- Translate selected text on-the-fly with Google Translate.
This guide will try to show you how to use these new features in Android Marshmallow text selection.
How to select, copy and paste text in Android Marshmallow?
Like in previous versions of Android, you can press and hold (aka long press) the text you want to select.
By default, a full word will be selected as shown below. You can then drag the handle to select more or less characters/words as shown below.
Once you finish the text selection, tap COPY as shown below to copy the text selected to clipboard.
To paste the entry (actually the last entry) in clipboard, tap and hold (long press) blank area in your text input filed.
A floating toolbar will pop out as shown below.
Tap PASTE as shown below to paste the entry into the location tapped.
How to use advanced features in Android Marshmallow text selection?
In addition to basic select, copy and paste as mentioned above, you have more options in Android Marshmallow text selection.
As shown below, in Android Marshmallow, whenever any text is selected, a floating section toolbar will pop out on the screen.
You can find that there are 3 sections once text is selected. They are marked as 1,2 and 3 in the screenshot below.
Web search for the selected text
In addition to the floating toolbar, if you are using Android Marshmallow text selection in a browser, you will also get the info bar in the bottom as shown below (marked as 1).
Tap this info bar will being you to the Google search result page for the selected text without leaving your current page as shown below.
For example, as Marshmallow is selected, the Google search results are for this term.
You can tap the close button (X) to close this pop out window without leaving your current web page/screen.
Actions for selection toolbar
As shown above, the action options (marked as 2 ) in the floating toolbar includes:
- Copy. If you tap this button, the selected text will be copied to the clipboard.
- Share. You can send the selected text to some apps. If you tap it, you will be prompted to select an app.
- Select all. This will select all text in the whole page (not just current screen). This is equivalent to drag the selection handles to both ends of the page.
After you tap Copy, the floating toolbar will be dismissed.
Additional Android Marshmallow text selection features are offered through the menu button (marked as 3 in the screenshot above).
Tap the menu button (3 vertical dots) in the floating toolbar to see the options.
By default, you can find:
- Web search. This is equivalent to tap the web search bar in the bottom as mentioned above. But this Web search option is available in all apps. The web search bar in the page bottom is only available for browsers.
- Assist. This will give you the contextual assistance on the selected text. It is very similar to Now on Tap. But the cards are now customized for the selected text, instead of current screen.
- Translate. You can get the selected text translated with Google Translate app.
- Back. You can close this menu option without dismissing the floating selection toolbar.
For example, if I tap Assist in the menu, I will get the Google Now cards tailored for the selected text and for me.
As mentioned, the cards are offered in the similar way as in Now on Tap.
Can you use Android Marshmallow text selection? Do you know how to use the new features, especially contextual assistance, in the text selection in Android Marshmallow?
If you have any questions or encounter any problems on Android Marshmallow text selection, please let us know in the comment box below.
The community will help you.
For any questions or problems with Android Marshmallow, please check our Android Marshmallow Guide page.
If your question is about Android Lollipop, please check out Android Lollipop Guide page.
For other Android guides, please check our Android 101 page.
on
March 16, 2021, 11:33 AM PDT
Android: All the things you can do with selected text and images
In Android, you can do more with selected text, links and images than just copy and paste them.
On your Android phone or tablet, you can select text, links and images in many types of apps, including your browser, email program, messaging app and news app. In many cases, you can use your selected text or image in different ways. Or course, you can always paste it elsewhere, but depending on the app, you can also share it with another person or service. You can run a search on the pasted item to define or get information on it. Selecting a link or image offers additional choices. Let’s look at all your options.
SEE: Top Android security tips (free PDF) (TechRepublic)
How to copy and paste in Android
First, open your browser and look for a page with text you want to copy. At the start of the text, tap and hold down to select the first word and then move your finger until you’ve selected all the text you need. You can adjust the handles at the start and end of the text to modify how much is selected.
After you release your finger, a menu pops up at the top of the selection with several choices. If you wish to select all the text on the current page, tap Select All. To copy the selection, tap the Copy command.
Move to the area or app where you want to paste the selection. Press down on the destination spot until a menu pops up. If the text has any hyperlinks or special formatting you wish to retain, tap the command for Paste, as that option will retain the formatting. If you want to paste the selection as plain text, tap the three-dot icon and select Paste As Plain Text. Your selected text is pasted (Figure A).
Figure A
How to share selected text in Android
Next, open an app that supports the copying of text, such as a news app. Select the text you want. From the top menu, tap the command for Share. The Android Share menu appears. You can now share the selected text through Messages, Gmail and other apps and services. Tap the specific app you want to use to share the text. The app opens with the text pasted (Figure B).
Figure B
How to search selected text in Android
Open another app with text that contains a word or term you’d like to research. Select the word or term and then tap the Web search command from the top menu. You’re taken to the default browser on your device where a search is automatically run to display results related to your selected text (Figure C).
Figure C
How to select links in Android
Next, let’s see what you can do with a selected link. Open a webpage in a browser, such as Chrome. Long press on a link on the page. From the popup window, you can open the link in a new tab, open it in an incognito tab, preview the page, copy the address for the link, copy the text of the link, download the linked page or share the link. Tap the command you want to run (Figure D).
Figure D
How to select images in Android
Finally, let’s try a selected image. Open a webpage that contains an image. Long press on the image. From the popup window, you can open the image in a new tab, preview the image, copy the image, download the image, search on the image using Google Lens and share the image (Figure E).
Figure E
Also See
-
Wi-Fi 6: A cheat sheet
(TechRepublic) -
Microsoft Surface Book 3: A cheat sheet (free PDF)
(TechRepublic) -
Android 12: What is rumored to be coming to Android’s next iteration
(TechRepublic) -
Hiring Kit: Application engineer
(TechRepublic Premium) -
The 10 best smartphones you can buy right now
(ZDNet) -
Android: More must-read coverage
(TechRepublic on Flipboard)
-
Android
-
Mobility
In this article, we are going to implement a very important feature related to TextView. While using any social media app or like using Facebook you may have seen there is a particular type of TextView which you cannot copy like a caption that people write on their posts. You can select the message but there few texts which cannot select or copy. So here we are going to learn how to implement that feature.
Implementing this feature for TextView
Method 1
Step 1: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:padding
=
"20dp"
android:gravity
=
"center"
android:orientation
=
"vertical"
tools:context
=
".SelectText"
>
<
TextView
android:id
=
"@+id/select"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Geeks For Geeks "
android:layout_alignParentTop
=
"true"
android:layout_centerHorizontal
=
"true"
android:textSize
=
"30dp"
/>
</
LinearLayout
>
Step 2: Working with the MainActivity.java file
This is something that enables our text to be get selected and then we can copy that text
select.setTextIsSelectable(true);
This disables our text to be get selected or even by default it is set as false. Hence you cannot select a text in default mode
select.setTextIsSelectable(false);
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file.
Java
import
androidx.appcompat.app.AppCompatActivity;
import
androidx.core.app.ActivityCompat;
import
androidx.core.content.ContextCompat;
import
android.Manifest;
import
android.content.pm.PackageManager;
import
android.os.Bundle;
import
android.telephony.SmsManager;
import
android.view.View;
import
android.widget.Button;
import
android.widget.CheckBox;
import
android.widget.EditText;
import
android.widget.TextView;
import
android.widget.Toast;
import
org.w3c.dom.Text;
public
class
SelectText
extends
AppCompatActivity {
TextView select;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_text);
select = findViewById(R.id.select);
select.setTextIsSelectable(
true
);
}
}
Method 2
Make Changes in the XML file. Add this line in your TextView.
android:textIsSelectable="true"
XML
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:padding
=
"20dp"
android:gravity
=
"center"
android:orientation
=
"vertical"
tools:context
=
".SelectText"
>
<
TextView
android:id
=
"@+id/select"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:textIsSelectable
=
"true"
android:text
=
"Geeks For Geeks "
android:layout_alignParentTop
=
"true"
android:layout_centerHorizontal
=
"true"
android:textSize
=
"30dp"
/>
</
LinearLayout
>
Output:
Implementing this feature for EditText
Step 1: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. We will create a simple EditText.
XML
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:padding
=
"20dp"
android:gravity
=
"center"
android:orientation
=
"vertical"
tools:context
=
".SelectText"
>
<
EditText
android:id
=
"@+id/selecte"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_alignParentTop
=
"true"
android:layout_centerHorizontal
=
"true"
android:cursorVisible
=
"false"
android:textSize
=
"30dp"
/>
</
LinearLayout
>
Step 2: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. In EditText By default, we can select text. Here firstly we will hide the cursor in the layout.xml file
android:cursorVisible="false"
And added an eventlistener for long click and display the cursor only when a selection starts.
selecte.setCursorVisible(true);
Java
import
androidx.appcompat.app.AppCompatActivity;
import
androidx.core.app.ActivityCompat;
import
androidx.core.content.ContextCompat;
import
android.Manifest;
import
android.content.pm.PackageManager;
import
android.os.Bundle;
import
android.telephony.SmsManager;
import
android.view.View;
import
android.widget.Button;
import
android.widget.CheckBox;
import
android.widget.EditText;
import
android.widget.TextView;
import
android.widget.Toast;
import
org.w3c.dom.Text;
public
class
SelectText
extends
AppCompatActivity {
EditText selecte;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_text);
selecte = findViewById(R.id.selecte);
selecte.setOnLongClickListener(
new
View.OnLongClickListener() {
@Override
public
boolean
onLongClick(View v) {
selecte.setCursorVisible(
true
);
Toast.makeText(SelectText.
this
,
"Visible"
,Toast.LENGTH_LONG).show();
return
false
;
}
});
}
}
Output: