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.

Penguin
  • 21
  • 1
  • 4

2 Answers2

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

mariuss
  • 1,177
  • 2
  • 14
  • 30
  • Thank you so much Lazai. I really appreciating to your knowledge :). This code helps me perfectly which I want it. – Penguin Sep 29 '15 at 03:25
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.

Community
  • 1
  • 1
mariuss
  • 1,177
  • 2
  • 14
  • 30
  • 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... – Penguin Sep 28 '15 at 07:10
  • 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 – mariuss Sep 28 '15 at 15:02
  • 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 .... – Penguin Sep 28 '15 at 15:05
  • Just only you told me code that how can I return selected text . Actually I am stuck on that concept please ... – Penguin Sep 28 '15 at 15:12