0

I am trying to change the color of the TextView depending on the SeekBar's progress as following:

  • 0-25% -> green
  • 25%-50% -> yellow and so on...

When I using the following code, the TextView doesn't show the value anymore.

I would greatly appreciate any help!

Code:

public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) 
{   
    textView.setText(String.valueOf(progress + "%"));
    if(progress >= 25 && progress < 50)
        textView.setTextColor(R.color.Yellow);
    else if(progress >= 50 && progress < 75)
        textView.setTextColor(R.color.Orange);
    else if(progress >= 75 && progress <= 100)
        textView.setTextColor(R.color.Red);
    else 
        textView.setTextColor(R.color.Green);
}    

XML:

<TextView
    android:id="@+id/eT"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/tvProgress"
    android:textColor="@color/Green"
    android:textSize="25dp"
    android:textStyle="bold"
    android:layout_gravity="center"
    android:layout_marginBottom="5dp"
    />
GAMA
  • 5,958
  • 14
  • 79
  • 126
iamdanchiv
  • 4,052
  • 4
  • 37
  • 42
  • Does it give any error? Are you sure of the strings R.color.Yellow, R.color.Orange and R.color.Red having proper hex values? – Sana Jun 15 '12 at 08:36
  • it looks like you are not updating the textview with setText. You have to do textView.setText(progress); in each if condition – Andro Selva Jun 15 '12 at 08:43
  • That code should work without any problems. Are you sure you are setting the text + color on the right `TextView`? – user Jun 15 '12 at 09:14
  • @Sana - No, I am not getting any errors. Yes, the strings have proper hex values. – iamdanchiv Jun 15 '12 at 10:52
  • @Luksprog - Yes, it's the right TextView; I've double checked it. – iamdanchiv Jun 15 '12 at 10:53
  • @Andro Selva - I surely thought that was the problem, still no value! Yet if I just use textView.setText(String.valueOf(progress + "%")), it will work properly, but not with the color thing, only the default color I've given it in the begining. – iamdanchiv Jun 15 '12 at 10:53

1 Answers1

2

I don't get what the problem is, but I worked on it and it works very fine for me. I have attached the screen shots of the out put also. Please check.

main.xml

 <ScrollView  android:layout_width="wrap_content"
     android:layout_height="wrap_content">

     <RelativeLayout  android:layout_width="wrap_content"
         android:layout_height="wrap_content">



            <TextView  android:id="@+id/aksharTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="afadjklhfg"
                android:paddingTop="10dip"
                android:layout_below="@+id/arialTextView"/>



            <SeekBar  android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/aksharTextView"
                android:padding="10dip"
                android:id="@+id/seekbar"/>
     </RelativeLayout>


 </ScrollView>

onCreate()

 seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            public void onStopTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub

            }

            public void onStartTrackingTouch(SeekBar arg0) {
                // TODO Auto-generated method stub

            }

            public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
                aksharTextView.setText(progress+"");
                if(progress >= 25 && progress < 50)
                    aksharTextView.setTextColor(Color.BLUE);
                else if(progress >= 50 && progress < 75)
                    aksharTextView.setTextColor(Color.WHITE);
                else if(progress >= 75 && progress <= 100)
                    aksharTextView.setTextColor(Color.GREEN);
                else 
                    aksharTextView.setTextColor(Color.RED);
            }
        });

OutPut Screens

One

enter image description here

enter image description here

enter image description here

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • Thanks very much! I actually have the same code and to no end. I need to take a closer look cause surely it's a small thing that's escaping my eye. – iamdanchiv Jun 15 '12 at 12:34
  • try to use Color.BLUE, instead of R.color.blue. it might work. That's the only difference there – Andro Selva Jun 15 '12 at 12:35