1

I'm trying to make simple clickable for part of string with this code, for example:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout salam = (LinearLayout) findViewById(R.id.salam);
        TextView     textView    = new TextView(this);

        textView.setHighlightColor(Color.RED);

        String text = "Android is a Software stack";

        SpannableString ss = new SpannableString(text);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                Toast.makeText(MainActivity.this, "asdasdasd", Toast.LENGTH_SHORT);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
            }
        };
        ss.setSpan(clickableSpan, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        textView.setText(ss);

        salam.addView(textView);
    }

but it doesn't work and i can't click on text to show simple Toast

tux-world
  • 2,680
  • 6
  • 24
  • 55

0 Answers0