Get Selected Text from Textview

Get Selected Text from TextView

I think what you're looking for is TextView.setCustomSelectionActionModeCallback. This will allow you to create your own ActionMode.Callback for when the text is selected. Then you can use TextView.getSelectionStart and TextView.getSelectionEnd to retrieve the selected text when your MenuItem is selected. Here's a quick example:

    mTextView.setCustomSelectionActionModeCallback(new Callback() {

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Remove the "select all" option
menu.removeItem(android.R.id.selectAll);
// Remove the "cut" option
menu.removeItem(android.R.id.cut);
// Remove the "copy all" option
menu.removeItem(android.R.id.copy);
return true;
}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Called when action mode is first created. The menu supplied
// will be used to generate action buttons for the action mode

// Here is an example MenuItem
menu.add(0, DEFINITION, 0, "Definition").setIcon(R.drawable.ic_action_book);
return true;
}

@Override
public void onDestroyActionMode(ActionMode mode) {
// Called when an action mode is about to be exited and
// destroyed
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case DEFINITION:
int min = 0;
int max = mTextView.getText().length();
if (mTextView.isFocused()) {
final int selStart = mTextView.getSelectionStart();
final int selEnd = mTextView.getSelectionEnd();

min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
}
// Perform your definition lookup with the selected text
final CharSequence selectedText = mTextView.getText().subSequence(min, max);
// Finish and close the ActionMode
mode.finish();
return true;
default:
break;
}
return false;
}

});

Results

Example

Android Get selected text in String

Ok here is the quick example where you select the text and using a Toast you show it to the screen :

MainActivity

public class MainActivity extends AppCompatActivity {
private static final int TRANSLATE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView mTextView = (TextView) findViewById(R.id.txt);

mTextView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
menu.add(0,TRANSLATE,0,"Translate").setIcon(R.drawable.ic_translate); //choose any icon
// Remove the other options
menu.removeItem(android.R.id.selectAll);
menu.removeItem(android.R.id.cut);
menu.removeItem(android.R.id.copy);
return true;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()){
case TRANSLATE:
int min = 0;
int max = mTextView.getText().length();
if (mTextView.isFocused()) {
final int selStart = mTextView.getSelectionStart();
final int selEnd = mTextView.getSelectionEnd();

min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
}

final CharSequence selectedText = mTextView.getText().subSequence(min, max); //this is your desired string
Toast.makeText(getApplicationContext(),selectedText,Toast.LENGTH_SHORT).show();

//Here put your code for translation

mode.finish();
}
return false;
}

@Override
public void onDestroyActionMode(ActionMode mode) {

}
});
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView
android:textIsSelectable="true"
android:id="@+id/txt"
android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />

How can we get the whole text from selected text in UITextView?

You have to set your view controller as the text view delegate UITextViewDelegate

override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
}

Implement textViewDidChangeSelection method

func textViewDidChangeSelection(_ textView: UITextView)

In this function you can get the UITextPosition of the start and end of the lines of the selection, then create a new range and retrieve the selected text:

func textViewDidChangeSelection(_ textView: UITextView) {
guard let selectedTextRange = textView.selectedTextRange,
let start = textView.tokenizer
.position(from: selectedTextRange.start, toBoundary: .line, inDirection: .backward),
let end = textView.tokenizer
.position(from: selectedTextRange.end, toBoundary: .line, inDirection: .forward),
let range = textView.textRange(from: start, to: end),
let text = textView.text(in: range)
else { return }
print(text)
}

Add this extension helpers to your project:

extension UITextDirection {
static let forward: Self = .init(rawValue: 0)
static let backward: Self = .init(rawValue: 1)
}

How to get selected text from edittext in android?

EditText et=(EditText)findViewById(R.id.edit);

int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();

String selectedText = et.getText().toString().substring(startSelection, endSelection);

Get selected text in a UITextView

What is happening is that UITextView method textInRange have been renamed to text(in: Range) since Swift 3. Btw you forgot to add the let keyword in your sentence:

if let range = textView.selectedTextRange {
UIPasteboard.general.string = textView.text(in: range)
}


Related Topics



Leave a reply



Submit