Make specific substring of SpannableString editable

540 views Asked by At

Im trying to make certain substrings of a SpannableString editable.

Im able to identify and make clickable any substring that matches my criteria using matcher.find() and ClickableSpan()

Im also able to make the substring editable using

myTxtView.setFocusable(true);
myTxtView.setEnabled(true);
myTxtView.setClickable(true);
myTxtView.setFocusableInTouchMode(true);
myTxtView.setCursorVisible(true);
myTxtView.setInputType(InputType.TYPE_CLASS_TEXT);
myTxtView.requestFocus();

enter image description here

The problem is the entire textview becomes editable, not just the substrings that where matched.

Here is the code

final SpannableString hashText = new SpannableString(myTxtView.getText().toString());

while (matcher2.find()) {

  //find and match strings

  hashText.setSpan(new ClickableSpan() {

    @Override
    public void onClick(View widget) {

    //do something on click       

    }

    @Override
    public void updateDrawState(TextPaint ds) {
      super.updateDrawState(ds);
      ds.setColor(Color.parseColor("#A8000000"));
      ds.setUnderlineText(true);

    }

  },matcher2.start(),matcher2.end(),1);

}
  myTxtView.setText(hashText);
  myTxtVIew.setMovementMethod(LinkMovementMethod.getInstance());
  myTxtView.setFocusable(true);
  myTxtView.setEnabled(true);
  myTxtView.setClickable(true);
  myTxtView.setFocusableInTouchMode(true);
  myTxtView.setCursorVisible(true);
  myTxtView.setInputType(InputType.TYPE_CLASS_TEXT);
  myTxtView.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

I've played around a bit with Editable.Factory() with no success. Not sure how or if that is applicable to my case.

1

There are 1 answers

1
Capsule On

You need to use ClickableSpan to perform the things you want, here is some codeSnippet which will help you

SpannableString spanString = new SpannableString(text);
    Matcher matcher = Pattern.compile("@([A-Za-z0-9_-]+)").matcher(spanString);

    while (matcher.find())
    {
        spanString.setSpan(new ForegroundColorSpan(Color.parseColor("#0000FF")), matcher.start(), matcher.end(), 0); //to highlight word havgin '@'
        final String tag = matcher.group(0);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                Log.e("click", "click " + tag);
                String searchText=tag.replace("@",""); //replace '@' with blank character to search on google.
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.co.in/search?q=" + searchText));
                startActivity(browserIntent);
            }
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);

            }
        };
        spanString.setSpan(clickableSpan, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

take a reference from androidgig.com

Output