Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 141
Registered: ‎02-27-2009
My Device: Not Specified

Is there any way to change the color of a ProgressIndicatorView??

With many of the other 6.0 controls, in order to change the color, I --

  • derive a class from the control,
  • over-ride the Paint method,
  • in that Paint method, set the graphic's color as desired,
  • call super.Paint to paint the color.

This works with Button, Checkbox, Label, etc. 

BUT IT DOES NOT WORK WITH ProgressIndicatorView.

 

I believe this is because this view actually contains a VerticalFieldManager, and in that, the ProgressBarField.

It is the ProgressBarField's paint method that needs to be over-riden, and the Progress classes provide no documented methods to be able to replace that field with a derived class.

 

I tried to get to it by calling getField(0) on the ProgressIndicatorView, to get the VerticalFieldManager, but that getField() call throw an exception.  The getFieldCount() is reporting 0 fields, yet, in the debugger I can see that the ProgressIndicatorView has one field - the VerticalFieldManager!

 

Anyway, without my having to draw a progress bar myself, is there any way to change the color of the progress bar and the text, that are drawn by a ProgressIndicatorView?

 

Retired
Posts: 1,561
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: Is there any way to change the color of a ProgressIndicatorView??

Hi there,

 

I've taken a look and unfortunately there don't appear to be any APIs to allow this directly. This would require a custom implementation of a progress bar.

 

Ihaven't tested myself, but perhaps overriding the paint method and drawing your own rectangle/text on top of the existing progress bar might allow you to hack around this.

 

Erik Oros

BlackBerry Development Advisor


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Developer
Posts: 141
Registered: ‎02-27-2009
My Device: Not Specified

Re: Is there any way to change the color of a ProgressIndicatorView??

thanks for the reply Erik

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Is there any way to change the color of a ProgressIndicatorView??

Developer
Posts: 141
Registered: ‎02-27-2009
My Device: Not Specified

Re: Is there any way to change the color of a ProgressIndicatorView??

Yes Peter, that does look like an interesting alternative.  I'll look into it.  Thanks.

Highlighted
Trusted Contributor
Posts: 145
Registered: ‎05-01-2011
My Device: 9780
My Carrier: Developer

Re: Is there any way to change the color of a ProgressIndicatorView??

[ Edited ]

Please try the following one:

 

I've changed the background color and text color of the progress view

 

Taken the sample code from

http://docs.blackberry.com/en/developers/deliverables/18125/Progress_indicator_1214566_11.jsp

 

& I've altered that coding to change the bg & text color.

 

The changes done are:

 

private final int maxValue = 100;
private int currentValue = 0;

 protected void paint(final Graphics graphics) {
            graphics.setColor(Color.DARKORANGE);
            final double widthToFill = ((double) getWidth()/maxValue) * currentValue;
            graphics.fillRect(0, 0, (int) widthToFill, getHeight());
            graphics.setColor(Color.BLUE);
            graphics.drawText(String.valueOf(currentValue), getWidth()/2, 0);
        };

  currentValue = i;

 

............

 

package com;

import net.rim.device.api.ui.Adjustment;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.progressindicator.ProgressIndicatorController;
import net.rim.device.api.ui.component.progressindicator.ProgressIndicatorListener;
import net.rim.device.api.ui.component.progressindicator.ProgressIndicatorModel;
import net.rim.device.api.ui.component.progressindicator.ProgressIndicatorView;
import net.rim.device.api.ui.container.MainScreen;


public class ProgressIndicatorDemoScreen extends MainScreen {   

    private final int maxValue = 100;
    private int currentValue = 0;

    ProgressIndicatorView view = new ProgressIndicatorView(0) {
        protected void paint(final Graphics graphics) {
            graphics.setColor(Color.DARKORANGE);
            final double widthToFill = ((double) getWidth()/maxValue) * currentValue;
            graphics.fillRect(0, 0, (int) widthToFill, getHeight());
            graphics.setColor(Color.BLUE);
            graphics.drawText(String.valueOf(currentValue), getWidth()/2, 0);
        };
    };
    ProgressIndicatorModel model = new ProgressIndicatorModel(0, maxValue, 0);    
    ProgressIndicatorController controller = new ProgressIndicatorController();        

    ProgressThread _progressThread;

    public ProgressIndicatorDemoScreen() {        
        model.setController(controller);
        model.addListener(new DemoProgressIndicatorListener());        
        view.setModel(model);
        view.setController(controller);        
        controller.setModel(model);
        controller.setView(view);

        view.createProgressBar(Field.FIELD_HCENTER);       

        add(new LabelField());
        add(new LabelField());
        add(new LabelField("Percent completion"));
        add(view);        

        final MenuItem _startIndicator = new MenuItem("Start indicator", 66000, 0) {
            public void run() {
                if(_progressThread != null) {
                    _progressThread.stopThread();                             
                }
                _progressThread = new ProgressThread();
                _progressThread.start();
            }
        };

        final MenuItem _pauseIndicator = new MenuItem("Pause indicator", 66010, 0) {
            public void run() {
                view.getModel().cancel();
            }
        };

       final MenuItem _resumeIndicator = new MenuItem("Resume indicator", 66020, 0) {
            public void run() {
                view.getModel().resume();
            }
        };

        addMenuItem(_startIndicator);
        addMenuItem(_pauseIndicator);
        addMenuItem(_resumeIndicator);   
    }

    class ProgressThread extends Thread {

        private boolean _paused;
        private boolean _stop;        

        public void run() {
            for(int i = 0; i <= 100; ++i) {  
                currentValue = i;
                synchronized(this) {
                    if(_stop) {
                        break;
                    }
                    if(_paused) {
                        try {                           
                            wait();                            
                        }
                        catch(final InterruptedException ie) { }
                    }                    
                }
                ProgressIndicatorDemoScreen.this.model.setValue(i);
                try {
                    sleep(250);
                }
                catch(final InterruptedException ie) { }                
            }           
        }

        public synchronized void setPaused(final boolean paused) {              
            _paused = paused;
            this.notify();       
        }             

        public synchronized void stopThread() {
            _stop = true;
            if(_paused) {
                this.notify();                                        
            }
        }      
    }   

    private final class DemoProgressIndicatorListener implements ProgressIndicatorListener {
        public void cancelled() {
            _progressThread.setPaused(true);
        }
        public void resumed() {        
            _progressThread.setPaused(false);
        }
        public void reset() { }
        public void setNonProgrammaticValue(final int value) { }      
        public void configurationChanged(final Adjustment source) { }
        public void valueChanged(final Adjustment source) { }        
    }
}

 

 

Progress1.png

 

Developer
Posts: 141
Registered: ‎02-27-2009
My Device: Not Specified

Re: Is there any way to change the color of a ProgressIndicatorView??

Yes, thank you for the code.  I agree that is isn't hard to paint this control yourself. 

 

Frankly, if you''re going to paint it yourself, its only a very small step to replacing the ProgressIndicatorView completely and just derive from Field.  Might as well replace the whole ProgressIndicatorView with a very simple custom control that you paint yourself.

 

However in this case, the requirement is to use RIM's controls and not create custom ones.

 

None the less, your code will be very helpful to other readers I'm sure.  Thanks.

Trusted Contributor
Posts: 145
Registered: ‎05-01-2011
My Device: 9780
My Carrier: Developer

Re: Is there any way to change the color of a ProgressIndicatorView??

@in order to change the color, I -- derive a class from the control, over-ride the Paint method

 

this is what I've done

 

Just get the progress indicator view from (http://docs.blackberry.com/en/developers/deliverables/18125/Progress_indicator_1214566_11.jsp) and override the paint method

 

Sorry I don't understand. Is anything wrong with the code?

Developer
Posts: 141
Registered: ‎02-27-2009
My Device: Not Specified

Re: Is there any way to change the color of a ProgressIndicatorView??

No there is nothing wrong with your code. I'm sure it works perfectly.

 

To continue quoting from my original post --

 

  • in that Paint method, set the graphic's color as desired,
  • call super.Paint to paint the color.  (should say ... to paint the control)

Also --

     ..... without my having to draw a progress bar myself,

 

Your code paints the progress bar rather than using the base class to do the painting.  Thus you don't use the styles set for the base class, nor any other features of the base class' painting method, whatever they might be.

 

Again your method is fine.  The only thing I would say, is if your are going to paint the control yourself, you don't need to bother with all of the overhead of ProgressIndicatorView and its relatively heavy model / view / control classes.

 

Again thanks for posting it, and i'm sure having your code posted will help future readers.

 

Developer
Posts: 141
Registered: ‎02-27-2009
My Device: Not Specified

Re: Is there any way to change the color of a ProgressIndicatorView??

Peter,

  Just to get back to you on this, GaugeField also will not allow me to over-ride the color either.

 

  In addiiton, GaugeField can only display the text of the value in the middle of the progress bar.  ProgressIndicatorView can put the text on either side.

 

  Thanks for the suggestion anyway.

  Paul