8

I would like to have the color of the progress bar (specifically the secondary progress that is updated with setSecondaryProgress) to change dynamically at runtime when it crosses a certain point.

I am already able to substitute my own drawable into the bar during my Activity's onCreate function (changing color from yellow to green) by calling setProgressDrawable and substituting a resource I've copied from the default Android version of a horizontal progress (SDK/platforms/android-2.1/data/res/drawable/progress_horizontal.xml). However, updating this at runtime (yes, from the UI thread via an AsyncTask) causes the entire progress bar except for the thumbtab to go black or transparent.

PseudoNoise
  • 518
  • 5
  • 10
  • One approach I can think of is to use a view switcher between two seekbars with different colors, but if the user is using the tracking control at that time, this won't work. – PseudoNoise Feb 25 '10 at 02:33
  • As of IRC office hours just now, experimented with using invalidateDrawable to no avail. Also tried reading the values of the drawables but each object (the two colors from resources, and the getProgressDrawable after setting it) all have different hash values. – PseudoNoise Feb 26 '10 at 02:05

3 Answers3

3

I think this is what you are looking for

enter image description here

enter image description here

enter image description here

This can be achived by creating custom drwables for background and progress of seak bar See this link for sample code.

Pls see the below links also http://javatechig.com/android/android-seekbar-example/

Change the color of a seekbar on onProgressChanged

Community
  • 1
  • 1
chandan
  • 2,400
  • 2
  • 18
  • 23
  • Yes! My question predates the linked question by several years, but I'm happy to finally have an answer (though I moved on from this project). – PseudoNoise Jun 28 '13 at 16:30
1

SeekColorActivity.java

package com.exercise.seekcolor;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.SeekBar;

public class SeekColorActivity extends Activity {

private int seekR, seekG, seekB;
SeekBar redSeekBar, greenSeekBar, blueSeekBar;
LinearLayout mScreen;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mScreen = (LinearLayout) findViewById(R.id.myScreen);
    redSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_R);
    greenSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_G);
    blueSeekBar = (SeekBar) findViewById(R.id.mySeekingBar_B);
    updateBackground();

    redSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
    greenSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
    blueSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);

}

private SeekBar.OnSeekBarChangeListener seekBarChangeListener
= new SeekBar.OnSeekBarChangeListener()
{

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
  boolean fromUser) {
// TODO Auto-generated method stub
 updateBackground();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
};

private void updateBackground()
{
 seekR = redSeekBar.getProgress();
 seekG = greenSeekBar.getProgress();
 seekB = blueSeekBar.getProgress();
 mScreen.setBackgroundColor(
  0xff000000
  + seekR * 0x10000
  + seekG * 0x100
  + seekB
  );
}
}
Reno
  • 33,594
  • 11
  • 89
  • 102
  • 1
    That sets the screen color due to what the progress bar is. What I asked is how to set the background color of the progress bar, not the entire screen. – PseudoNoise Aug 01 '11 at 18:25
-1

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys.

When SeekBarChange, the onProgressChanged() of OnSeekBarChangeListener will be called, and the background of the screen(LinearLayout) will be updated according to the value (SeekBar.getProgress()).

main.xml

  • 1
    This is completely unrelated to the question. The OP asked about updating the color, and not once do you mention anything about color. – lrAndroid May 04 '15 at 17:46