Shadow
Shadow

Reputation: 6899

How to change color of textview when changing seekbar android?

I am using seekbar to change color of textview, but problem is when is click at particular position in seekbar, other position of textview color is not changing. How to solve this issue?

Thanks in advance.

Here is code.

  public class SplashActivity extends Activity {
private TextView textView;
private SeekBar seekBar;
int p = 0, noOfLength;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.test);
    textView = (TextView) findViewById(R.id.textView1);
    seekBar = (SeekBar) findViewById(R.id.seekBar1);
    textView.setText("Loading.Please wait...");// length of string is 22
    String textLength = textView.getText().toString();
    noOfLength = textLength.length();
    seekBar.setMax(noOfLength);
    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            try {
                Spannable spannableString = new SpannableString(textView
                        .getText());
                if (progress > p) {
                    p = progress;
                    spannableString.setSpan(new ForegroundColorSpan(
                            Color.YELLOW), 0, progress,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    textView.setText(spannableString);
                } else {
                    p = progress;
                    spannableString.setSpan(new ForegroundColorSpan(
                            Color.GRAY), progress - 1, progress + 1,
                            Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    textView.setText(spannableString);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });

}

}

When I am moving seekbar without leaving from start to end and from end to start position, it works but when i click seekbar at particular position, it's not working.

Here is screenshot of issue:

enter image description here

Upvotes: 2

Views: 1019

Answers (1)

rajesh
rajesh

Reputation: 199

If you are willing to color single character to color while moving seekbar then put this in onProgressChanged :

            try {
                Spannable spannableString = new SpannableString(textView
                        .getText());
                if (progress > p) {
                    p = progress;
                    spannableString.setSpan(new ForegroundColorSpan(
                            Color.YELLOW), progress-1, progress,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    textView.setText(spannableString);
                    spannableString.setSpan(new ForegroundColorSpan(
                            Color.GRAY), 0, progress-1,
                            Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    textView.setText(spannableString);
                } else {
                    p = progress;
                    spannableString.setSpan(new ForegroundColorSpan(
                            Color.YELLOW), progress-1, progress,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    textView.setText(spannableString);
                    spannableString.setSpan(new ForegroundColorSpan(
                            Color.GRAY), progress, noOfLength,
                            Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    textView.setText(spannableString);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

or if you want to color all the characters then try this:

        try {
                Spannable spannableString = new SpannableString(textView
                        .getText());
                if (progress > p) {
                    p = progress;
                    spannableString.setSpan(new ForegroundColorSpan(
                            Color.YELLOW), 0, progress,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    textView.setText(spannableString);
                } else {
                    p = progress;
                    spannableString.setSpan(new ForegroundColorSpan(
                            Color.GRAY), progress - 1, noOfLength,
                            Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                    textView.setText(spannableString);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

Upvotes: 2

Related Questions