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

Posts: 19
Registered: ‎02-03-2009
My Device: Not Specified

Text scrolling............

is it possible to show scrollable text ......


like a marquee or stock ticker?
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Text scrolling............

There is no BlackBerry UI that does this though I believe there is a J2ME control that does.  However you can't mix J2ME and BlackBerry UI Components and I presume you want a BlackBerry one.


So you have to write your own.


I wrote the following as a test sometime ago.  It is far from perfect, but it might help you create something you can use.  Note that in additoin to scrolling, it also stops scrolling when you focus on it, and then you can use the trackball to scroll left and right on the text.  Not sure if that is a good idea or not,  Like I said, this was just an experiment, this is not production code, use at your own risk.  Feel free to use it, however if you do flash it up and get it working properly, it would be nice if you could share it.


Note: I would definitely add onObscured() and onUndisplay() code in there to stop the Timer when the Screen/Field was not in View, and put the start in onExposed().


LabelField testLabel = new LabelField("This is a very long string that will not fit on one line - let us see if we can make it scroll.", Field.FOCUSABLE) { int currentChar = 0; String currentText = null; Font ourFont; private Timer _scrollTimer; private TimerTask _scrollTimerTask; public void paint(Graphics graphics) { currentText = this.getText(); if ( currentChar < currentText.length() ) { currentText = currentText.substring(currentChar); } graphics.drawText(currentText, 0, 0, DrawStyle.ELLIPSIS, Display.getWidth()); } public void layout(int width, int height) { ourFont = this.getFont(); setExtent(Display.getWidth(), ourFont.getHeight()); } protected void onDisplay() { startScroll(); } protected void onUnfocus() { startScroll(); } private void startScroll() { // Start scrolling final String fullText = this.getText(); if ( _scrollTimer == null ) { _scrollTimer = new Timer(); _scrollTimerTask = new TimerTask() { public void run() { currentChar = currentChar + 4; if ( currentChar > fullText.length() ) { currentChar = 0; } invalidate(); } }; _scrollTimer.scheduleAtFixedRate(_scrollTimerTask, 500, 500); } } protected void onFocus(int direction) { if ( _scrollTimer != null ) { _scrollTimerTask.cancel(); _scrollTimer.cancel(); _scrollTimer = null; _scrollTimerTask = null; } } protected boolean navigationMovement(int dx, int dy, int status, int time) { currentText = this.getText(); int oldCurrentChar = currentChar; if ( Math.abs(dx) > Math.abs(dy) ) { // horizontal scroll if ( dx > 0) { currentChar = Math.min(currentText.length()-1, currentChar+1); } else if ( dx < 0 ) { currentChar = Math.max(0, currentChar-1); } if ( oldCurrentChar != currentChar ) { this.invalidate(); } return true; } else { return super.navigationMovement(dx, dy, status, time); } } };


Posts: 18
Registered: ‎07-29-2010
My Device: Not Specified

Re: Text scrolling............

peter can u give me the complete code beggining from main of scrollable text

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

Re: Text scrolling............

There is no main here.  This is just a Field.  So you need to create your own UiApplication and Screen, then create this LabelField and add it to a Screen.


The samples have a number of examples of screens that you could add this to.

New Contributor
Posts: 8
Registered: ‎10-27-2010
My Device: 9700

Re: Text scrolling............

Hi Peter, I am using your code.

It works fine as in it does give a ticker effect.


But i am having a problem with it.

If i use it with few other layouts the screen doesnt scroll.


I have a main vertical manager on top of which one more vertical and one horizontal is placed.

I want to add a RichtextField on top of the above two layouts.

But When i add it along with ur code, the long RTF string does move(left to right) but the screen doesnt scroll vertically.


Heres the code -



 VerticalFieldManager verticalFieldManager = new VerticalFieldManager(VerticalFieldManager.USE_ALL_HEIGHT | VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR);
        this.setTitle(new LabelField("Clean Water - Maintenance", Field.FIELD_HCENTER));
        VerticalFieldManager vrfm = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH |VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR | VerticalFieldManager.FIELD_HCENTER);        
        HorizontalFieldManager hrfm = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH| HorizontalFieldManager.HORIZONTAL_SCROLL| HorizontalFieldManager.HORIZONTAL_SCROLLBAR);

        vrfm.setMargin(0, 5, 5, 5);
        Border br = BorderFactory.createRoundedBorder(new XYEdges(10, 10, 10, 10), 0xD8E9FB,Border.STYLE_FILLED);
        hrfm.setMargin(0, 5, 5, 5);
        float p1Values[] = txp.getValues(txp.getElementById("P1",0));
        float p2Values[] = txp.getValues(txp.getElementById("P2",0));
        float p3Values[] = txp.getValues(txp.getElementById("P3",0));
        final String[] barLabels = {"P1", "P2", "P3"};
        final String[] pieLabels = {"50", "60", "90"};
        final int[] values = { (int)p1Values[0], (int)p1Values[1], (int)p1Values[2], (int)p2Values[0], (int)p2Values[1], (int)p2Values[2], (int)p3Values[0], (int)p3Values[1], (int)p3Values[2]};
        final int[] colors = {0x009F49, 0xFFC101, 0xF90602 };
        emr_job_bar = new BarGraphField1(values, colors, barLabels, 130, 200, 30, 30,
                "No. of Jobs", 1, 3);
        emr_job_barLegend = new LegendField("", pieLabels, colors, Font.getDefault());

        int myTelc = Integer.parseInt(txp.getElementById("TELLC",0));
        int myTels = Integer.parseInt(txp.getElementById("TELLS",0));
        final String[] barLabels1 = {"  TELC", "  TELS"};
        final int[] values1 = { myTelc, myTels};
        final int[] colors1 = {0x009F49, 0x009F49};
        job_vol_bar = new BarGraphField1(values1, colors1, barLabels1, 130, 200, 30, 30,
                "Count of Jobs", 0, 1);
        Font font = Font.getDefault();
            FontFamily ff = FontFamily.forName("BBAlpha Serif");
            font = ff.getFont(Font.BOLD, 18);
        catch(Exception e)
            System.out.println("==== Exception inside CWRMBarScreen ==== "+e);
        RichTextField rtf = new RichTextField("Emergency Jobs" ,Field.NON_FOCUSABLE);
        RichTextField rtf2 = new RichTextField("Job Volumes" ,Field.NON_FOCUSABLE);

        TickerRichTextField trtf = new TickerRichTextField("Main Incidents : Burst at Christ Church Road,Reading - Mains Repair in North London disrupted due to bad weather." ,Field.FOCUSABLE);
        VerticalFieldManager vrfm2 = new          VerticalFieldManager(VerticalFieldManager.FIELD_HCENTER);
        vrfm2.setPadding(5, 5, 5, 5);
        vrfm2.add(new NullField(NullField.FOCUSABLE));
        vrfm2.add(new NullField(NullField.FOCUSABLE));
        verticalFieldManager.add(new NullField(NullField.FOCUSABLE));
        verticalFieldManager.add(new NullField(NullField.FOCUSABLE));




This TickerRichTextField is what i have created extending RichTextField which has ur code for scrolling text.


Is it because of the onFocus metod u have over ridden ???


Pls Help


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

Re: Text scrolling............

Welcome to the forums!


Can you create a minimal sample that show the problem - there is a lot of code that you have in there?


Also I am pretty sure that arkadyz created a better ticker sample.  I would search round to see if you find some other code to base your ticker on.  I don't use the code above in any production app, it was just created as a sample to give someone an idea. 

New Contributor
Posts: 8
Registered: ‎10-27-2010
My Device: 9700

Re: Text scrolling............

Hey peter Thanks. I fixed it. But thanks. but now i have another issue. haha The text, when it ends scrolling, starts from the same position from where it started i.e. form the left of the screen. What i want is it should start scrolling from the right end of the screen and finish at left and iterate like this. Can u help in it ? Thanks. And another query is what does dis method do in your code - navigationMovement. any specific reason to over ride it ? It will work the same if u comment it also.

Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Text scrolling............


contrary to Peter's opinion, I haven't posted any ticker fields to the forums yet, even though this is a relatively simple exercise.


As for alternating the scrolling direction left and right - look at this piece of Peter's code:


            _scrollTimerTask = new TimerTask() {
                public void run() {
                    currentChar = currentChar + 4;
                    if ( currentChar > fullText.length() ) {
                        currentChar = 0;



The red line shifts the new starting position to the right. The blue lines make the jump back to the beginning of the line.


If you want your "bouncing" scroll effect, have a scrollDirection member in your TimerTask, initial value of 1 (going forward), at the end of the line flip it to -1, at the beginning of the line flip it back to 1 and so forth.

The red line would then read currentChar = currentChar + 4 * scrollDirection;, the blue lines would become


if (currentChar >= fullText.length()) {
  scrollDirection = -1;
  currentChar = fullText.length();
if (currentChar < 0) {
  scrollDirection = 1;
  currentChar = 0;

 Good luck!



please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Text scrolling............

Sorry arkadyz, must have been someone else, but I was sure you could do it anyway!  Smiley Happy


With respect to the second question, the idea of overriding navigationMovement was to give the user the option to actually stop the ticker and then move it back and forward, so that they could read it as they wished.  Only works on trackball devices, not sure how useful it is, as already noted, this was proof of concept code only, not production. 


Glad it has been useful. 

Posts: 84
Registered: ‎07-22-2011
My Device: bb-9550,bb-9700
My Carrier: bb developer

Re: Text scrolling............

hi peter,


i have tried with your code its working but my issue is that i need to scroll the text from right again when it completes the first scroll.can u please say me where to make changes in your code.