2

could anyone explain, what i'm doing wrong?

in this case below, "tt" is my TexView. On my Oncreate() method, I have:

tt = (TextView) findViewById(R.id.ni);
tt.setText("This is a try");

and then

tt.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                String vi = tt.getText().toString().substring(tt.getSelectionStart(),tt.getSelectionEnd());
                Toast.makeText(getApplicationContext(), vi,Toast.LENGTH_LONG).show();

                }

        });

Nothing is Shown at Onclick.

thanks.

FpontoDesenv
  • 162
  • 1
  • 13
  • It looks like you're missing a little code - should be more like setOnClickListener(...) ? Anyway, try breaking the action down into a few more statement so it's easier to debug. Put start and stop into local variables. One more thing: you will get multiple onClicks - how do you know which one is the result of highlighting the selection? Instead of Toast, use the log file - easy way: System.out.println ("...stuff..."); – Peri Hartman Oct 25 '13 at 00:56
  • Thank you, @PeriHartman. I guess my problem is with the methods to getselection, because if I replace the methods for numbers, my toast works fine, shown the substring. – FpontoDesenv Oct 25 '13 at 02:53

1 Answers1

-1

I encountered the same problem a few days ago and I could find the following solution in the end. Delaying the process even 10 milliseconds could help interestingly. Put this in your onClick/onLongClick method, wherever you want to fetch the selected text.

For the original answer: textView.getSelectionEnd() returning start index value on Samsung Marshmallow 6.0 devices

I hope this helps those who have similar problem in the future.

new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {

                int startIndex = textView.getSelectionStart();
                int endIndex = textView.getSelectionEnd();

                if ((endIndex - startIndex) <= 0) {
                    return;
                }

                // UI related code here
            }
        }, TIME_IN_MS_TO_DELAY);
        return false;
U.Savas
  • 129
  • 11