0

I have a few TextView and a few EditText from which I would like to select a substring of the data for copyingfor API level 10 and above.I have implemented an OnLongClickListener for the problem but it(understandably) throws an ArrayOutOfBoundsException:

   public boolean onLongClick(View v) {
    // TODO Auto-generated method stub
    //copy
    longPressedView=v;
    startSelection=((TextView)v).getSelectionStart();
    endSelection=((TextView)v).getSelectionEnd();
    Log.d(TAG, "Selection starts at "+startSelection+" and ends at "+endSelection);
    if(startSelection>endSelection)
    {
        startSelection=startSelection+endSelection;
        endSelection=startSelection-endSelection;
        startSelection=startSelection-endSelection;
        Log.d(TAG, "After interchanging positions selection starts at "+startSelection+" and ends at "+endSelection);
    }
    mSelectedText=((TextView)v).getText().toString().substring(startSelection, endSelection);
    mActionMode=startActionMode(actionModeCallback);
    return true;
}

I have thought upon implementing the OnTouchListener,but that would only return the x and y positions which would be of no use to me.

 java.lang.StringIndexOutOfBoundsException: length=93; regionStart=-1; regionLength=0
 at java.lang.String.startEndAndLength(String.java:583)
 at java.lang.String.substring(String.java:1464)
 at com.example.clipboardtest.MainActivity.onClick(MainActivity.java:139)
 at android.view.View.performClick(View.java:4240)
 at android.view.View$PerformClick.run(View.java:17721)
 at android.os.Handler.handleCallback(Handler.java:730)
 at android.os.Handler.dispatchMessage(Handler.java:92)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:5103)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:525)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 at dalvik.system.NativeStart.main(Native Method)
vamsiampolu
  • 6,328
  • 19
  • 82
  • 183
  • what is the `startselection` and `endselection` values which gets printed in your logs? And also what is the value of `mSelectedText`? – Kavin Nov 12 '13 at 09:05
  • Nothing,instead it throws a big fat StringIndexOutOfBoundsException,I was trying it with a TextView by the way. – vamsiampolu Nov 12 '13 at 09:13
  • first of all , when you long press the textview for the first time. startselection and endselection is -1. That is why ,that error is coming – Rahul Gupta Nov 12 '13 at 09:32
  • It is not selecting the text from the TextView. – vamsiampolu Nov 12 '13 at 09:37

2 Answers2

1

First of all add this property in textview in your xml : -

android:textIsSelectable="true"

Then use this code :-

    t = (TextView)findViewById(R.id.textView1);
    t.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {

            int start = t.getSelectionStart();
            int end = t.getSelectionEnd();
            if(start == -1 && end == -1){
                return true;
            }
            String mSelectedText=((TextView)v).getText().toString().substring(start, end);
            System.out.println(mSelectedText);

            return false;
        }
    });
Rahul Gupta
  • 5,275
  • 8
  • 35
  • 66
  • I have found that `android:textIsSelectable` is available only from API level 11 and above.So,I have started using `EditText` with `android:editable=false`.Now,I cannot select the text as the cursor is not visible even when I chose to use `android:cursorVisible=true` – vamsiampolu Nov 13 '13 at 05:01
  • So change api level to 11. What is the big deal in it – Rahul Gupta Nov 13 '13 at 05:25
  • The app shall be tested on API level 10 whatever happens...so no I cannot change it to API level 11.If I do it is going to be a big deal. – vamsiampolu Nov 13 '13 at 06:04
  • This feature is available from 11.Look into this :- http://stackoverflow.com/questions/6625300/selecting-text-on-textview-android-2-2 – Rahul Gupta Nov 13 '13 at 06:16
0

How about OnClickListener ??

text_field.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                //-------- Your code goes here
            }
        });
Vishnu
  • 159
  • 1
  • 3
  • 9