How to align images a the bottom - blackberry

I am trying to align images at the bottom of the screen in the format img1 str img2 |img3 ,the issue i am facing is if the str is small the images are getting shifted left ,else the images moves right based on the string length.I want to keep the images always at the right corner ,how to do it ?
VerticalFieldManager test = new VerticalFieldManager();
HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(
FIELD_BOTTOM | Manager.USE_ALL_WIDTH) {
public void paint(Graphics graphics) {
graphics.setBackgroundColor(0x316AC5);
graphics.clear();
super.paint(graphics);
}
};
Bitmap fadeBitmap = Bitmap
.getBitmapResource("icon_tz_faded_316AC5_35x39.png");
Bitmap clockBitmap = Bitmap
.getBitmapResource("icon_date_picker_white_316AC5_35x39.png");
Bitmap tzBitmap = Bitmap
.getBitmapResource("icon_tz_white_316AC5_35x39.png");
final ImageButtonField unfocus = new ImageButtonField("",
Field.FOCUSABLE | FIELD_LEFT, "icon_tz_faded_316AC5_35x39.png",
"icon_tz_faded_316AC5_35x39.png", 0xFFFFFF);
final ImageButtonField bitmapField = new ImageButtonField("",
Field.FOCUSABLE | ImageButtonField.FIELD_RIGHT,
"icon_date_picker_white_316AC5_35x39.png",
"icon_date_picker_selected_35x39.png", 0xFFFFFF);
final ImageButtonField bitmapField1 = new ImageButtonField("",
Field.FOCUSABLE | ImageButtonField.FIELD_RIGHT,
"icon_tz_white_316AC5_35x39.png", "icon_tz_selected_35x39.png",
0xFFFFFF);
int margin = ((Display.getWidth() - (fadeBitmap.getWidth()
+ clockBitmap.getWidth() + tzBitmap.getWidth() + timezoneTitle
.getWidth())) / 4);
unfocus.setMargin(0, 3, 0, 0);
timezoneTitle.setMargin(0, 120, 0, 0);
bitmapField1.setMargin(0, 0, 0, 0);
horizontalFieldManager.add(unfocus);
horizontalFieldManager.add(timezoneTitle);
horizontalFieldManager.add(bitmapField);
horizontalFieldManager.add(bitmapField1);
test.add(horizontalFieldManager);

Create another horizontal field and add images to this field, then use setPadding(top,left,bottom,right) to arrange the images in order.

Related

How to create Horizontal scroll view with the TextField in Blackberry

I am new to the Blackberry Field, I want to create a customized Horizontal scroll view which should exactly looks like in the below image. Any Help will be Apprreciated
I am using Blackberry Simulator 9900, Thanks in advance .
I guess Middle portion is ONLY scrollable, right ?
You must have one HorizontalFieldManager with HORIZONTAL_SCROLL ( Middle one )
e.g. HorizontalFieldManager hfm = new HorizontalFieldManager( HORIZONTAL_SCROLL );
add your custom field in this manager.
You must add hfm in Custom OuterManager.
For your reference I'm posting my own code here...
private class HScroll extends MainScreen{
public HScroll() {
super( USE_ALL_WIDTH );
OuterManager father = new OuterManager( );
LabelField ll = new LabelField("<");
father.add(ll);
HorizontalFieldManager hfm = new HorizontalFieldManager( HORIZONTAL_SCROLL );
for( int i=0; i<10; i++ ){
ButtonField btn = new ButtonField(" i " + i);
hfm.add(btn);
}
father.add(hfm);
LabelField ll1 = new LabelField(">");
father.add(ll1);
add(father);
}
private class OuterManager extends net.rim.device.api.ui.Manager{
public OuterManager() {
super(USE_ALL_WIDTH);
}
protected void sublayout(int width, int height) {
int x = 0;
Field ff = getField( 0 );
Field ff2 = getField( 2 );
setPositionChild(ff, x, 0);
layoutChild(ff, ff.getPreferredWidth(), ff.getPreferredHeight());
x = x + ff.getPreferredWidth();
Field ff1 = getField( 1 );
setPositionChild(ff1, x , 0);
layoutChild(ff1, width - ff.getPreferredWidth() - ff2.getPreferredWidth() , ff1.getPreferredHeight());
x = width - ff2.getPreferredWidth();
setPositionChild(ff2, x, 0);
layoutChild(ff2, ff2.getPreferredWidth(), ff2.getPreferredHeight());
setExtent(width, height);
}
}
}

GridFieldManager Blackberry align fields

I have a GridFieldManager whith 3 columns and I want to align their content like this:
|Title left | Title Center | Title Right|
The problem is that I'm using a RichTextField instead of a LabelField because I want the text of each title to grap like this:
|Title left | Title Center | Title Right |
|is wrapped | is wrapped too | also wrapped |
If I use RichTextField instead of LabelField, then, the the alignment is ignored. This is my code:
public class CustomGridFieldextends GridFieldManager {
private int numColumns = 3;
private int margin = 5;
public CustomGridField(String leftText, String centerText, String rightText) {
super(1, 3, GridFieldManager.USE_ALL_WIDTH);
setPadding(0, margin, 0, margin);
int columnWidth = (Display.getWidth() / numColumns);
for (int i = 0; i < numColumns; i++) {
setColumnProperty(i, GridFieldManager.FIXED_SIZE, columnWidth);
}
// leftmost text
RichTextField leftLabel = new RichTextField(leftText){
protected void paint(Graphics g) {
g.setColor(Color.WHITE);
g.setFont(getFont().derive(Font.BOLD));
super.paint(g);
}
};
leftLabel.setFont(getFont().derive(Font.BOLD));
add(leftLabel, Field.FIELD_LEFT);
// center text
RichTextField centerLabel = new RichTextField(centerText){
protected void paint(Graphics g) {
g.setFont(getFont().derive(Font.BOLD));
g.setColor(Color.WHITE);
super.paint(g);
}
};
centerLabel.setFont(getFont().derive(Font.BOLD));
add(centerLabel, Field.FIELD_HCENTER);
// rightmost text
RichTextField rightLabel = new RichTextField(rightText) {
protected void paint(Graphics g) {
g.setFont(getFont().derive(Font.BOLD));
g.setColor(Color.WHITE);
super.paint(g);
}
};
rightLabel.setFont(getFont().derive(Font.BOLD));
add(rightLabel, Field.FIELD_RIGHT);
}
protected void paintBackground(Graphics g) {
// draw a nice background...
}
}
Found the answer, turns out that I had to use this styles when creating the RichTextFields:
new RichTextField("text on left",RichTextField.TEXT_ALIGN_LEFT);
new RichTextField("text on center",RichTextField.TEXT_ALIGN_HCENTER);
new RichTextField("text on right",RichTextField.TEXT_ALIGN_RIGHT);
Now I'm working on the grapping...
PD: found it here: http://v4ks1n.wordpress.com/2009/04/16/richtextfield-alignment/

Align field to right in grid layout in blackberry

I want my application to align two field, one to left and other to extreme left of the screen. For that i am using GridLayoutManager class. Here is my code
GridFieldManager gridFieldManager = new GridFieldManager(2,2, GridFieldManager.PREFERRED_SIZE_WITH_MAXIMUM);
gridFieldManager.add(new ButtonField("Button One"), Field.FIELD_HCENTER);
gridFieldManager.add(new ButtonField("Button Two"), Field.FIELD_RIGHT);
gridFieldManager.add(new ButtonField("HC", Field.FIELD_HCENTER));
gridFieldManager.add(new ButtonField("RT", Field.FIELD_RIGHT));
add(gridFieldManager);
And, here is my output in simulator
Can anyone please help me to align the Button Two to the extreme right of the screen ? Any help will be appreciated.
Basically it's all a matter of alignment flags and grid's column properties.
Changing the GridFieldManager style to Manager.USE_ALL_WIDTH and setting column properties to GridFieldManager.AUTO_SIZE makes all the available space to be divided evently among the two columns.
gridFieldManager.setColumnProperty(0, GridFieldManager.AUTO_SIZE, 0);
gridFieldManager.setColumnProperty(1, GridFieldManager.AUTO_SIZE, 0);
The following code snippet
GridFieldManager gridFieldManager = new GridFieldManager(2,2, Manager.USE_ALL_WIDTH);
gridFieldManager.setColumnProperty(0, GridFieldManager.AUTO_SIZE, 0);
gridFieldManager.setColumnProperty(1, GridFieldManager.AUTO_SIZE, 0);
gridFieldManager.add(new ButtonField("Button One"), Field.FIELD_LEFT);
gridFieldManager.add(new ButtonField("Button Two"), Field.FIELD_RIGHT);
gridFieldManager.add(new ButtonField("HC"), Field.FIELD_LEFT);
gridFieldManager.add(new ButtonField("RT"), Field.FIELD_RIGHT);
add(gridFieldManager);
produces
This slightly modified code snippet
GridFieldManager gridFieldManager = new GridFieldManager(1,2, Manager.USE_ALL_WIDTH);
gridFieldManager.setColumnProperty(0, GridFieldManager.AUTO_SIZE, 0);
gridFieldManager.setColumnProperty(1, GridFieldManager.AUTO_SIZE, 0);
VerticalFieldManager vfmLeft = new VerticalFieldManager();
vfmLeft.add(new ButtonField("Button One", Field.FIELD_HCENTER));
vfmLeft.add(new ButtonField("HC", Field.FIELD_HCENTER));
gridFieldManager.add(vfmLeft, Field.FIELD_LEFT);
VerticalFieldManager vfmRight = new VerticalFieldManager();
vfmRight.add(new ButtonField("Button Two", Field.FIELD_HCENTER));
vfmRight.add(new ButtonField("RT", Field.FIELD_HCENTER));
gridFieldManager.add(vfmRight, Field.FIELD_RIGHT);
add(gridFieldManager);
produces
Finally, to illustrate what I said previously about the available space being divided evently among the two columns, the following code snippet
GridFieldManager gridFieldManager = new GridFieldManager(1,2, Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT);
gridFieldManager.setColumnProperty(0, GridFieldManager.AUTO_SIZE, 0);
gridFieldManager.setColumnProperty(1, GridFieldManager.AUTO_SIZE, 0);
gridFieldManager.setRowProperty(0, GridFieldManager.AUTO_SIZE, 0);
VerticalFieldManager vfmLeft = new VerticalFieldManager(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT);
vfmLeft.setBackground(BackgroundFactory.createSolidBackground(Color.CYAN));
gridFieldManager.add(vfmLeft);
VerticalFieldManager vfmRight = new VerticalFieldManager(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT);
vfmRight.setBackground(BackgroundFactory.createSolidBackground(Color.GRAY));
gridFieldManager.add(vfmRight);
add(gridFieldManager);
produces two columns of equal size.
Please use this following class.
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.container.HorizontalFieldManager;
public class HFMLeftFieldRightField extends HorizontalFieldManager {
private Field leftField;
private Field rightField;
private final static int TOP_MARGIN = 0;
private final static int LEFT_MARGIN = 30;
public HFMLeftFieldRightField() {
super(USE_ALL_WIDTH);
}
public HFMLeftFieldRightField(boolean isQatari) {
super(USE_ALL_WIDTH | Field.FIELD_LEFT);
}
public void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
int width = getWidth();
if (rightField != null) {
int x = width - rightField.getWidth() - LEFT_MARGIN;
int y = TOP_MARGIN;
setPositionChild(rightField, x, y);
}
if (leftField != null) {
int y = TOP_MARGIN+rightField.getHeight()/5;
int x = LEFT_MARGIN;
setPositionChild(leftField, 0, y);
}
setExtent(maxWidth, rightField.getHeight() + TOP_MARGIN * 2);
}
public void setLeftButton(Field leftField) {
this.leftField = leftField;
super.add(leftField);
}
public void setRightButton(Field rightField) {
this.rightField = rightField;
super.add(rightField);
}
}
And add field this way.
HFMLeftFieldRightField hfm = new HFMLeftFieldRightField();
hfm.setLeftButton(new EditField("Left"));
hfm.setRightButton(new EditField("Right"));
add(hfm);
More detail http://keraisureshvblackberry.blogspot.in/2012/02/there-are-very-common-there-there-are.html
Hope helpfull..
It seems like you should use JustifiedHorizontalFieldManager instead of GridFieldManager. JustifiedHorizontalFieldManager is not a standard manager included in BlackBerry SDK, but a member of a set of UI components released by RIM later to help developers build more rich UI Interfaces. You have to download the code from here, add it to your proyect and then include the following line:
JustifiedHorizontalFieldManager justifiedManager = new JustifiedHorizontalFieldManager(buttonOne, buttonTwo, false, USE_ALL_WIDTH );
Change the style parameter of your gridFieldManager constructor to
GridFieldManager.USE_ALL_WIDTH
It should be like this
GridFieldManager gridFieldManager = new GridFieldManager(2,2,GridFieldManager.USE_ALL_WIDTH );

howto center a label on a screen using horizontalfield

I want to center a logo and a textfield in the middle of the screen, both vertically and horizontally. What is the best way to achieve this?
i have been trying with this so far..
public upd8rMainScreen(){
Bitmap logoBitmap = Bitmap.getBitmapResource("upd8rLOGO.png");
bitmapField = new BitmapField(logoBitmap, Field.FIELD_VCENTER);
labelfield = new LabelField("Tap Your Phone Onto The Reader To Activate",Field.FIELD_VCENTER);
topHfm = new HorizontalFieldManager(Manager.VERTICAL_SCROLL);
middleHfm = new HorizontalFieldManager(Manager.VERTICAL_SCROLL);
vfm = new VerticalFieldManager();
topHfm.add(bitmapField);
middleHfm.add(labelfield);
vfm.add(topHfm);
vfm.add(middleHfm);
add(vfm);
}
May be it help you Try this
Bitmap logoBitmap = Bitmap.getBitmapResource("basket.png");
bitmapField = new BitmapField(logoBitmap,Field.FIELD_HCENTER);
labelfield = new LabelField("Tap Your Phone Onto ",Field.FIELD_HCENTER);
//
VerticalFieldManager vrt=new VerticalFieldManager(USE_ALL_WIDTH)
{
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(Display.getWidth(),Display.getHeight());
setExtent(Display.getWidth(),Display.getHeight());
}
};
Font f=labelfield.getFont();
int hight1=f.getAdvance(labelfield.getText());
int k=labelfield.getPreferredHeight();
int number=hight1/Display.getWidth()+1;
int hight2=logoBitmap.getHeight();
int padding=(Display.getHeight()-((number*k)+hight2))/2;
if(padding>0){
bitmapField.setPadding(padding,0,0,0);
}
vrt.add(bitmapField);
vrt.add(labelfield);
image display like following
You can try re-setting the margin of the VerticalFieldManager like this :
topHfm = new HorizontalFieldManager(Manager.FIELD_HCENTER | Manager.FIELD_VCENTER);
midHfm = new HorizontalFieldManager(Manager.FIELD_HCENTER | Manager.FIELD_VCENTER);
topHfm.add(bitmapField);
midHfm.add(new LabelField("My Label", Manager.FIELD_HCENTER));
VerticalFieldManager vfm = new VerticalFieldManager(Manager.FIELD_VCENTER);
vfm.add(topHfm);
vfm.add(midHfm);
int screenHeight = Display.getHeight();
int screenWidth = Display.getWidth();
int totalContentHeight = topHfm.getPreferredHeight()+ midHfm.getPreferredHeight();
int maxContentWidth = (topHfm.getPreferredWidth()>midHfm.getPreferredWidth()) ? topHfm.getPreferredWidth() : midHfm.getPreferredWidth() ;
int marginTop = (screenHeight>totalContentHeight)? (screenHeight - totalContentHeight) / 2 : 0;
int marginLeft = (screenWidth>maxContentWidth)? (screenWidth - maxContentWidth ) / 2 : 0;
vfm.setMargin(marginTop, 0, 0, marginLeft);
add(vfm);
I don't claim this is the "best way" to do this, but I hope it'll work for your case.

Setting buttonfield to Right and bottom side (Blackberry)

I want my button to be at the right side and bottom side. How it is possible? Please help me. My code below only displays the button on the right side but not bottom.
ButtonField b = new ButtonField("button", Field.FIELD_RIGHT | FIELD_BOTTOM);
VerticalFieldManager vf = new VerticalFieldManager(Field.USE_ALL_WIDTH);
vf.add(b);
add(vf);
By default, a VerticalFieldManager will only use the smallest amount of vertical space necessary to display all of its children. So, your button is displaying at the bottom of the manager, but the manager is only as tall as your button. Also, a VerticalFieldManager is intended for top-down display. Try something like this:
HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_HEIGHT | USE_ALL_WIDTH);
VerticalFieldManager vfm = new VerticalFieldManager(USE_ALL_WIDTH | FIELD_BOTTOM);
ButtonField b = new ButtonField("Button", Field.FIELD_RIGHT);
vfm.add(b);
hfm.add(vfm);
add(hfm);
thanks to all. but i got my answer.
HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_HEIGHT | USE_ALL_WIDTH) {
public int getPreferredWidth() {
return Display.getWidth();
}
public int getPreferredHeight() {
return Display.getHeight();
}
protected void sublayout(int maxWidth, int maxHeight) {
int displayWidth = Display.getWidth();
int displayHeight = Display.getHeight();
super.sublayout(displayWidth, displayHeight);
setExtent( Math.min( Display.getWidth(), getPreferredWidth() ),
Math.min( Display.getHeight(), getPreferredHeight() ) );
}
};
VerticalFieldManager vfm = new VerticalFieldManager(USE_ALL_WIDTH | FIELD_RIGHT);
ButtonField b = new ButtonField("Button", Field.FIELD_RIGHT);
vfm.add(b);
hfm.add(vfm);
add(vfm);
It's perfect work to me.

Resources