Search This Blog

Wednesday, 21 March 2012

How to implement a News strip text field in your app

Hello friends,
                      So another issue for today , how to implement a text field that will scroll horizontally like a news strip in your blackberry app? here it is copy this code and make a class with the name of this class...and use it as other fields in blackberry you will get the arguments detail from the construstor....

package mypackage;

import java.util.Timer;
import java.util.TimerTask;

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.LabelField;

class MarqueLabel extends LabelField {
    int currentChar = 0;
    String currentText = null;
    Font ourFont;
    private Timer _scrollTimer;
    private TimerTask _scrollTimerTask;
    public MarqueLabel(String text, long style) {
        super(text, style);    
    }
    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());
        super.paint(graphics);
    }
    public void layout(int width, int height) {
        ourFont = this.getFont();
        setExtent(500, 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 + 2;
                    if (currentChar > fullText.length()) {
                        currentChar = 0;
                    }
                    invalidate();
                }
            };
            _scrollTimer.scheduleAtFixedRate(_scrollTimerTask, 0, 450);
        }
    }
    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)) {
            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);
        }
    }
    }



Use it as many times as you want and can change the speed by editing the timer in this class....ask for any queries....

No comments:

Post a Comment