0

I want all ClickableSpans to have toasts show their own text. But whatever I write in Toast in onClick in for loop, all ClickableSpans show same Toast output. Briefly, I just want ClickableSpans to show different Toasts in for loop. How can I do it? I want all words have clickable. And when all words are clicked, they do different thing. Thank you for your help.

public class MainActivity extends AppCompatActivity {
private String[] textArray;
    private String text = "1981 senesinde kuantum bilgisayar fikrini Paul Beniof, Max Planck’ın " +
            "enerjinin sürekli olmadan kesikli değerlerde yer alan m, n, k enerji kuantlarıyla ";

    private Chronometer chronometer;

    private int hold=0;
    private TextView textView;
    private int i;

    private MediaPlayer player;
    private long pauseOffset;
    private boolean running;
    private boolean start=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textArray = text.split(" ");
        final SpannableString ss = new SpannableString(text);
        chronometer = findViewById(R.id.chronometer);
        chronometer.setFormat("Time: %s");
        chronometer.setBase(SystemClock.elapsedRealtime());

        textView = findViewById(R.id.text_view);
        textView.setText(text);
        textView.setTextColor(Color.BLACK);

        final ClickableSpan[] clickableSpans = new ClickableSpan[textArray.length];
        for(i = 0; i<textArray.length; i++){

            clickableSpans[i] = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    Toast.makeText(MainActivity.this, "", Toast.LENGTH_LONG).show();
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setColor(Color.BLACK);
                    ds.setUnderlineText(false);
                }


            };

            final boolean[] c = {true};

            chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
                @Override
                public void onChronometerTick(Chronometer chronometer) {

                    if ((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 5000&& c[0]) {
                        for(int i=0; i<textArray.length;i++){
                            ss.setSpan(clickableSpans[i], hold, hold + textArray[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            hold+=textArray[i].length();
                            hold++;
                        }
                        start=false;
                        hold=0;
                        textView.setText(ss);
                        textView.setMovementMethod(LinkMovementMethod.getInstance());
                        if (running) {
                            chronometer.stop();
                            pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
                            running = false;
                        }
                        c[0]=false;
                    }
                }
            });
            hold=0;
        }
        i=0;

    }

    public void play(View v) {
        if(start){
            if (!running) {
                chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
                chronometer.start();
                running = true;
            }
        }
    }

    public void stop(View v) {
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}
Baris
  • 31
  • 5
  • Can you share the whole code. Where are you setting these clickable spans? – NIKHIL AGGARWAL Mar 30 '20 at 18:15
  • @NIKHILAGGARWAL I shared. Thanks. – Baris Mar 30 '20 at 18:39
  • Is this code even displaying any toast message when you click on the text on screen? You haven't set the spannable string to the textview. Neither you have set the clickable spans to the spannable string. – NIKHIL AGGARWAL Mar 30 '20 at 18:56
  • @NIKHILAGGARWAL how to set it? I spend many time for it, but I cant achive. I cant differentiate them all to make toast shows different text. – Baris Mar 30 '20 at 19:18

0 Answers0