0

friends I'm new in Android programming and I want to learn that how we can select text and return selected text into String.

2
  • Explain your Question,select text means ? from where? any sample piece of code u tried? Commented Sep 27, 2015 at 15:20
  • Actually brother I just want define those selected text but I don't know that how to do Commented Sep 27, 2015 at 15:23

2 Answers 2

2

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" />

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much Lazai. I really appreciating to your knowledge :). This code helps me perfectly which I want it.
0

If you want to select text from an EditText you can use this:

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

Where :

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

However if you have a TextView you need to create your own ActionMode.Callback when you select your text. To view a quick example go to this post.

4 Comments

Hello Lazai , actually dear I just want to develop an app where user can select word and I want define option to translate that word ... Please help me to making this app...
Well when you select that text , let's assume it's a word or even a phrase , using the ActionMode.Callback you have certain menu icons ; each of them representing a functionality ; All you need to do is in onCreateActionMode() -> define a new menu icon (functionality) let's say translation : menu.add(0,TRANSLATION,0,"Translation").setIcon(R.drawable.ic_translate); After that in onActionItemClicked() -> you wrote your code there to translate that string and return it , whatever you want to do with it
Dear Lazai, Actually I tried some code but actually can't able to do that please can you give me some code that it will help me to do that please .... 😢
Just only you told me code that how can I return selected text . Actually I am stuck on that concept please ... 😊

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.