Add A ticker to Blackberry Display - blackberry

im new to blackberry,So
Please give a blackberry code for adding ticker to blackberry display bottom part of the disply ??
Need Only for how to Create a Ticker.??
Thank you

try this
import java.util.Timer;
import java.util.TimerTask;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
public class Ticker extends Field {
String text;
final int screenWidth = Display.getWidth();
int offset = screenWidth;
Timer timer = new Timer();
final int delay = 30;
public void setText(String text) {
this.text = text;
}
public String getText() {
return text;
}
Ticker(String text) {
this.text = text;
final int width = Font.getDefault().getAdvance(text);
TimerTask timerTask = new TimerTask() {
public void run() {
offset--;
if (offset + width == 0) {
offset = screenWidth;
}
invalidate();
}
};
timer.scheduleAtFixedRate(timerTask, delay, delay);
}
protected void layout(int width, int height) {
int w = Display.getWidth();
int h = Font.getDefault().getHeight();
setExtent(w, h);
}
protected void paint(Graphics graphics) {
graphics.drawText(text, offset, 0);
}
}

Related

Setting selected value in editfield?

I have made a custom auto suggest list.
I want to set the selcted value in edit field.
I am using this Place holder text on a AutoCompleteField in blackberry
CustomAutoCompleteField.js
package mypackage;
import net.rim.device.api.collection.util.BasicFilteredList;
import net.rim.device.api.collection.util.BasicFilteredListResult;
import net.rim.device.api.system.Application;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.AutoCompleteField;
import net.rim.device.api.ui.component.ListField;
class CustomAutoCompleteField extends AutoCompleteField {
private int yOffset = 0;
private int xOffset = 0;
public CustomAutoCompleteField(BasicFilteredList filteredList) {
super(filteredList);
}
protected void paint(Graphics g) {
super.paint(g);
if (xOffset == 0) {
// initialize text offsets once
xOffset = getEditField().getContentLeft();
yOffset = getEditField().getContentTop();
}
String text = getEditField().getText();
if (text == null || text.length() == 0) {
int oldColor = g.getColor();
g.setColor(Color.GRAY);
g.drawText("enter text", xOffset, yOffset);
g.setColor(oldColor);
}
}
public void onSelect(Object selection, int SELECT_TRACKWHEEL_CLICK) {
ListField _list = getListField();
if (_list.getSelectedIndex() > -1) {
final String selectedText = getEditField().getText();
if(selectedText!=null){
final BasicFilteredListResult result = (BasicFilteredListResult) selection;
Application.getApplication().invokeLater(new Runnable() {
public void run() {
selectedText.setText(result._object.toString());
}
});
// selectedText.setText(result._object.toString());
}
}
}
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
super.sublayout(250, 250);
}
}
myscreen.js
public final class MyScreen extends MainScreen
{
/**
* Creates a new MyScreen object
*/
public MyScreen()
{
// Set the displayed title of the screen
BasicFilteredList filterList = new BasicFilteredList();
String[] days = {"Monday(TAS)","Tuesday-(PAQ)","Wednesday-(MAN)",
"Thursday","Friday","Saturday","Sunday(I_)"};
filterList.addDataSet(1,days,"days",BasicFilteredList.COMPARISON_IGNORE_CASE);
HorizontalFieldManager hr =new HorizontalFieldManager();
LabelField userName= new LabelField("hjsdhas");
hr.add(userName);
CustomAutoCompleteField autoCompleteField = new CustomAutoCompleteField(filterList);
hr.add(autoCompleteField);
add(hr);
}
}
CustomAutoCompleteField.java is my custom java class and I call the class from myscreen.java class

How to create a custom drop down in blackberry

Can anyone please help me to create a custom drop down similar to the image attached in blackberry 5.0. I have used object choice field but it is coming along with label. Guide me to create custom drop down as show in image below
I have created my customized ChoiceField, using popup Screen. Here is the below code sample
package com.src.java.rim.ui;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;
public class FWCustomChoiceField extends HorizontalFieldManager {
/*space between text and icon*/
final int padding = 10;
String arrowBitmapName = "arrow_state_grey_right.png";
Object choice[] = null;
int index = -1;
String text = "Please select one option";
ListField choiceField = null;
public FWCustomChoiceField(final Object choice[]) {
this.choice = new Object[choice.length];
this.choice = choice;
choiceField = new ListField(){
protected boolean navigationClick(int status, int time) {
Field focus = UiApplication.getUiApplication().getActiveScreen() .getLeafFieldWithFocus();
if (focus instanceof ListField) {
ChoicePopupScreen popup = new ChoicePopupScreen(10, 80, choice);
popup.setChoice(choice);
UiApplication.getUiApplication().pushScreen(popup);
}
return super.navigationClick(status, time);
}
};
choiceField.setSize(1);
choiceField.setCallback(new TestListCallback());
add(choiceField);
}
public void setSelIndex(int index){
this.index = index;
this.text = choice[index].toString();
choiceField.invalidate();
}
public int getSelectedIndex(){
return index;
}
final class TestListCallback implements ListFieldCallback {
TestListCallback() {
}
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
Bitmap bitmap = Bitmap.getBitmapResource(arrowBitmapName);
int fontWidth = getFont().getAdvance(text);
int width = Display.getWidth() ;
int posX = (width-(fontWidth + padding + bitmap.getWidth()))/2;
int height = list.getRowHeight();
int posY = (height - bitmap.getHeight())/2;
g.drawText(text, posX, y, 0, w);
g.drawBitmap(posX+fontWidth+padding,posY, bitmap.getWidth(), bitmap.getHeight(), bitmap, 0, 0);
}
public Object get(ListField listField, int index) {
return null;
}
public int getPreferredWidth(ListField listField) {
return Graphics.getScreenWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
return listField.indexOfList(prefix, start);
}
}
public class ChoicePopupScreen extends PopupScreen {
Object []choice = null;
/*holds the position for popup screen*/
private int _posX = 0;
private int _posY = 0;
public ChoicePopupScreen(int posx,int posy, Object[] choice) {
/*calling super class constructor*/
super(new VerticalFieldManager(), Field.FOCUSABLE);
this.setMargin(new XYEdges(1,1,1,1));
this.choice = new Object[choice.length];
this.choice = choice;
/*Setting position for popup screen*/
_posX = posx;
_posY = posy;
/*list field customized to display as choice picker*/
ListField choiceList = new ListField(){
/*list field event handling using navigation click*/
protected boolean navigationClick(int status, int time) {
/*getting the selected list index value*/
int index = this.getSelectedIndex();
/*returning the selected index to the main field*/
setSelIndex(index);
/*removing the popup screen from the screen stack*/
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
/*required for event handling*/
return super.navigationClick(status, time);
}
};
/*displays the message when list is empty*/
choiceList.setEmptyString("List is empty", DrawStyle.LEFT);
choiceList.setSize(choice.length);
choiceList.setCallback(new PopUpListCallback());
add(choiceList);
Border border = BorderFactory.createSimpleBorder( new XYEdges(), Border.STYLE_TRANSPARENT);
this.setBorder(border);
}
protected void setChoice(Object []choice) {
this.choice = new Object[choice.length];
this.choice = choice;
}
protected void sublayout(int width, int height) {
super.sublayout(width, height);
/*setting the position for the popup screen*/
setPosition(_posX , _posY);
}
}
private class PopUpListCallback implements ListFieldCallback {
PopUpListCallback() {
}
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
String text = choice[index].toString();
g.drawText(text, padding, y, 0, w);
}
public Object get(ListField listField, int index) {
return null;
}
public int getPreferredWidth(ListField listField) {
return Graphics.getScreenWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
return listField.indexOfList(prefix, start);
}
}
}
To use the above class
String choicestrs[] = {"Opt 1", "Opt 2", "Opt 3"};
add(new FWCustomChoiceField(choicestrs));

BasicEditField 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();
}
}
}

Splash screen with progress indicator on Blackberry

I am trying to design a splash screen with an activity indicator on it.
I am successfully creating a splash screen but my indicator is not being added to it,
it is being hidden below the image while i am setting its layout.
How can I fix this?
Here is my code:
package mypackage;
import java.util.Timer;
import java.util.TimerTask;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.progressindicator.ActivityIndicatorController;
import net.rim.device.api.ui.component.progressindicator.ActivityIndicatorModel;
import net.rim.device.api.ui.component.progressindicator.ActivityIndicatorView;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
//import net.rim.device.api.ui.extension.container.EyelidFieldManager;
public final class SplashScreen extends SplashScreenPage
{
public SplashScreen()
{
super(Bitmap.getBitmapResource("splash-blackberry.png"),2);
}
}
class SplashScreenPage extends MainScreen
{
VerticalFieldManager vfm= new VerticalFieldManager(Field.FIELD_VCENTER);
HorizontalFieldManager hfm = new HorizontalFieldManager(Field.FIELD_VCENTER)
{
protected void sublayout( int maxWidth, int maxHeight )
{
super.sublayout(360,60);
setExtent(360, 60);
Field field = getField(0);
layoutChild(field,140, 60);
setPositionChild(field, 100,200);
}
public void paint(Graphics graphics)
{
//Draw the background image and then call paint.
// super.paint(graphics);
//graphics.clear();
// graphics.setBackgroundColor(0x000000);
//graphics.drawBitmap(0, 0, 360, 480, popup, 0, 0);
// super.paint(graphics);
// graphics.clear();
// graphics.drawBitmap(0, 0, 360, 480, popup, 0, 0);
}
};
ActivityIndicatorView view = new ActivityIndicatorView(Field.USE_ALL_WIDTH);
ActivityIndicatorModel model = new ActivityIndicatorModel();
ActivityIndicatorController controller = new ActivityIndicatorController();
boolean notlogged = false;
Bitmap popup;
SplashScreenPage screen1 = this;
private Timer splashTimer = new Timer();
private TimerTask splashTask;
int count = 0;
int screenWidth = Display.getWidth();
int screenHeight = Display.getHeight();
int yCoord;
int xCoord;
boolean showSplash = true;
boolean splashDisplayed = false;
//SplashScreen page is here
public SplashScreenPage(Bitmap popup, final int seconds)
{
view.setController(controller);
view.setModel(model);
view.setLabel("Loading");
controller.setModel(model);
controller.setView(view);
model.setController(controller);
Bitmap bitmap = Bitmap.getBitmapResource("images.jpeg");
view.createActivityImageField(bitmap, 5, Field.FIELD_BOTTOM);
// add(view);
add(hfm);
hfm.add(view);
// add(vfm);
// vfm.add(Bitmap.getBitmapResource("splash-blackberry.png"));
this.popup = popup;
xCoord = (screenWidth - popup.getWidth()) / 2;
yCoord = (screenHeight - popup.getHeight()) / 2;
splashTask = new TimerTask() {
public void run() {
if (showSplash && !splashDisplayed) {
count++;
if (count == seconds * 15) {
showSplash = false;
splashDisplayed = true;
splashTimer.cancel();
invalidate();
synchronized (Application.getEventLock()){
UiApplication.getUiApplication().pushScreen(new secondscreen());
notlogged = true;
}
}
}
}
};
splashTimer.scheduleAtFixedRate(splashTask, 100, 100);
}
protected void paint(Graphics graphics) {
super.paint(graphics);
if (showSplash && !splashDisplayed) {
graphics.drawBitmap(xCoord, yCoord, popup.getWidth(), popup.getHeight(), popup, 0, 0);
// super.paint(graphics);
// graphics.clear();
}
}
protected void onUiEngineAttached(boolean attached) {
showSplash = true;
invalidate();
super.onUiEngineAttached(attached);
}
protected boolean navigationMovement(int dx, int dy, int status, int time) {
return DismissSplash();
}
protected boolean navigationClick(int status, int time) {
return DismissSplash();
}
protected boolean keyChar(char c, int status, int time) {
return DismissSplash();
}
private boolean DismissSplash() {
if (showSplash) {
showSplash = false;
splashDisplayed = true;
invalidate();
return true;
}else{
return false;
}
}
public void onExposed() {
if( notlogged)
UiApplication.getUiApplication().popScreen(this);
}
}
In your HorizontalFieldManager that you are adding view to, you need to make sure you are calling super.paint() because it called subpaint() which will tell its child Fields (view in this case) to paint themselves. I see that you have it commented out, so you've probably been messing with that. Also, be sure that you do your background painting before you call super.paint() so you don't cover up the ActivityIndicatorView with your own painting.
Would it be possible for you to go through and clean up the code (removing the commented out code that isn't important) to a point where you would expect it to work so it is a littler easier to distinguish between "trying anything you can" code and "working" code.

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);
}
}
}

Resources