1

Good afternoon. Could you help me with a clickable span? The fact is that when I assign a clickable substring at the end of an origin String, not only the text becomes clickable, but also the entire TextView area to the right of the substring, which is bad for me (it is necessary that only a substring is clickable. Can you advise me something ? match parent for my textview is required. Unfortunately, programmatically adding " " to the original string is also unacceptable to me. Pastebin link with code: here. I created a video on my Dropbox:

bad work my programm: dropbox

I want it to work like this dropbox

My TextView needs the following layout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = findViewById(R.id.text_view);
    SpannableString spanable = new SpannableString("my clickable span");

    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            Toast.makeText(MainActivity.this, "click span", Toast.LENGTH_SHORT).show();
        }
    };

    spanable.setSpan(clickableSpan, 13, 17, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

    textView.setText(spanable);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
}

1 Answers1

0

set all text using length.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = findViewById(R.id.text_view);

    String strService = "my clickable span";

    int serviceStart = strService.indexOf("my clickable span");
    int serviceEnd = serviceStart + "my clickable span".length();

    SpannableString spanable = new SpannableString(strService);

    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            Toast.makeText(MainActivity.this, "click span", Toast.LENGTH_SHORT).show();
        }
    };

    spanable.setSpan(clickableSpan, serviceStart, serviceEnd, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

    textView.setText(spanable);
    textView.setClickable(true);  //add clickable
    textView.setMovementMethod(LinkMovementMethod.getInstance());
}
S T
  • 1,068
  • 2
  • 8
  • 16