Maaz Patel
Maaz Patel

Reputation: 774

Highlight text of String word by word inside card view on button click listener

I have to highlight specific words when I click on a button within a CardView. The text I need to highlight words in is also part of the CardView. The problem is that when I click on the button the text in other CardViews is also getting highlighted.

What causes this and how do I fix this issue?

This is my code:

 @Override
public void onBindViewHolder(final DuaViewHolder duaViewHolder,final  int i) {

    ci = allDuasLists.get(i);
    duaViewHolder.arebicText.setText(ci.getDuas());
    duaViewHolder.arebicEuivalantText.setText(ci.getArebicText());
    duaViewHolder.nativeText.setText(ci.getNativeText());
    mp = new MediaPlayer();
    duaViewHolder.playAudio.setBackgroundResource(R.drawable.play_48);

      class MyTimer extends CountDownTimer {
          public MyTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            WordtoSpan = new SpannableString(CurrentDua);
        }
        @Override
        public void onFinish() {
            k=0;
            this.cancel();
            Toast.makeText(context, "Timer finished", Toast.LENGTH_LONG).show();
        }
        @Override
        public void onTick(long millisUntilFinished) {
                WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), arrString[k], arrString[k + 1], Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                k++;
                duaViewHolder.arebicText.setText(WordtoSpan);
        }
    }

    duaViewHolder.playAudio.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ci = allDuasLists.get(i);
            CurrentDua = ci.getDuas();
            Log.e("Current Dua", CurrentDua);
            arrString = new int[CurrentDua.length() + 1];
            MakeSubString();
            duaViewHolder.playAudio.setBackgroundResource(R.drawable.stop_48);
            MyTimer timer = new MyTimer(mp.getDuration() * 1000, 1300);
            timer.start();
            k = 0;
        }

    });
}

Here is how it looks like:

Current look

Upvotes: 1

Views: 131

Answers (1)

Rajen Raiyarela
Rajen Raiyarela

Reputation: 5634

Try this way, instead of getting value of ci inside onclicklistener from list

ci = allDuasLists.get(i);

set ci to duaViewHolder.playAudio tag like

duaViewHolder.playAudio.setTag(ci);

and inside onClickListener get it like

ci = /*(Cast to your ci class)*/ v.getTag();

Upvotes: 1

Related Questions