I have a TextView
that I'm looking to allow the user to search in for a specific string. If the string is found, it should highlight. Using a background span is too slow and awkward, so I am trying to figure out if I can just have it select the string. I know with EditText
this would be possible using setSelection()
, but I don't want the user to be able to edit the text, while still being able to manually highlight text, which I can't seem to do manage with an EditText
.
I guess, then it's an either or; is it either possible to programmatically select text in a TextView
or possible to allow text selection without allowing editing in an EditText
?
Note: I'm actually using a custom view extending TextView
, so I'm assuming it's either that or extend EditText
; I'm just not sure which (if either) will work.
Select + copy text in a TextView? Generally, we can copy / paste the value from EditText by long click. It is an in-build functionality for Android. Now, let's say you have the same requirement in TextView .
EditText is used for user input. TextView is used to display text and is not editable by the user. TextView can be updated programatically at any time.
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.
You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!
Not sure whether the question is still actual, I will provide my solution. Maybe will be useful for people coming from search engines.
So the purpose, as I understood, is to select all text in TextView
without being able to modify its content. I didn't check how effective it is against very large text, but hope that not so bad.
Please note, API version should be >=11
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.Selection;
import android.text.Spannable;
import android.util.AttributeSet;
public class SelectableTextView extends TextView
{
public SelectableTextView(Context context)
{
super(context);
init();
}
public SelectableTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public SelectableTextView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public SelectableTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init()
{
if (Build.VERSION.SDK_INT > 10)
setTextIsSelectable(true);
}
@Override
public boolean onTextContextMenuItem(int id)
{
switch (id)
{
case android.R.id.cut:
return true;
case android.R.id.paste:
return true;
case android.R.id.shareText:
{
String selectedText = getText().toString().substring(getSelectionStart(), getSelectionEnd());
if (selectedText != null && !selectedText.isEmpty())
{
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, selectedText);
sendIntent.setType("text/plain");
sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(sendIntent);
}
return true;
}
case android.R.id.selectAll:
{
selectAllText();
return true;
}
}
return super.onTextContextMenuItem(id);
}
public void selectAllText()
{
if (Build.VERSION.SDK_INT > 10)
Selection.setSelection((Spannable) getText(), 0, length());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With