BasicEditField BlackBerry - blackberry

I want a BasicEditField with rounded border and white background fill.
This is my code
public class BasicField extends BasicEditField {
XYEdges padding = new XYEdges(0,0,0,0);
int color = Color.BLACK;
int lineStyle = Border.STYLE_SOLID;
int Width, Height;
Border roundedBorder = BorderFactory.createRoundedBorder(padding, color, lineStyle);
public BasicField()
{
super(BasicEditField.NO_NEWLINE);
//this.setPadding(2, 2, 2, 2);
//this.setBorder(roundedBorder);
}
public int getPreferredWidth()
{
int labelWidth = getFont().getAdvance(getLabel()) - 1;
Width = Graphics.getScreenWidth() -150;
return Width;
}
public int getPreferredHeight()
{
return 10;
}
public void paint(Graphics g) {
int currCol = g.getColor();
g.setBackgroundColor( Color.WHITE );
g.fillRect(0, 0, getPreferredWidth(), getPreferredHeight() );
g.clear();
g.setColor( Color.NAVY );
super.paint( g );
}
protected void layout( int maxWidth, int maxHeight )
{
super.layout( getPreferredWidth(), getPreferredHeight() );
}
}

Here you go -
Screenshot:
border.png:
MyEdit.java:
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
public class MyEdit extends UiApplication {
public static void main(String args[]) {
MyEdit app = new MyEdit();
app.enterEventDispatcher();
}
public MyEdit() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
Border myBorder = BorderFactory.createBitmapBorder(
new XYEdges(20, 16, 27, 23),
Bitmap.getBitmapResource("border.png"));
BasicEditField myField = new BasicEditField(TextField.NO_NEWLINE) {
protected void paint(Graphics g) {
if (getTextLength() == 0) {
g.setColor(Color.LIGHTGRAY);
g.drawText("Search", 0, 0);
}
g.setColor(Color.BLACK);
super.paint(g);
}
};
public MyScreen() {
myField.setBorder(myBorder);
setTitle(myField);
}
}

Friend... try this code.....
class BasicEditScreen extends MainScreen
{
public BasicEditScreen()
{
add(new BasicField(200, 40, FIELD_LEFT, "Enter here..", "horizontal"));
}
}
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.VerticalFieldManager;
public class BasicField extends Manager
{
private int managerWidth;
private int managerHeight;
private int inactiveBorderColor = Color.GRAY;
private int activeBorderColor = Color.GREENYELLOW;
private int borderColor = inactiveBorderColor;
private int backgroundColor = Color.WHITE;
private int arcWidth;
private VerticalFieldManager vfm ;
private EditField editField;
int nn =1;
int count=0;
public BasicField(int width, int height, long style, final String hint,String type_horizontal_vertical)
{
super(style | NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL);
if(type_horizontal_vertical.equalsIgnoreCase("Horizontal"))
vfm = new VerticalFieldManager(HORIZONTAL_SCROLL | USE_ALL_WIDTH );
if(type_horizontal_vertical.equalsIgnoreCase("Vertical"))
vfm = new VerticalFieldManager(VERTICAL_SCROLL| USE_ALL_WIDTH | USE_ALL_HEIGHT);
managerWidth = width;
managerHeight = height;
long innerStyle = style & (READONLY | FOCUSABLE_MASK); // at least
if (innerStyle == 0)
{
innerStyle = FOCUSABLE;
}
if(style==EditField.FILTER_EMAIL)
{
innerStyle = EditField.FILTER_EMAIL;
}
if(style==EditField.FILTER_INTEGER)
{
innerStyle = EditField.FILTER_INTEGER;
}
editField = new EditField("", null, EditField.DEFAULT_MAXCHARS, innerStyle)
{
public void paint(Graphics g)
{
if(editField.getText().length()==0)
{
g.setColor(Color.GRAY);
g.drawText(hint,5,2);
super.paint(g);
}
g.setColor(Color.BLACK);
super.paint(g);
}
};
arcWidth = editField.getFont().getHeight() & 0xFFFFFFFE; // make it even
add(vfm);
vfm.add(editField);
}
public void setFont(Font font)
{
super.setFont(font);
editField.setFont(font);
arcWidth = editField.getFont().getHeight() & 0xFFFFFFFE;
updateLayout();
}
public void setBorderColors(int inactive, int active)
{
inactiveBorderColor = inactive;
activeBorderColor = active;
invalidate();
}
public void setBackgroundColor(int bgColor)
{
backgroundColor = Color.AQUA;
invalidate();
}
public String getText()
{
return editField.getText();
}
public void setText(String newText)
{
editField.setText(newText);
}
public int getPreferredWidth()
{
return managerWidth;
}
public int getPreferredHeight()
{
return managerHeight;
}
protected void sublayout(int w, int h)
{
if (managerWidth == 0)
{
managerWidth = w;
}
if (managerHeight == 0)
{
managerHeight = h;
}
int actWidth = Math.min(managerWidth, w);
int actHeight = Math.min(managerHeight, h);
layoutChild(vfm, actWidth - arcWidth, actHeight - arcWidth);
setPositionChild(vfm, arcWidth / 2, arcWidth / 2);
setExtent(actWidth, actHeight);
}
protected void paint(Graphics g)
{
int prevColor = g.getColor();
int myWidth = managerWidth;
int myHeight = managerHeight;
g.setColor(backgroundColor);
g.fillRoundRect(0, 0, myWidth, myHeight, arcWidth, arcWidth);
g.setColor(borderColor);
g.drawRoundRect(0, 0, myWidth, myHeight, 10,10);
g.drawRoundRect(1, 1, myWidth - 2, myHeight - 2, 10-2, 10-2);
g.setColor(prevColor);
super.paint(g);
}
private void adjustBorderColor()
{
int nextColor;
if (editField.isFocus())
{
nextColor = activeBorderColor;
}
else
{
nextColor = inactiveBorderColor;
}
if (borderColor != nextColor)
{
borderColor = nextColor;
invalidate();
}
}
}

Related

How can i select a drop down which i added over a list field?

Hi Friend's in my BlackBerry Application i use a list Field, and over this list Field i add a drop down. Now my problem is how do i select the drop down which i added over the list Field.Now i only select list field and not the drop down but my requirement is first i should select the drop down and then list field. I have included my code here please check it.
public class TaskListField extends ListField implements ListFieldCallback, KeypadListener {
private Vector rows;
private Bitmap p1;
private Bitmap p2;
public LoginPage objLoginPage;
public String[] data={"Call","Visit"};
public String strIndex = null;
public int tempIndex = 0;
public int intvalue;
public int id;
public int value;
public boolean hasFocus = false;
public static String[] arrOutletName = { "Dosa Store", "Benfish Bhawan",
"Nalban Park", "Korunamoyee", "206 Bus Stop", "aaaaaaaa",
"bbbbbbbb", "ccccccccc", "dddddddd", "eeeeeee" };
public ObjectChoiceField ocf;
public TaskListField() {
super(0, ListField.MULTI_SELECT);
setRowHeight(80);
setEmptyString("Hooray, no tasks here!", DrawStyle.HCENTER);
setCallback(this);
p1 = Bitmap.getBitmapResource("name.png");
p2 = Bitmap.getBitmapResource("name.png");
rows = new Vector();
for (int x = 0; x < XmlHander.vectSrno.size(); x++) {
TableRowManager row = new TableRowManager();
strIndex = String.valueOf(x);
String strVectno = XmlHander.vectSrno.elementAt(x).toString();
String strSLADate = XmlHander.vectSldate.elementAt(x).toString();
if (x % 2 == 0) {
row.add(new BitmapField(null));
}
else {
row.add(new BitmapField(null));
}
LabelField task = new LabelField(" ");
if (x % 15 == 0) {
task.setFont(Font.getDefault().derive(Font.BOLD | Font.UNDERLINED));
} else {
task.setFont(Font.getDefault().derive(Font.BOLD));
}
row.add(task);
row.add(new LabelField(strVectno, DrawStyle.ELLIPSIS) {
protected void paint(Graphics graphics) {
graphics.setColor(0x00878787);
// graphics.setColor(Color.PINK);
super.paint(graphics);
}
});
row.add(ocf=new ObjectChoiceField("",data){
protected void paint(Graphics graphics){
graphics.setColor(0x00878787);
super.paint(graphics);
}
});
ocf.setChangeListener(new FieldChangeListener(){
public void fieldChanged(Field field, int context) {
if(context != PROGRAMMATIC)
Dialog.alert(data[ocf.getSelectedIndex()]);
}
});
rows.addElement(row);
}
setSize(rows.size());
}
public void drawListRow(ListField listField, Graphics g, int index, int y, int width) {
TaskListField list = (TaskListField) listField;
TableRowManager rowManager = (TableRowManager) list.rows.elementAt(index);
rowManager.drawRow(g, 0, y, width, list.getRowHeight());
}
private class TableRowManager extends Manager {
public TableRowManager() {
super(0);
}
public void drawRow(Graphics g, int x, int y, int width, int height) {
layout(width, height);
setPosition(x, y);
g.pushRegion(getExtent());
subpaint(g);
g.setColor(0x00CACACA);
g.drawLine(0, 0, getPreferredWidth(), 0);
g.popContext();
}
protected void sublayout(int width, int height) {
int fontHeight = Font.getDefault().getHeight();
int preferredWidth = getPreferredWidth();
Field field = getField(0);
layoutChild(field, 32, 32);
setPositionChild(field, 0, 0);
field = getField(1);
layoutChild(field, preferredWidth - 16, fontHeight + 1);
setPositionChild(field, 34, 3);
field = getField(2);
layoutChild(field, 150, fontHeight + 1);
setPositionChild(field, 34, fontHeight + 6);
field = getField(3);
layoutChild(field, 150, fontHeight + 1);
setPositionChild(field, preferredWidth - 152, fontHeight + 6);
setExtent(preferredWidth, getPreferredHeight());
}
public int getPreferredWidth() {
return Graphics.getScreenWidth();
}
public int getPreferredHeight() {
return getRowHeight();
}
}
public Object get(ListField listField, int index) {
return null;
}
public int getPreferredWidth(ListField listField) {
return 0;
}
public int indexOfList(ListField listField, String prefix, int start) {
return 0;
}
protected boolean navigationClick(int status, int time) {
final int index;
index = this.getFieldWithFocusIndex() - 1;
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushScreen(new Details(id));
}
});
return false;
}
public int moveFocus(int amount, int status, int time) {
invalidate(getSelectedIndex());
id = this.getSelectedIndex();
invalidate();
return super.moveFocus(amount, status, time);
}
public void onFocus(int direction) {
hasFocus = true;
super.onFocus(direction);
System.out.println("direction==" + direction);
invalidate();
}
public void onUnfocus() {
updateLayout();
super.onUnfocus();
invalidate();
}
private int getFieldWithFocusIndex() {
return 0;
}
}

How Can I use blackberry browser in my view hiearchy?

I need to show some links in my application's views. But I can't find anything about it. is it possible??
I want to place browsercontent(field) where gray area for os <5.00 ?
If you want to show a field which is a hyperlink use the following code.
package com.myApp.controls;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
public class HrefField extends Field {
private String content;
private Font fieldFont;
private int fieldWidth;
private int fieldHeight;
private boolean active = false;
private int backgroundColour = Color.WHITE;
private int textColour = Color.BLACK;
private int[] drawFocusColors;
public HrefField(String content) {
super(Field.FOCUSABLE);
this.content = content;
fieldFont = Font.getDefaultFont();
fieldWidth = fieldFont.getAdvance(content) + 2;
fieldHeight = fieldFont.getHeight() + 3;
drawFocusColors = new int[] { Color.ORANGE,
Color.ORANGE,Color.RED,
Color.RED};
}
public void setColours(int backgroundColour, int textColour) {
this.backgroundColour = backgroundColour;
this.textColour = textColour;
invalidate();
}
public void setBackgroundColour(int backgroundColour) {
this.backgroundColour = backgroundColour;
invalidate();
}
public void setTextColour(int textColour) {
this.textColour = textColour;
invalidate();
}
public void setMaskColour() {
invalidate();
}
public void setFont(Font fieldFont) {
this.fieldFont = fieldFont;
}
public int getPreferredWidth() {
return fieldWidth;
}
public int getPreferredHeight() {
return fieldHeight;
}
protected void layout(int arg0, int arg1) {
setExtent(getPreferredWidth(), getPreferredHeight());
}
protected void paint(Graphics graphics) {
int[] X_PTS = new int[] { 0, fieldWidth, fieldWidth, 0 };
int[] Y_PTS = { 0, 0, fieldHeight, fieldHeight };
if (active) {
graphics.drawShadedFilledPath(X_PTS, Y_PTS, null, drawFocusColors,
null);
} else {
graphics.setColor(backgroundColour);
graphics.fillRect(0, 0, fieldWidth, fieldHeight);
}
graphics.setColor(textColour);
graphics.setFont(fieldFont);
graphics.drawText(content, 1, 1);
graphics.drawLine(1, fieldHeight - 2, fieldWidth - 2, fieldHeight - 2);
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(1);
return true;
}
protected void onFocus(int direction) {
active = true;
invalidate();
}
protected void onUnfocus() {
active = false;
invalidate();
}
}

Blackberry: custom ButtonField with disappearing focus highlight

I'm trying to create a custom ButtonField, whose focus (the blue highlight color) would disappear after few seconds of inactivity - like in original music player on BB phones with touchscreen.
I've almost succeeded in that with the following example:
Here are the east.png and west.png (courtesy of openclipart.org):
Here is my test code MyFocus.java:
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.image.*;
public class MyFocus extends UiApplication {
public static void main(String[] args) {
MyFocus app = new MyFocus();
app.enterEventDispatcher();
}
public MyFocus() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
public MyScreen() {
setTitle("2nd trackpad click not working");
getMainManager().setBackground(BackgroundFactory.createLinearGradientBackground(Color.WHITE, Color.GRAY, Color.DARKGRAY, Color.GRAY));
add(new MyButton(MyButton.EAST));
add(new MyButton(MyButton.WEST));
add(NF);
}
class MyButton extends ButtonField {
public final static int EAST = 0;
public final static int WEST = 1;
public final static int WIDTH = 100;
public final static int HEIGHT = 100;
private final XYEdges EDGES = new XYEdges(0, 0, 0, 0);
private final Application _app = UiApplication.getUiApplication();
private final static long FOCUS_DURATION = 3000L;
private int _focusTimer = -1;
private final int _direction;
public MyButton(int direction) {
setMargin(EDGES);
setPadding(EDGES);
setImageSize(WIDTH, HEIGHT);
setBorder(VISUAL_STATE_NORMAL,
BorderFactory.createSimpleBorder(EDGES));
setBorder(VISUAL_STATE_FOCUS, BorderFactory.createSimpleBorder(EDGES));
setBorder(VISUAL_STATE_ACTIVE, BorderFactory.createSimpleBorder(EDGES));
setBackground(VISUAL_STATE_NORMAL, BackgroundFactory.createSolidTransparentBackground(Color.GREEN, 0));
setBackground(VISUAL_STATE_FOCUS, BackgroundFactory.createSolidTransparentBackground(Color.BLUE, 100));
setBackground(VISUAL_STATE_ACTIVE, BackgroundFactory.createSolidTransparentBackground(Color.RED, 200));
_direction = direction;
switch(_direction) {
case EAST:
setImage(ImageFactory.createImage("east.png"));
break;
case WEST:
setImage(ImageFactory.createImage("west.png"));
break;
}
}
// display red background on long touch and hold
protected boolean touchEvent(TouchEvent event) {
if (event.getEvent() == TouchEvent.CLICK) {
applyThemeOnStateChange();
return true;
}
return super.touchEvent(event);
}
protected void onUnfocus() {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
super.onUnfocus();
}
protected void onFocus(int direction) {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
_focusTimer = _app.invokeLater(new Runnable() {
public void run() {
MyButton.super.onUnfocus();
_focusTimer = -1;
}
}, FOCUS_DURATION, false);
super.onFocus(direction);
}
public int getPreferredHeight(){
return HEIGHT;
}
public int getPreferredWidth(){
return WIDTH;
}
protected void layout(int width, int height) {
setExtent(WIDTH, HEIGHT);
}
}
}
My problem is visible, when I first select a button and wait few seconds for its focus to disappear. Then I click on the track pad and while the button is pushed (verified that), you don't see anything at the screen - it doesn't turn blue or red.
I've tried all combinations of navigationClick() and trackwheelUnclick() but can not fix that.
Any help please?
Alex
UPDATE 1:
I've tried the following, but it doesn't work well (focus disappears forever, probably because button thinks it is in the needed visual state already):
protected void onFocus(int direction) {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
_focusTimer = _app.invokeLater(new Runnable() {
public void run() {
MyButton.this.setBorder(BorderFactory.createSimpleBorder(EDGES));
MyButton.this.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.GREEN, 0));
invalidate();
_focusTimer = -1;
}
}, FOCUS_DURATION, false);
super.onFocus(direction);
}
UPDATE 2:
A try with a NullField, still not working properly:
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.image.*;
public class MyFocus extends UiApplication {
public static void main(String[] args) {
MyFocus app = new MyFocus();
app.enterEventDispatcher();
}
public MyFocus() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
static NullField NF = new NullField();
HorizontalFieldManager hfm = new HorizontalFieldManager() {
int lastFocused = -1;
protected int firstFocus(int direction) {
lastFocused = super.firstFocus(direction);
System.out.println("XXX firstFocus: " + lastFocused);
return lastFocused;
}
protected int nextFocus(final int direction, final int axis) {
if (getFieldWithFocus() == NF) {
return lastFocused;
}
lastFocused = super.nextFocus(direction, axis);
System.out.println("XXX nextFocus: " + lastFocused);
return lastFocused;
}
};
public MyScreen() {
setTitle("2nd trackpad click not working");
getMainManager().setBackground(BackgroundFactory.createLinearGradientBackground(Color.WHITE, Color.GRAY, Color.DARKGRAY, Color.GRAY));
hfm.add(new MyButton(MyButton.WEST));
hfm.add(new MyButton(MyButton.EAST));
hfm.add(new MyButton(MyButton.EAST));
hfm.add(NF);
add(hfm);
}
class MyButton extends ButtonField {
public final static int EAST = 0;
public final static int WEST = 1;
public final static int WIDTH = 100;
public final static int HEIGHT = 100;
private final XYEdges EDGES = new XYEdges(0, 0, 0, 0);
private final Application _app = UiApplication.getUiApplication();
private final static long FOCUS_DURATION = 3000L;
private int _focusTimer = -1;
private final int _direction;
public MyButton(int direction) {
setMargin(EDGES);
setPadding(EDGES);
setImageSize(WIDTH, HEIGHT);
setBorder(VISUAL_STATE_NORMAL, BorderFactory.createSimpleBorder(EDGES));
setBorder(VISUAL_STATE_FOCUS, BorderFactory.createSimpleBorder(EDGES));
setBorder(VISUAL_STATE_ACTIVE, BorderFactory.createSimpleBorder(EDGES));
setBackground(VISUAL_STATE_NORMAL, BackgroundFactory.createSolidTransparentBackground(Color.GREEN, 0));
setBackground(VISUAL_STATE_FOCUS, BackgroundFactory.createSolidTransparentBackground(Color.BLUE, 100));
setBackground(VISUAL_STATE_ACTIVE, BackgroundFactory.createSolidTransparentBackground(Color.RED, 200));
_direction = direction;
switch(_direction) {
case EAST:
setImage(ImageFactory.createImage("east.png"));
break;
case WEST:
setImage(ImageFactory.createImage("west.png"));
break;
}
}
protected boolean touchEvent(TouchEvent event) {
if (event.getEvent() == TouchEvent.CLICK) {
applyThemeOnStateChange();
return true;
}
return super.touchEvent(event);
}
protected void onUnfocus() {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
super.onUnfocus();
}
protected void onFocus(int direction) {
if (_focusTimer != -1) {
_app.cancelInvokeLater(_focusTimer);
_focusTimer = -1;
}
_focusTimer = _app.invokeLater(new Runnable() {
public void run() {
MyScreen.NF.setFocus();
_focusTimer = -1;
}
}, FOCUS_DURATION, false);
super.onFocus(direction);
}
public int getPreferredHeight(){
return HEIGHT;
}
public int getPreferredWidth(){
return WIDTH;
}
protected void layout(int width, int height) {
setExtent(WIDTH, HEIGHT);
}
}
}
I would probably ovverride the drawFocus() method to check a flag _showFocus before calling super.drawFocus(), then in onFocus(), set the flag to true and schedule the Runnable to change the _showFocus flag to false and invalidate (also setting it to false in onUnfocus()). Don't have to worry about canceling timers or anything this way either, as it is just going to change a flag and invalidate, keeping it in the appropriate state.

How can I display scroll text like marque in Blackberry using J2ME?

How can I display scroll like marquee text in Blackberry using J2ME? That moves from left to right or vertically?
Any help will be very much appreciated.
its really easy to display.
check the code below .
import java.util.Timer;
import java.util.TimerTask;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class MarqueeApp extends UiApplication {
public MarqueeApp() {
pushScreen(new MarqueeScreen());
}
public static void main(String[] args) {
(new MarqueeApp()).enterEventDispatcher();
}
}
class MarqueeScreen extends MainScreen {
public MarqueeScreen() {
setTitle(new LabelField("hi"));
MarqueeLabel testLabel1 = new MarqueeLabel("This is a marquee",
Field.FOCUSABLE);
add(testLabel1);
MarqueeLabel testLabel2 = new MarqueeLabel("This is a long long " +
"long long long long long long long long long long long " +
"long long marquee", Field.FOCUSABLE);
add(testLabel2);
}
}
class MarqueeLabel extends LabelField {
int currentChar = 0;
String currentText = null;
Font ourFont;
private Timer _scrollTimer;
private TimerTask _scrollTimerTask;
public MarqueeLabel(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, 200);
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 + 4;
if (currentChar > fullText.length()) {
currentChar = 0;
}
invalidate();
}
};
_scrollTimer.scheduleAtFixedRate(_scrollTimerTask, 0, 300);
}
}
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);
}
}
}

Marquee Text on Blackberry

I want to marquee some text in a BlackBerry app.
Here it is. Just make a class with name MarqueeLabel and copy-paste this code then you can use this class to display a marquee text:
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);
}
}
}

Resources