Scrollable field with non-editable text - blackberry

In my app I need a scrollable field with non-editable text with custom size. I'm trying to do this like below:
final int sx2 = Display.getWidth()-Display.getWidth()*2/51-kButtonWidgh;
final int sy2 = Display.getHeight()*7/12;
VerticalFieldManager vfm3 = new VerticalFieldManager()
{
public void sublayout( int maxWidth, int maxHeight )
{
super.sublayout(sx2, sy2);
}
};
text = new TextField((Field.FOCUSABLE & Field.READONLY & Field.STATUS_MOVE_FOCUS_HORIZONTALLY)){
public void layout( int maxWidth, int maxHeight ) {
super.layout(sx2, sy2); //my custom frame
}
};
vfm3.add(text);
vfm2.add(vfm3);
But it editable, and not scrollable. Text prints from beginning field and ends far away from it's frame. there is cursor, which goes out of field frame, and there is no scrolling. How should I do this?

try this:
EditField _text = new EditField(Field.HORIZONTAL_SCROLL|Field.VERTICAL_SCROLL);
_text.setEditable(false);

Related

Scroll only single listfield in multiple listfields blackberry

I'm new to blackberry and I've a question, I've 3 listfields in a row like
[--------One--------][--Two--][--Three--]
but when I scroll single one, every one scrolls!, how do I restrict scroll of others when focused one is scrolling?
EDIT
// ListFields
HorizontalFieldManager hfmMain = new HorizontalFieldManager();
HorizontalFieldManager hfmFist = new HorizontalFieldManager(FIELD_LEFT);
hfmFist.add(myListView);
HorizontalFieldManager hfmSecond = new HorizontalFieldManager();
hfmSecond.add(hizabListView);
HorizontalFieldManager hfmThird = new HorizontalFieldManager();
hfmThird.add(paraListView);
hfmMain.add(hfmFist);
hfmMain.add(hfmSecond);
hfmMain.add(hfmThird);
add(hfmMain);
The key is that you need to disable vertical scrolling for the Screen that contains all these managers and fields.
Then, you can create one horizontal field manager. And then, three vertical field managers. Put each list in its own vertical field manager, and then all three vertical field managers go into the horizontal field manager.
Here's a simple prototype that I tested:
public class ListFocusScreen extends MainScreen implements ListFieldCallback {
private ObjectListField list1;
private ListField list2;
private ListField list3;
private Bitmap icon2; // for list 2 cell background
private Bitmap icon3; // for list 3 cell background
public ListFocusScreen() {
// Do NOT allow vertical scrolling at the Screen level!!
super(MainScreen.NO_VERTICAL_SCROLL | MainScreen.NO_VERTICAL_SCROLLBAR);
// A container for the "row" of three side-by-side lists
HorizontalFieldManager hfm = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
// Do create a vertical field manager for each list, that scrolls
VerticalFieldManager vfm1 = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR) {
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(2 * Display.getWidth() / 3, maxHeight); // 2/3 width
}
};
VerticalFieldManager vfm2 = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR) {
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth() / 6, maxHeight); // 1/6 width
}
};
VerticalFieldManager vfm3 = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR) {
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth() / 6, maxHeight); // 1/6 width
}
};
Object[] listData1 = new Object[24];
for (int i = 0; i < listData1.length; i++) {
// generate fake data for list1
listData1[i] = String.valueOf(i) + ". Click to Download";
}
list1 = new ObjectListField();
list1.set(listData1);
list2 = new ListField();
list2.setCallback(this);
icon2 = Bitmap.getBitmapResource("octagon.png");
list2.setSize(15);
list3 = new ListField();
list3.setCallback(this);
icon3 = Bitmap.getBitmapResource("frame.png");
list3.setSize(15);
vfm1.add(list1);
vfm2.add(list2);
vfm3.add(list3);
hfm.add(vfm1);
hfm.add(vfm2);
hfm.add(vfm3);
add(hfm);
}
public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
// this same method will be used for custom drawing of both lists 2 and 3
final int PAD = 4;
String text = (String)get(listField, index);
if (listField == list2) {
graphics.drawBitmap(0, y, width, width, icon2, 0, 0);
graphics.drawText(text, PAD, y + PAD);
} else if (listField == list3) {
graphics.drawBitmap(0, y, width, width, icon3, 0, 0);
graphics.drawText(text, PAD, y + PAD);
}
}
public Object get(ListField listField, int index) {
// TODO: normally, get this value from a vector of actual
// data for each list
return String.valueOf(index);
}
public int getPreferredWidth(ListField listField) {
return Display.getWidth() / 6;
}
public int indexOfList(ListField listField, String prefix, int start) {
return -1; // no search support
}
}
Results
As you can see, I was able to get each list scrolling vertically, independently.
You have several list fields into one screen manager and when you scroll down and when this manager is selected, then the scroll event is being sent to all these fields. And all of them are scrolling simultaneously.
I would separate every listfield into its own manager.

Custom layout in blackberry

I need a custom layout as below in BlackBerry.
I did same layout in Android. Now I need same layout in BlackBerry. I am new to BlackBerryapp development. The Fields of BlackBerry like Views in Android seem to be very confusing things to me.
I tried with VerticalFieldManager & HorizontalFieldManager by mixing these with BitmapField & LabelField to produce my layout.
I failed particularly in placing LabelField at bottom of screen. I used USE_ALL_HEIGHT & FIELD_BOTTOM style to put at bottom, but it is showing after scrolling long time.
My requirement is the header and footer should not scroll when my middle list is scrolling.
The easiest way to add header and footer fields that don't scroll with the content in the middle of the screen is to use MainScreen#setBanner() and MainScreen#setStatus().Here's an example:
public class HeaderFooterListScreen extends MainScreen {
private static final int BG_COLOR = Color.BLACK;
private static final int HIGHLIGHT_COLOR = Color.BLUE;
private static final int FONT_COLOR = Color.WHITE;
private static final int ROW_HEIGHT = 60;
private Object[] _rowData;
private Field _header;
private Field _footer;
private Field _spacer;
private int _orientation;
public HeaderFooterListScreen() {
super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
Background bg = BackgroundFactory.createSolidBackground(BG_COLOR);
setBackground(bg);
getMainManager().setBackground(bg);
// header
Bitmap headerImg = Bitmap.getBitmapResource("header.png");
_header = new BitmapField(headerImg);
setBanner(_header);
// list
_rowData = new Object[] { "row one", "row two", "row three" }; //, "row four", "row five", "row six", "row seven", "row eight", "row nine", "row ten" };
ListField list = new ListField();
int c = Color.RED;
XYEdges edgeColors = new XYEdges(c, c, c, c);
XYEdges edgeThicknesses = new XYEdges(5, 5, 5, 5);
list.setBorder(BorderFactory.createSimpleBorder(edgeThicknesses, edgeColors, Border.STYLE_SOLID));
list.setCallback(new CustomListFieldCallback());
list.setRowHeight(ROW_HEIGHT);
list.setSize(_rowData.length);
add(list);
// footer
_footer = new LabelField("Footer Showing Status As Text", Field.USE_ALL_WIDTH | DrawStyle.HCENTER) {
public void paint(Graphics g) {
// change font color
int oldColor = g.getColor();
g.setColor(FONT_COLOR);
super.paint(g);
g.setColor(oldColor);
}
};
_footer.setFont(_footer.getFont().derive(Font.PLAIN, 24));
setStatus(_footer);
}
private void centerList() {
if (_spacer != null && _spacer.getManager() != null) {
// delete the old spacer field, if there was one
delete(_spacer);
}
int listHeight = _rowData.length * ROW_HEIGHT;
int availableHeight = getHeight() - _footer.getHeight() - _header.getHeight();
if (availableHeight > listHeight) {
boolean firstRun = (_spacer == null);
// add a spacer above the list to force it down enough to be centered
final int SPACE = (availableHeight - listHeight) / 2;
_spacer = new Field() {
protected void layout(int width, int height) {
setExtent(width, SPACE);
}
protected void paint(Graphics graphics) {
}
};
insert(_spacer, 0);
if (firstRun) {
getMainManager().setVerticalScroll(0);
}
}
}
// called when device orientation changes
protected void sublayout(int width, int height) {
super.sublayout(width, height);
if (_orientation != Display.getOrientation()) {
_orientation = Display.getOrientation();
// run with invokeLater() to avoid recursive sublayout() calls
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// TODO: may have to adjust header, too?
centerList();
}
});
}
}
private class CustomListFieldCallback implements ListFieldCallback {
private final int PAD = 10;
public void drawListRow(ListField listField, Graphics graphics,
int index, int y, int width) {
int oldColor = graphics.getColor();
if (listField.getSelectedIndex() == index) {
graphics.setColor(HIGHLIGHT_COLOR);
} else {
graphics.setColor(BG_COLOR);
}
graphics.fillRect(0, y, width, listField.getRowHeight());
graphics.setColor(FONT_COLOR);
String text = (String)get(listField, index);
graphics.drawText(text, PAD, y + PAD, DrawStyle.LEFT);
graphics.setColor(oldColor);
}
public Object get(ListField listField, int index) {
return _rowData[index];
}
public int getPreferredWidth(ListField listField) {
return Display.getWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
return -1; // TODO?
}
}
}
You didn't specify how you wanted the list in the middle to work, so I just made some guesses. I also wasn't sure if the red border was something you wanted, or just something you used to describe your layout. Edit your question, or post a new question, if you have more requirements for the list.
Field Concepts
If you're coming from Android, and are unclear about the role of BlackBerry UI classes, like Fields and Managers, here's some resources:
another Stack Overflow answer I posted
BlackBerry Advanced UI Sample Code on Github
BlackBerry Layout Managers Tutorial
Results

How to display text in multiline using customized label field in Blackberry?

How to display text in multi-line using customized label field in Blackberry?
When I use following code, I can display the label with desired font size, however the problem is when the extent is limited in width the text gets cut off and not displayed entirely , also rest of the label part is not displayed on next line.
Any Help here is appreciated.
Following is my code for customized label field.
public class GrayBgLabelField extends LabelField {
private int width = Display.getWidth();
private int height = 40;
private String label;
private Font font;
public GrayBgLabelField(){
super();
}
public GrayBgLabelField(String label, int fieldWidth){
super(label, 0 );
this.label = label;
width = fieldWidth;
}
public GrayBgLabelField(String label, int fieldWidth , long style){
super( label, style );
this.label = label;
width = fieldWidth;
}
public GrayBgLabelField(String label, int fieldWidth , int fieldHeight){
this(label,fieldWidth);
this.label = label;
height = fieldHeight;
}
public GrayBgLabelField(String label, int fieldWidth , int fieldHeight, long style){
super( label, style );
this.label = label;
width = fieldWidth;
height = fieldHeight;
}
protected void layout( int maxWidth, int maxHeight)
{
super.layout( width, height);
setExtent( width, height);
}
protected void paint(Graphics g) {
if(font !=null){
g.setFont(font);
}
if (label.length() != 0) {
g.drawText(label, width/30, height/4);
}
g.setColor(Color.BLACK);
}
public void setFont(int f){
font = this.getFont().derive(f);
}
public void setFont(Font font){
this.font = font;
}
protected void paintBackground(Graphics g) {
int oldColor = g.getColor();
try {
g.setColor(0xF5F6F8); // Gray-DDDDDD color code.
g.fillRect(0, 0, getWidth(), getHeight());
} finally {
g.setColor(oldColor);
}
}
}
Override methods which control width and height of your custom field.
Here is the tutorial: "How to create a custom field"
And when you change your label field text, invalidate it via invalidate() method, to redraw field contents on the screen.

Editfield scroll fails to reach top in blackberry

I am having 2 EditFields in my login form with names Email: and Password:. Just below email I have login button. Suppose I come down till login, I can scroll back only till password field.The cursor fails to reach Email field. In simulator, I tried using arrow keys as well as trackpad. Please help how to scroll back to first editfield
AbsoluteFieldManager ab = new AbsoluteFieldManager();
add(ab);
new SeparatorField();
et=new EditField("Email-id:","");
pwd=new PasswordEditField("Password:","");
ab.add(et,35,110);
ab.add(pwd,35,150);
I am using AbsoluteFieldManager and developing for OS 6.0. I want the loginscreen to look like facebook login page.
Kindly let me know what can possibly be the reason for not able to scroll up
Maybe it is a RIM bug with the AbsoluteFieldManager. Never used it before so I don't know about it. You can create a work around to solve this problem. Find it below:
et=new EditField("Email-id:","");
pwd=new PasswordEditField("Password:","") {
protected int moveFocus(int amount, int status, int time) {
int cursorPosition = this.getCursorPosition();
if ((cursorPosition == 0) && (amount < 0)) {
et.setFocus();
return 0;
}
else {
return super.moveFocus(amount, status, time);
}
}
};
In this way, when you arrive to the first element in the password edit field, you will oblige the email field to get focused. This will work for you as a work around.
Another way to solve the problem is to add the two fields in an horizontal field manager, in that way I guess this will work for you for sure. If not use the first method. You can find below the code for HorizontalFieldManager:
et=new EditField("Email-id:","");
pwd=new PasswordEditField("Password:","");
HorizontalFieldManager manager = new HorizontalFieldManager();
manager.add(et);
manager.add(pwd);
ab.add(manager, yourX, yourY);
It also may be a RIM bug. What OS do you use? Is it OS 5+? Do you use custom paddings/margins/borders for some of the UI elements on the screen (including the screen itself)? If yes, try to comment out any code that sets paddings/margins/borders to check whether this it the case.
You can use this code for your login page:
public class loginscreen extends MainScreen implements FieldChangeListener {
private int deviceWidth = Display.getWidth();
private int deviceHeight = Display.getHeight();
private VerticalFieldManager subManager;
private VerticalFieldManager mainManager;
public long mycolor = 0x00FFFFFF;
Screen _screen = home.Screen;
TextField heading = new TextField(Field.NON_FOCUSABLE);
TextField username_ef = new TextField();
PasswordEditField password_ef = new PasswordEditField();
CheckboxField rememberpass = new CheckboxField();
public ButtonField login_bt = new ButtonField("Login", ButtonField.CONSUME_CLICK);
public ButtonField register_bt = new ButtonField("Register", ButtonField.CONSUME_CLICK);
public loginscreen()
{
super();
final Bitmap backgroundBitmap = Bitmap.getBitmapResource("bgd.png");
HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR )
{
protected void sublayout(int width, int height)
{
Field field;
int numberOfFields = getFieldCount();
int x = 245;
int y = 0;
for (int i = 0;i < numberOfFields;i++)
{
field = getField(i);
setPositionChild(field,x,y);
layoutChild(field, width, height);
x +=_screen.getWidth()-381;
y += 0;//l17
}
width=_screen.getWidth();
height=48;//w19
setExtent(width, height);
}
};
mainManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR )
{
public void paint(Graphics graphics)
{
graphics.clear();
graphics.drawBitmap(0, 0, deviceWidth, deviceHeight, backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
//this manger is used for adding the componentes
subManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR )
{
protected void sublayout( int maxWidth, int maxHeight )
{
int displayWidth = deviceWidth;
int displayHeight = deviceHeight;
super.sublayout( displayWidth, displayHeight);
setExtent( displayWidth, displayHeight);
}
public void paint(Graphics graphics)
{
graphics.setColor((int) mycolor);
super.paint(graphics);
}
};
username_ef.setLabel("Username: ");
password_ef.setLabel("Password: ");
rememberpass.setLabel("Remember Password");
heading.setLabel("Please enter your credentials: ");
username_ef.setMaxSize(8);
password_ef.setMaxSize(20);
subManager.add(heading);
subManager.add(username_ef);
subManager.add(password_ef);
subManager.add(rememberpass);
subManager.add(new SeparatorField());
login_bt.setChangeListener(this);
register_bt.setChangeListener(this);
hfm.add(login_bt);
hfm.add(register_bt);
subManager.add(hfm);
mainManager.add(subManager);
this.add(mainManager);
}
public boolean onSavePrompt()
{
return true;
}
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
if(field == login_bt)
{
//do your code for login button click
}
if(field == register_bt)
{
//code for register button click
}
}}
What you have described is not normal behavior.
My conclusion is that your code has one or more bugs, in order to solve your problem you should modify your code to fix the bugs. You will then be able to scroll up and down through the various fields.
note: As this question stands it's not possible for me to be more specific about the exact bugs. So instead I will show you an example of the layout you described that would scroll properly and you can use as a default to determine which of your deviations have caused your bugs.
// inside MainScreen constructor
add(new EditField("Username:","",0));
add(new EditField("Password:","",0));
add(new ButtonField(buttonBMP,ButtonField.CONSUME_CLICK));

Blackberry VerticalFieldManager with fixed size : Scroll issue

I'm trying to have a full screen UI with a fix header ( a manager with some fields) and a scrollable contents (a list of custom field). The idea is to emulate a kind of scrollable list.
For this I made a custom VerticalFieldManager that accept a maxHeight (the screen height - the header height).
I got the following problems:
The scroll arrows do not show up (ever)
On OS 4.7 (Storm), I can scroll lower that the last item, until having nothing on my screen but the header.
My code need to compile with the JDE 4.2.1 & 4.7 and to run on Pearl and Storm. (at worst I could have two version of this class)
I suspect the two problems are related. I probably do something wrong. I looked at a few example/forum and always found similar solution/code.
Do you guys can tell me what I did wrong?
/**
* custom class, so we can set a max height (to keep the header visible)
*/
class myVerticalFieldManager extends VerticalFieldManager{
private int maxHeight = 0;
myVerticalFieldManager(int _maxHeight){
super(
//this provoc an "empty scrollable zone" on Storm
// but if you don't put it, on other OS, the vertical manager does not scroll at all.
Manager.VERTICAL_SCROLL
| Manager.VERTICAL_SCROLLBAR
);
maxHeight = _maxHeight;
}
protected void sublayout(int width, int height){
super.sublayout(width, getPreferredHeight());
setExtent(width, getPreferredHeight());
}
public int getPreferredWidth() {
return Graphics.getScreenWidth();
}
/**
* allow the manager to use all the given height. (vs auto Height)
*/
public boolean forceMaxHeight = false;
public int getPreferredHeight() {
if (forceMaxHeight) return maxHeight;
int m = super.getPreferredHeight();
if (m > maxHeight) m = maxHeight;
return m;
}
////////////////////////////////////////////////////////////////////////////
protected boolean isUpArrowShown(){
//TODO: does not seem to work (4.2.1 emulator & 4.5 device). (called with good return value but the arrows are not painted)
int i = getFieldWithFocusIndex();
//Trace("isUpArrowShown " + i);
return i > 0;
// note: algo not correct, cause the up arrow will be visible event when no field are hidden.
// but not so bad, so the user "know" that he can go up.
}
protected boolean isDownArrowShown(){
int i = getFieldWithFocusIndex();
return i < getFieldCount();
}
////////////////////////////////////////////////////////////////////////////
// note : since 4.6 you can use
// http://www.blackberry.com/developers/docs/4.6.0api/net/rim/device/api/ui/decor/Background.html
public int myBackgroundColor = 0xffffff;
protected void paint(Graphics g){
g.setBackgroundColor(myBackgroundColor);
// Clears the entire graphic area to the current background
g.clear();
super.paint(g);
}
}
any helps is welcome.
so,
I came with this workaround for the "empty scrollable zone" problem on STORM
it's ugly and doesn't allow a custom ScrollChangeListener, but it's working on Pearl & Storm
implements ScrollChangeListener
//in constructor:
setScrollListener(null);
setScrollListener(this);
private boolean MY_CHANGING_SCROLL = false;
public void scrollChanged(Manager manager, int newHorizontalScroll, int newVerticalScroll){
if (!MY_CHANGING_SCROLL){
MY_CHANGING_SCROLL = true;
myCheckVerticalScroll();
MY_CHANGING_SCROLL = false;
}
}
protected int myMaxVerticalScrollPosition(){
int vh = getVirtualHeight();
int h = getHeight();
if (vh < h ) return 0; // no scroll
return vh - h; // don't scroll lower than limit.
}
protected void invCheckVerticalScroll() {
int i = getVerticalScroll();
int m = myMaxVerticalScrollPosition();
if ( i > m){
i = m;
setVerticalScroll(i);
}
}
I'm still looking for a solution to the scroll arrows problem...
If anybody got an idea...
You can use the method setBanner() instead of add for your header. Then you can add a default VerticalFieldManager to the screen and it will scroll normally but won't hide the header. Note that the MainScreen delegate manager is a VerticalScrollManager so you might not need a second vfm.
HorizontalFieldManager hfm = new HorizontalFieldManager();
setBanner(hfm)
add(new ButtonField("Hello 1");
add(new ButtonField("Hello 2");
...
Hey i did the same thing using a HorizontalFieldManager that contains an image and a title
header_img = Bitmap.getBitmapResource("header.png");
title = new LabelField("Welcome",LabelField.FIELD_RIGHT);
header_manager = new HorizontalFieldManager()
{
protected void paint(net.rim.device.api.ui.Graphics graphics)
{
int y = this.getVerticalScroll();
graphics.drawBitmap( 0, y, header_img.getWidth(), header_img.getHeight(), header_img, 0, 0 );
graphics.setColor(Color.LEMONCHIFFON);
super.paint( graphics );
}
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(Display.getWidth(), 240);
Field field = title;
layoutChild(field, title.getWidth(), title.getHeight());
setPositionChild(field, (Display.getWidth()/2) -10, 13);
setExtent(Display.getWidth(),55);
}
};
header_manager.add(title);

Resources