center object in GridFieldManager on blackberry - blackberry

I want to center the objects in the GridFieldManager. This can be seen from the selected image in the blue zone like in the first picture. How can I center these objects (text+picture)???
This is my code
VerticalFieldManager manager = (VerticalFieldManager) getMainManager();
gfm = new GridFieldManager(rows, columns, GridFieldManager.FIXED_SIZE);
manager.add(gfm);
int columnWidth = (Display.getWidth() / columns)
- gfm.getColumnPadding();
for ( i = 0; i < columns; i++) {
gfm.setColumnProperty(i, GridFieldManager.FIXED_SIZE, columnWidth);
}
BitmapField[] images = new BitmapField[6];
EncodedImage Icon = null;
for (i = 0; i < 6; i++) {
Icon = EncodedImage
.getEncodedImageResource("img/HOME.png");
images[i] = new BitmapField(Icon.getBitmap(), Field.FIELD_HCENTER
| Field.FIELD_VCENTER | Field.FOCUSABLE) {
protected void layout(int width, int height) {
setExtent(getPreferredWidth()+20, getPreferredHeight() + 15);
}
protected void paint(Graphics graphics) {
super.paint(graphics);
graphics.drawText("text", 0,
getBitmapHeight(), 2, getBitmapWidth() + 20);
}
};
gfm.setPadding(10, 0, 0, 0);
gfm.setRowPadding(20);
images[i].setPadding(20, 10, 5, 10);
gfm.add(images[i]);
}
}

Try this sample code:
public class PictureScreen extends MainScreen implements FieldChangeListener
{
static EditField editField;
private Bitmap lockBit;
private BitmapField bitmapField[];
private int size=0, i=0;;
public PictureScreen()
{
lockBit=Bitmap.getBitmapResource("HOME.png");
size=10;
createGUI();
}
private void createGUI()
{
bitmapField=new BitmapField[size];
HorizontalFieldManager hr=null;
for(i=0;i<size;i=i+3)
{
hr=new HorizontalFieldManager(Field.FIELD_HCENTER);
for(int j=i;j<i+3;j++) //Here I am taking 3 images per line(Horizontal Manager);
{
if(j<size)
{
final String _label="Text: "+(j+1);
bitmapField[j]=new BitmapField(null,Field.FOCUSABLE)
{
protected void layout(int width, int height)
{
setExtent( 100,100);
}
protected void paint(Graphics g)
{
g.drawBitmap(50-24, 0,lockBit.getWidth(), lockBit.getHeight(), lockBit, 0, 0); //Here 24= image width is 48pixel so,48/2 and 50=setExtent( 100,100); width=100/2;
g.setFont(Font.getDefault().derive(Font.BOLD|Font.ITALIC, 18));
g.drawText(_label, 50-24, 50);
super.paint(g);
}
};
bitmapField[j].setChangeListener(this);
bitmapField[j].setPadding(10, 10, 10, 10);
hr.add(bitmapField[j]);
}
}
add(hr);
}
}
public void fieldChanged(Field field, int context)
{
for(int i=0;i<size;i++)
{
if(field==bitmapField[i])
{
Status.show("Buy the Full Version", 500);
}
}
}
}
Then I got like this:
Try this and see the comments in the class;

I thinks your image is not in center and its Transparent area is not align properly . If you want to do this in easy way than it have to use PictureBackgroundButtonField Custom class. In this class you can handle focus and unfocus using tow different images . Below is the class .
package com.picturebackgroundbuttonfield;
import net.rim.device.api.ui.*;
import net.rim.device.api.system.*;
public class PictureBackgroundButtonField extends Field
{
private String _label;
private int _labelHeight;
private int _labelWidth;
private Font _font;
private Bitmap _onPicture, _offPicture;
private Bitmap _currentPicture;
public PictureBackgroundButtonField(Bitmap onFocus, Bitmap offFocus, String text, long style)
{
super(style);
_onPicture = onFocus;
_offPicture = offFocus;
_font = getFont();
_label = text;
_labelHeight = _onPicture.getHeight();
_labelWidth = _onPicture.getWidth();
_currentPicture = _offPicture;
}
/**
* #return The text on the button
*/
String getText()
{
return _label;
}
/**
* Field implementation.
* #see net.rim.device.api.ui.Field#getPreferredHeight()
*/
public int getPreferredHeight()
{
return _labelHeight;
}
/**
* Field implementation.
* #see net.rim.device.api.ui.Field#getPreferredWidth()
*/
public int getPreferredWidth()
{
return _labelWidth;
}
/**
* Field implementation. Changes the picture when focus is gained.
* #see net.rim.device.api.ui.Field#onFocus(int)
*/
protected void onFocus(int direction)
{
_currentPicture = _onPicture;
// setFont(getFont().derive(Font.BOLD));
invalidate();
}
/**
* Field implementation. Changes picture back when focus is lost.
* #see net.rim.device.api.ui.Field#onUnfocus()
*/
protected void onUnfocus()
{
_currentPicture = _offPicture;
// setFont(getFont().derive(Font.PLAIN));
invalidate();
}
/**
* Field implementation.
* #see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
*/
protected void drawFocus(Graphics graphics, boolean on)
{
// Do nothing
}
/**
* Field implementation.
* #see net.rim.device.api.ui.Field#layout(int, int)
*/
protected void layout(int width, int height)
{
setExtent(Math.min( width, getPreferredWidth()),
Math.min( height, getPreferredHeight()));
}
/**
* Field implementation.
* #see net.rim.device.api.ui.Field#paint(Graphics)
*/
/**
* Overridden so that the Event Dispatch thread can catch this event
* instead of having it be caught here..
* #see net.rim.device.api.ui.Field#navigationClick(int, int)
*/
protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(1);
return true;
}
/*protected void paint(Graphics graphics) {
// TODO Auto-generated method stub
}*/
protected void paint(Graphics graphics)
{
graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
graphics.setBackgroundColor(Color.BLACK);
graphics.drawText(_label, 2, 0,
(int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ),
getWidth() - 6 );
}
}

Related

Title on mapfieldScreen in blackeberry is not visible

I am creating a map app. in which i am using mapfield. on the MapFieldScreen I am trying to add Title and also creating tabs but these two things are not visible. instead there is just gray color on these two places..
Here is my code.
class MapFieldScreen extends MainScreen
{
MapFieldScreen()
{
title= new LabelField("My Trip",LabelField.FIELD_HCENTER|LabelField.USE_ALL_WIDTH)
{
protected void layout(int width,int height)
{
setExtent(UIConstants.SCREEN_WIDTH, getFont().getHeight()*2);
}
public void paint(Graphics g)
{
g.setColor(Color.WHITE);
g.drawText(label,UIConstants.SCREEN_WIDTH*2/5,getFont().getHeight()/2);
super.paint(g);
}
};
setTitle(title);
mLoc= Bitmap.getBitmapResource(UIConstants.STOP);
mmMapField = new MapField();
add(mMapField);
}
}
You can check follwing code:
LabelField title = new LabelField("My Trip") {
int _width = Display.getWidth();
int _height = getFont().getHeight() * 2;
protected void layout(int width, int height) {
setExtent(_width, _height);
}
public void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
int xText = (_width - getFont().getAdvance(getText())) / 2;
int yText = (_height - getFont().getHeight()) / 2;
graphics.drawText(getText(), xText, yText);
}
};
setTitle(title);

BlackBerry: Button with Images for normal, focused and pressed state

I'd like to create an Image Button that has three states:
Normal
Focused
Pressed (or "down" or "active" whatever you call it)
Normal and Focused is pretty straightforward. I used the well-known classes BaseButtonField and BitmapButtonField as a bases. My Problem is that
protected boolean trackwheelClick(int status, int time)
is not called. My Button extends from Field and has Field.FOCUSABLE | Field.EDITABLE as styles. What am I missing?
You can try with "Tutorial: Creating a custom button" of the official RIM docs.
I think it is what your looking for
The below code is a custom button field for bottom menu bar. This will be useful for your task.
public class PictureBackgroundButtonField extends BitmapField {
MyTooltip _tooltip;
Bitmap mNormal;
Bitmap mFocused;
Bitmap mActive;
String text;
int mWidth;
int mHeight;
int xpos1;
public PictureBackgroundButtonField(String text,Bitmap normal, Bitmap focused, int xpos)
{
super(normal,FOCUSABLE);
mNormal = normal;
mFocused = focused;
mWidth = mNormal.getWidth();
mHeight = mNormal.getHeight();
this.text=text;
setMargin(0, 0, 0, 0);
setPadding(0, 0, 0, 0);
xpos1 = xpos;
}
public String getText()
{
return text;
}
public void setText(String text)
{
this.text=text;
}
protected void paint(Graphics graphics) {
Bitmap bitmap = mNormal;
if(isFocus())
{
bitmap = mFocused;
}
else
{
bitmap = mNormal;
}
graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmap, 0, 0);
}
protected void drawFocus(Graphics graphics, boolean on) {
}
protected void onFocus(int direction) {
//lbt.setText(text);
invalidate();
super.onFocus(direction);
if ( _tooltip != null ) {
_tooltip.removeToolTip();
_tooltip = null;
}
// Display tooltip at 50,50 for 5 seconds
_tooltip = MyTooltip.addToolTip(UiApplication.getUiApplication(), text, xpos1, 270, 1);
}
protected void onUnfocus() {
//lbt.setText("");
invalidate();
super.onUnfocus();
if ( _tooltip != null ) {
// We have displayed a Tooltip - remove it
_tooltip.removeToolTip();
_tooltip = null;
}
}
public int getPreferredWidth() {
return mWidth;
}
public int getPreferredHeight() {
return mHeight;
}
protected void layout(int width, int height) {
setExtent(mWidth, mHeight);
}
}
When a field is in the 'active' or 'pressed' state its visual state is set to Field.VISUAL_STATE_ACTIVE. If you check for this in your paint() method by calling Field.getVisualState() you'll be able to change how your button is displayed when it's pressed.

Not showing Transparent background?

Hi developers I have a screen with transparent background(bitmap). I have a list field placed on top of it, but the issue is the transparent background is not showing the previous screen, instead it shows white screen with listfield.
public class IndexScreen extends MainScreen implements ListFieldCallback {
private Vector rows;
ListField listItems;
int listItem;
int listFieldIndex = -1;
TableRowManager row;
Bitmap indexBox = Bitmap.getBitmapResource("res/screens/Index_Box.png");
public IndexScreen() {
listItems = new ListField() {
listItems.setRowHeight(20);
listItems.setCallback(this);
rows = new Vector();
listItems.setSize(rows.size());
_listVfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL) {
public void paint(Graphics g) {
g.drawBitmap(0, 0, UIinitialize.screenWidth,
UIinitialize.screenHeight, indexBox, 0, 0);
super.paint(g);
}
protected boolean navigationMovement(int dx, int dy, int status,
int time) {
return super.navigationMovement(dx, dy, status, time);
}
protected void sublayout(int maxWidth, int maxHeight) {
layoutChild(listItems, maxWidth, maxHeight);
setPositionChild(listItems, 5, 0);
setExtent(maxWidth, 255);
}
};
_listVfm.add(listItems);
add(_listVfm);
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
listItems.invalidate();
}
}, 500, true);
}
public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
TableRowManager rowManager = (TableRowManager) rows.elementAt(index);
rowManager.drawRow(graphics, 0, y, width, listItems.getRowHeight());
}
class TableRowManager extends Manager {
public TableRowManager() {
super(0);
}
public void drawRow(Graphics g, int x, int y, int width, int height) {
layout(width, 250);
setPosition(x, y);
g.pushRegion(getExtent());
subpaint(g);
g.setColor(0x00CACACA);
g.popContext();
}
protected void sublayout(int width, int height) {
int preferredWidth = getPreferredWidth();
Field field = getField(0);
layoutChild(field, preferredWidth, height);
setPositionChild(field, 0, 0);
setExtent(width, height);
}
public int getPreferredWidth() {
return listItems.getWidth();
}
public int getPreferredHeight() {
return listItems.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;
}
}
The above code is the part of the code that I am using.How can I implement to view the transparent background.Can anybody please help me.Thanks.
MainScreen includes its own non-transparent background. You'll have to extend Screen if you want to have a transparent/translucent background.
Another option, which I've used in my apps, is using FullScreen instead of MainScreen. FullScreen allows transparent backgrounds. If you're using setStatus or setTitle you'll have to find another method of displaying that data, but otherwise it's very similar.
From your code, though, extending Screen instead of MainScreen would likely by more appropriate.

BlackBerry customized HorizontalFieldManager

In Blackberry I want to create a class that extends HorizontalFieldManager so that I can display a label and an image on the same line with the label to the left and the image to the right.
I want the user to be able to interact with the HorizontalFieldManager as if it were a single field. I also want each HorizontalFieldManager to be focusable when added to a VeriticalFieldManager. I also want the clcik action events.
Sounds like you'll be wanting to start writing your own Field classes, here's an example to get you started:
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;
public class SimpleField extends Field {
private String label;
private Bitmap image;
private int fieldWidth;
private int fieldHeight;
private boolean hover = false;
private int focusColor = 0xcccccc;
public SimpleField(String label, Bitmap image) {
super(Field.FOCUSABLE);
this.label = label;
this.image = image;
fieldWidth = Display.getWidth();
fieldHeight = image.getHeight();
}
protected void onFocus(int direction) {
hover = true;
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
hover = false;
invalidate();
super.onUnfocus();
}
public int getPreferredWidth() {
return fieldWidth;
}
public int getPreferredHeight() {
return fieldHeight;
}
protected void layout(int width, int height) {
setExtent(fieldWidth, fieldHeight);
}
protected void paint(Graphics graphics) {
if(hover){
graphics.setColor(focusColor);
graphics.fillRect(0, 0, fieldWidth, fieldHeight);
}
graphics.drawText(label, 0, (fieldHeight - graphics.getFont().getHeight()) / 2);
graphics.drawBitmap(graphics.getFont().getAdvance(label), 0, image.getWidth(), image.getHeight(), image, 0, 0);
}
}

Reduce height and width of BlackBerry BasicEditField

I am trying to change the width and height of a BlackBerry BasicEditField.
But its not displaying the BasicEditField as i mention.
HorizontalFieldManager HFMreg =
new HorizontalFieldManager(
HorizontalFieldManager.USE_ALL_WIDTH
| HorizontalFieldManager.USE_ALL_HEIGHT) {
//Override the paint method to draw the background image.
public void paint(Graphics graphics) {
//Draw the registration background image
graphics.drawBitmap(0,0,Display.getWidth(),Display.getHeight(),BMregbg, 0, 0);
super.paint(graphics);
}
};
BEFfirstname = new BasicEditField("","",5,EditField.NO_NEWLINE) {
protected void paint(Graphics graphics) {
graphics.fillRect(0,0,80,25);
graphics.setBackgroundColor(Color.WHITE);
graphics.clear();
super.paint(graphics);
}
protected void layout() {
super.layout(getPreferredWidth(),getPreferredHeight());
setExtent(80,25); //width,height
}
public int getPreferredWidth() {
int fieldWidth = 80; //required width
return fieldWidth;
}
public int getPreferredHeight() {
int fieldHeight = 25; // required height
return fieldHeight;
}
};
//BEFfirstname.setMargin(200,0,0,60);
HFMreg.add(LFfirstname);
HFMreg.add(BEFfirstname);
add(HFMreg);
just an other way round:
BasicEditField BEFfirstname = new BasicEditField("","",5,EditField.NO_NEWLINE);
MyManager obj = new MyManger();
obj.add(BEFfirstname);
add(obj);
class MyManager extends Manager
{
MyManager()
{
super(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT);
}
protected void sublayout(int width, int height)
{
Field f = getField(0);
layoutChild(f,80,25);
setPositionChild(f, 10, 10);
}
}

Resources