Monotouch Paging ViewControllers keeps adding views - ios

I am creating an app that is required to be able to swipe between views. I used the sample code from here to get started. The issue I am having is that when I go from one ViewController to the paged ViewControllers then back again the ViewControllers are added to the list and the list keeps growing. I get duplicate ViewControllers on the screen that I can scroll through. The new list is just added to the end of the already populated viewControllers.
I am not using a navigation controller on the home screen it is simply a scroll view with buttons on it. This is part of the HomeViewController class:
private void BuildScrollView()
{
float padding = 0.0f;
float h = 80.0f;
UIView content = new UIView (new RectangleF (15, 130, UIScreen.MainScreen.Bounds.Width - 30, UIScreen.MainScreen.Bounds.Height));
content.BackgroundColor = UIColor.FromRGB(ColorManager.Grey_undrline_RGB[0], ColorManager.Grey_undrline_RGB[1], ColorManager.Grey_undrline_RGB[2]);
UIScrollView scrollView = new UIScrollView {
Frame = new RectangleF(2, 2, content.Frame.Width - 4 , content.Frame.Height -4),
BackgroundColor = UIColor.FromRGB(ColorManager.Grey_backgrd_RGB[0], ColorManager.Grey_backgrd_RGB[1], ColorManager.Grey_backgrd_RGB[2]),
AutoresizingMask = UIViewAutoresizing.FlexibleWidth
};
List<myObject> prayerList = myObjectBuilder.GeObjectFiles ();
for(int i = 0; i < myList.Count; i++){
UIView btnView = new UIView(new RectangleF (padding , padding * (i + 1) + (i * h), scrollView.Frame.Width - (padding * 2), h));
btnView.BackgroundColor = UIColor.FromRGB (ColorManager.Grey_backgrd_RGB [0], ColorManager.Grey_backgrd_RGB [1], ColorManager.Grey_backgrd_RGB [2]);
myButton btnImage = new myButton (new RectangleF (3, 3, 135, 80));
UIImage tempImage = new UIImage ("Images/bottomImages/" + myList[i].bottomImage ).Scale(new SizeF(135, 80));
btnImage.BackgroundColor = UIColor.FromPatternImage (tempImage);
btnImage.PrayerID = i;
btnImage.TouchUpInside += (object sender, EventArgs e) => {
LoadPrayerView(sender);
};
btnView.AddSubview (btnImage);
myButton button = new myButton(new RectangleF (141, 3, btnView.Frame.Width - 145, h));
button.PrayerID = i;
button.BackgroundColor = UIColor.White;
button.SetTitle (myList[i].ShortTitle.ToUpper(), UIControlState.Normal);
button.SetTitleColor (UIColor.Black, UIControlState.Normal);
button.HorizontalAlignment = UIControlContentHorizontalAlignment.Left;
button.TitleLabel.Font = UIFont.FromName ("HelveticaNeue", 16.0f);
button.TitleLabel.Lines = 0;
button.TitleLabel.LineBreakMode = UILineBreakMode.WordWrap;
button.TitleEdgeInsets = new UIEdgeInsets (0, 10, 0, 10);
button.TouchUpInside += (object sender, EventArgs e) => {
LoadView(sender);
};
btnView.AddSubview (button);
scrollView.AddSubview (btnView);
}
scrollView.ContentSize = new SizeF (scrollView.Frame.Width, (prayerList.Count + 2) * (h + padding) + padding);
content.AddSubview (scrollView);
this.View.AddSubview (content);
}
I can post more code if needed. At this point I am not really sure where to start looking for a solution.

Related

Flipping character

I would like to flip character when it walks left\right, I created a character from different body parts so flipping each of them caused this:
the reason of course was because it flipped the body parts in their own position, but not all the player together.
after that I had an idea and it was to draw the player to render target and flip the rendertarget when drawing, it worked (kind of), but when I walked when flipped it walked backwards and it also flipped the player position on the screen. here is the code:
if(mLeavusSprite.isflipped==0)
spriteBatch.Draw(character, rec,rec, Color.White,0,Vector2.Zero,SpriteEffects.None,0);
else
spriteBatch.Draw(character, rec, rec, Color.White, 0, Vector2.Zero, SpriteEffects.FlipHorizontally, 0);
character=render target that the player was drawn to.
is there anything I can do? flipping manually going to be a serious pain, I will need to move manually over 10 animations with 4+ frames each twice!
edit:
here is the code for drawing:
if (Frame == 0)
{
HeadPosition.X = Position.X;
HeadPosition.Y = Position.Y;
BodyPosition.X = HeadPosition.X + 8;
BodyPosition.Y = HeadPosition.Y + 32;
TopHandPosition.X = HeadPosition.X + 2;
TopHandPosition.Y = HeadPosition.Y + 36;
BackHandPosition.X = HeadPosition.X + 20;
BackHandPosition.Y = HeadPosition.Y + 36;
HeadSource = new Rectangle(0, 0, this.Head.Width, this.Head.Height);
BodySource = new Rectangle(0, 0, 24, 54);
TopHandSource = new Rectangle(0, 0, 10, 27);
BackHandSource = new Rectangle(0, 0, 15, 27);
theSpriteBatch.Draw(BackHand, BackHandPosition, BackHandSource,
Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(Body, BodyPosition, BodySource,
Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(Head, HeadPosition, HeadSource,
Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(TopHand, TopHandPosition, TopHandSource,
Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
}
Edit 2:
if (Frame == 0)
{
HeadPosition.X = Position.X;
HeadPosition.Y = Position.Y;
BodyPosition.X = HeadPosition.X + 8 ;
BodyPosition.Y = HeadPosition.Y + 32;
TopHandPosition.X = HeadPosition.X + 2 ;
TopHandPosition.Y = HeadPosition.Y + 36;
BackHandPosition.X = HeadPosition.X + 20 ;
BackHandPosition.Y = HeadPosition.Y + 36;
HeadSource = new Rectangle(0, 0, this.Head.Width, this.Head.Height);
BodySource = new Rectangle(0, 0, 24, 54);
TopHandSource = new Rectangle(0, 0, 10, 27);
BackHandSource = new Rectangle(0, 0, 15, 27);
int bigx=0;
int smallx=0;
float[] numbers = new[] { HeadPosition.X, BodyPosition.X , TopHandPosition.X, BackHandPosition.X};
float min = numbers.Min();
numbers = new[] { HeadPosition.X+HeadSource.Width, BodyPosition.X + BodySource.Width, TopHandPosition.X + TopHandSource.Width, BackHandPosition.X + BackHandSource.Width };
float max = numbers.Max();
float center = (max - min) / 2;
if (flip==1)
{
HeadPosition.X = Position.X;
BodyPosition.X = HeadPosition.X +center+ 8*flipOffset;
TopHandPosition.X = HeadPosition.X +center+ 2*flipOffset;
BackHandPosition.X = HeadPosition.X +center+ 20*flipOffset;
}
Debug.WriteLine("fff: " + center);
theSpriteBatch.Draw(BackHand, BackHandPosition, BackHandSource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(Body, BodyPosition, BodySource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(Head, HeadPosition, HeadSource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(TopHand, TopHandPosition, TopHandSource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
}
hmm.. that is a tricky one.
There's probably a few different ways to do this and I think drawing they player to a render target is a reasonable approach if you can get it to work.
However, if you want a quick and dirty approach you might be able to simply flip the offsets of each part like this:
var flipOffset = FlipIs == SpriteEffects.FlipHorizontally ? -1 : 1;
if (Frame == 0)
{
HeadPosition.X = Position.X;
HeadPosition.Y = Position.Y;
BodyPosition.X = HeadPosition.X + 8 * flipOffset;
BodyPosition.Y = HeadPosition.Y + 32 * flipOffset;
TopHandPosition.X = HeadPosition.X + 2 * flipOffset;
TopHandPosition.Y = HeadPosition.Y + 36 * flipOffset;
BackHandPosition.X = HeadPosition.X + 20 * flipOffset;
BackHandPosition.Y = HeadPosition.Y + 36 * flipOffset;
HeadSource = new Rectangle(0, 0, this.Head.Width, this.Head.Height);
BodySource = new Rectangle(0, 0, 24, 54);
TopHandSource = new Rectangle(0, 0, 10, 27);
BackHandSource = new Rectangle(0, 0, 15, 27);
theSpriteBatch.Draw(BackHand, BackHandPosition, BackHandSource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(Body, BodyPosition, BodySource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(Head, HeadPosition, HeadSource, Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
theSpriteBatch.Draw(TopHand, TopHandPosition, TopHandSource,Color.White, 0.0f, Vector2.Zero, Scale, FlipIs, 0);
}
This is assuming that Position is right in the center of the sprite, if not you might need to do some tweaking.
Look, I'll be honest with you. There are better ways to approach this problem, but they'd require a significant redesign of the code and it's difficult to explain in one answer.
The first thing I would do to refactor the design is create a class to represent a single Sprite part to hold the common data points (offset, source rectangle, texture, color, etc) and setup data in one place. The animation code needs to be decoupled from the drawing code, etc.

How do I programmatically create irregular buttons in Xamarin?

I am trying to programmatically create buttons for a interactive element that is part of my app. The interface looks like a doughnut chart, and if the user touches one of the regions in the doughnut chart, it will highlight that region and all of the others. I am not so concerned right now with the colors, I am trying to get the seven buttons to appear. Here is a code sample of how I am trying to create the buttons:
var radius = (Frame.Width - 38) / 2.0f;
var innerRadius = radius - 32;
var diameter = radius * 2;
energyCircleView = new UIView();
energyCircleView.BackgroundColor = UIColor.White;
energyCircleView.Frame = new RectangleF(19, confirmEntryButton.Frame.Top - 16 - (diameter), diameter, diameter);
energyCircleView.Layer.CornerRadius = radius;
energyCircleView.Layer.MasksToBounds = true;
var center = new PointF(energyCircleView.Frame.X + radius, energyCircleView.Frame.Y + radius);
var ninety = MathHelper.TwoPi * (90.0f / 360.0f);
var seventh = MathHelper.TwoPi * (1.0f / 7.0f) * 360.0f;
for (int i = 0; i < 7; i++)
{
var startAngle = ninety + (i * seventh);
var endAngle = ninety + ((i + 1) * seventh);
var shapePath = new CGPath();
shapePath.AddArc(center.X, center.Y, radius, startAngle, endAngle, false);
shapePath.AddArc(center.X, center.Y, innerRadius, endAngle, startAngle, true);
var shapeLayer = new CAShapeLayer();
shapeLayer.Path = shapePath;
var button = new UIButton();
button.Frame = new RectangleF(0, 0, diameter, diameter);
button.BackgroundColor = MyColors[i];
button.Layer.Mask = shapeLayer;
button.Layer.MasksToBounds = true;
energyButtons.Add(button);
}
foreach (UIButton button in energyButtons)
{
energyCircleView.AddSubview(button);
}
You are adding buttons as a subview of the view. But you do not show the view itself.
Add the following code to show energyCircleView:
this.View.AddSubview(energyCircleView);

generate barcode EAN13 blackberry os 7

i have some problem during generate EAN 13 barcode on blackberry os 7
i actually already create some code to generate EAN 13 and its working fine on simulator 9800 os 6 but when i it on simulator 9900 dakota os 7 the barcode goes all black
can somebody please help me to solve my problem
here my code
public void ean13writer(String ang){
try {
EAN13Writer ean13 = new EAN13Writer();
ByteMatrix barcode = ean13.encode( ang , BarcodeFormat.EAN_13 , width , height );
System.out.println("barcode : "+barcode);
jajal= ByteMatrix2Bitmap(barcode);
VerticalFieldManager cardcodemanager = new VerticalFieldManager(){
};
Bitmap borderBitmap = Bitmap.getBitmapResource("rounded-border.png");
BitmapField cardcode = new BitmapField(jajal);
cardcodemanager.add(cardcode);
cardcodemanager.setMargin(2, 40, 2, 40);
cardcodemanager.setPadding(2, 10, 2, 10);
cardcodemanager.setBorder(
BorderFactory.createBitmapBorder(
new XYEdges(12,12,12,12), borderBitmap
)
);
cardScreen.add(cardcodemanager);
}catch (Exception e) {
//add( new RichTextField( "gagal coy : " + e ) );
String a = String.valueOf(e).toString();
Dialog.alert(a);
}
}
private static Bitmap ByteMatrix2Bitmap(ByteMatrix matrix){
int width = matrix.getWidth();
int height = matrix.getHeight();
//matrix is a 0-1 matrix
byte[][] array = matrix.getArray();
int[] imgdata = new int[width*height];
Bitmap bitmap = new Bitmap(width, height);
//System.out.println("hasil array byte [] [] ="+matrix.getArray());
for (int y = 0; y < height; y++) {
for (int x = 0; x< width; x++){
if (array[y][x] == 0){
imgdata[y * width + x] = Bitmap.TRUE_WHITE;
//System.out.println("KALO 0 = "+imgdata[y * width + x]);
}else{
imgdata[y * width + x] = Bitmap.TRUE_BLACK;
//System.out.println("KALO BUKAN = "+imgdata[y * width + x]);
}
}
}
bitmap.setARGB(imgdata, 0, width, 0, 0, width, height);
Bitmap retmap = new Bitmap(2*width ,10*height );
bitmap.scaleInto(retmap, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FIT);
return retmap;
}
best regards
aditya eka putra
I already solve this problem :) , the problem is Bitmap.TRUE_WHITE not work on os 7 so i replace it with (0xFFFFFF)
maybe it can help somebody later

Blackberry UI showing images at bottom

I want to add a bar at the bottom of the screen which has images and a string ,i wanted to have the spacing equally like
img1 string img2|img3 ,this is how the bottom bar should look like,below code is not wroking properly i am gettign the alignment and the last iamge is getting disappeared.
HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(
FIELD_BOTTOM) {
public void paint(Graphics graphics) {
graphics.setBackgroundColor(0x316AC5);
graphics.clear();
super.paint(graphics);
}
};
Bitmap fadeBitmap = Bitmap
.getBitmapResource("GE_TimeZone_Fade_blue.PNG");
Bitmap clockBitmap = Bitmap.getBitmapResource("GE_Cal_icon_blue.PNG");
Bitmap tzBitmap = Bitmap
.getBitmapResource("GE_TimeZone_Button_blue.PNG");
final ImageButtonField unfocus = new ImageButtonField("",
Field.FOCUSABLE | FIELD_LEFT, "GE_TimeZone_Fade_blue.PNG",
"GE_TimeZone_Fade_blue.PNG", 0xFFFFFF);
LabelField test = new LabelField("hello");
final ImageButtonField bitmapField = new ImageButtonField("",
Field.FOCUSABLE | FIELD_HCENTER, "GE_Cal_icon_blue.PNG",
"GE_Cal_icon_onSelect.PNG", 0xFFFFFF);
final ImageButtonField bitmapField1 = new ImageButtonField("",
Field.FOCUSABLE | FIELD_RIGHT, "GE_TimeZone_Button_blue.PNG",
"GE_TimeZone_Btn_OnSelect.PNG", 0xFFFFFF);
int margin = ((Display.getWidth() - (fadeBitmap.getWidth()
+ clockBitmap.getWidth() + tzBitmap.getWidth() + test
.getWidth())) / 4);
unfocus.setMargin(0, margin, 0, 0);
test.setMargin(0, margin, 0, 0);
bitmapField.setMargin(0, margin, 0, 0);
bitmapField1.setMargin(0, margin, 0, 0);
horizontalFieldManager.add(unfocus);
horizontalFieldManager.add(test);
horizontalFieldManager.add(bitmapField);
horizontalFieldManager.add(bitmapField1);
this.setStatus(horizontalFieldManager);
There is a problem on the code you are using. Check following line.
int margin = ((Display.getWidth() - (fadeBitmap.getWidth()
+ clockBitmap.getWidth() + tzBitmap.getWidth() + test
.getWidth())) / 4);
The getWidth() and getHeight() of any Field will return valid values if the layout method of it's parent manager gets called.
So, adjusting the margin using getWidth(), getHeight() is not safe.
But it is possible to control the alignment and position of the Fields via extending HorizontalFieldManager. Check the following codes and output to get an idea about how it can be done.
Output
Using the StatusFieldManager:
StatusFieldManager statusFieldManager = new StatusFieldManager();
statusFieldManager.setBackground(BackgroundFactory.createSolidBackground(0x316AC5));
final Bitmap bmTest = Bitmap.getBitmapResource("bitmap.png");
BitmapField bmOne = new BitmapField(bmTest, Field.FOCUSABLE | FIELD_LEFT);
BitmapField bmTwo = new BitmapField(bmTest, Field.FOCUSABLE | FIELD_LEFT);
BitmapField bmThree = new BitmapField(bmTest, Field.FOCUSABLE | FIELD_LEFT);
LabelField lblTest = new LabelField("Test");
statusFieldManager.add(bmOne);
statusFieldManager.add(lblTest);
statusFieldManager.add(bmTwo);
statusFieldManager.add(bmThree);
setStatus(statusFieldManager);
Implementation of StatusFieldManager
class StatusFieldManager extends HorizontalFieldManager {
protected void sublayout(int width, int height) {
int numField = getFieldCount();
Field f;
int nHeight = 0, maxFieldWidth = width / 4;
if (numField == 4) {
f = getField(0);
layoutChild(f, maxFieldWidth, height);
nHeight = Math.max(nHeight, f.getHeight());
f = getField(1);
layoutChild(f, maxFieldWidth, height);
nHeight = Math.max(nHeight, f.getHeight());
f = getField(2);
layoutChild(f, maxFieldWidth, height);
nHeight = Math.max(nHeight, f.getHeight());
f = getField(3);
layoutChild(f, maxFieldWidth, height);
nHeight = Math.max(nHeight, f.getHeight());
// set position of the child fields
int x = 0, y = 0;
int requiredFieldWidth = 0;
for (int i=0;i<numField;i++) {
requiredFieldWidth += getField(i).getWidth();
}
int spaceBetweenFields = (width - requiredFieldWidth) / (numField - 1);
for (int i=0;i<numField;i++) {
setPositionChild(getField(i), x, (nHeight - getField(i).getHeight()) / 2);
x += getField(i).getWidth() + spaceBetweenFields;
}
setExtent(width, nHeight);
} else {
setExtent(0, 0);
}
}
}
try this -
unfocus.setMargin(0, margin, 0, 0);
test.setMargin(0, 10, 0, 0);
bitmapField.setMargin(0, 10, 0, 0);
bitmapField1.setMargin(0, 10, 0, 0);

ObjectChoiceField width and height issue in BlackBerry?

I am working a UI based application in BlackBerry. UI is like the following:
label-1 : Oject choice field-1
label-2 : Oject choice field-2
label-3 : Oject choice field-3
label-4 : Oject choice field-4
label-5 : Oject choice field-5
It is really getting difficult to generate this UI. The width and height is the main issue for my difficulty.
public HomeScreen() {
super(MainScreen.USE_ALL_WIDTH | USE_ALL_HEIGHT | VERTICAL_SCROLL);
_dbOperations = new DBOperations();
_mainVFM = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH);
_mainVFM.setMargin(10, 10, 40, 10);
_menuFieldVFM = new VerticalFieldManager();
_menuFieldVFM.setMargin(0, 5, 0, 0);
_menuDropDownVFM = new VerticalFieldManager();
((VerticalFieldManager) getMainManager()).setBackground(BackgroundFactory.createSolidBackground(Constants.BgColorCode));
XYEdges padding = new XYEdges(1, 1, 1, 1);
Border roundedBorder = BorderFactory.createSimpleBorder(padding);
// HEADING
_headingHFM = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH);
_headingLabelField = new LabelField(Constants.Text_heading);
_headingLabelField.setFont(ApplicationFont.labelFont);
_headingHFM.add(_headingLabelField);
_headingHFM.setMargin(0, 0, 20, 0);
// _productLine LINE
_productLineLabelField = new LabelField(Constants.Text_ProductLine);
_productLineLabelField.setFont(ApplicationFont.explainationFont);
_productLineLabelField.setMargin(8, 0, 0, 0);
_menuFieldVFM.add(_productLineLabelField);
int plSetTo1 = 0;
_productLineOCF = new ObjectChoiceField("", _productLineArray, plSetTo1, Field.FIELD_RIGHT) {
protected void layout(int width, int height) {
setMinimalWidth(width - 60);
super.layout(width, height);
};
};
_productLineOCF.setFont(ApplicationFont.explainationFont);
_productLineOCF.setChangeListener(this);
_menuDropDownVFM.add(_productLineOCF);
// HP
_HPLabelField = new LabelField(Constants.Text_HP);
_HPLabelField.setFont(ApplicationFont.explainationFont);
_HPLabelField.setMargin(25, 0, 0, 0);
_menuFieldVFM.add(_HPLabelField);
int hpSetTo1 = 0;
String data_HP[] = { "Select HP" };
_HP_OCF = new ObjectChoiceField("", data_HP, hpSetTo1, Field.FIELD_RIGHT) {
protected void layout(int width, int height) {
setMinimalWidth(width - 60);
super.layout(width, height);
};
};
_HP_OCF.setFont(ApplicationFont.explainationFont);
_HP_OCF.setChangeListener(this);
_menuDropDownVFM.add(_HP_OCF);
_RPMLabelField = new LabelField(Constants.Text_RPM);
_RPMLabelField.setFont(ApplicationFont.explainationFont);
_RPMLabelField.setMargin(25, 0, 0, 0);
_menuFieldVFM.add(_RPMLabelField);
int rpmSetTo1 = 0;
String data_RPM[] = { "Select RPM" };
_RPM_OCF = new ObjectChoiceField("", data_RPM, rpmSetTo1, Field.FIELD_RIGHT) {
protected void layout(int width, int height) {
setMinimalWidth(width - 60);
super.layout(width, height);
};
};
_RPM_OCF.setChangeListener(this);
_menuDropDownVFM.add(_RPM_OCF);
// VOLTAGE
_voltageLabelField = new LabelField(Constants.Text_voltage);
_voltageLabelField.setFont(ApplicationFont.explainationFont);
_voltageLabelField.setMargin(25, 0, 0, 0);
_menuFieldVFM.add(_voltageLabelField);
int volSetTo1 = 0;
String data_voltage[] = { "Select Voltage" };
_voltageOCF = new ObjectChoiceField("", data_voltage, volSetTo1, Field.FIELD_RIGHT) {
protected void layout(int width, int height) {
setMinimalWidth(width - 60);
super.layout(width, height);
};
};
_voltageOCF.setChangeListener(this);
_menuDropDownVFM.add(_voltageOCF);
// FRAME SIZE
_frameSizeLabelField = new LabelField(Constants.Text_frameSize);
_frameSizeLabelField.setFont(ApplicationFont.explainationFont);
_frameSizeLabelField.setMargin(25, 0, 0, 0);
_menuFieldVFM.add(_frameSizeLabelField);
// int iSetTo1 = 0;
_frameSizeOCF = new ObjectChoiceField("", _frameArray, plSetTo1, Field.FIELD_RIGHT) {
protected void layout(int width, int height) {
setMinimalWidth(width - 60);
super.layout(width, height);
};
};
_frameSizeOCF.setChangeListener(this);
_menuDropDownVFM.add(_frameSizeOCF);
_menuMainHFM = new HorizontalFieldManager();
_menuMainHFM.add(_menuFieldVFM);
_menuMainHFM.add(_menuDropDownVFM);
// -OR- LABEL
_orLabelField = new LabelField(Constants.Text_or);
_orLabelField.setMargin(20, 0, 0, 0);
_orLabelField.setFont(ApplicationFont.labelFont);
// SEARCH BY PART NUMBER
_SearchByPartNumberField = new LabelField(Constants.Text_PartNoLabel);
_SearchByPartNumberField.setMargin(20, 0, 0, 0);
_SearchByPartNumberField.setFont(ApplicationFont.labelFont);
// ENTER PART NUMBER
_enterPartNoHFM = new HorizontalFieldManager();
_enterPartNoLabelField = new LabelField("Enter Part Number: ");
_partNumberEditField = new EditField();
_partNumberEditField.setBorder(roundedBorder);
_enterPartNoHFM.add(_enterPartNoLabelField);
_enterPartNoHFM.add(_partNumberEditField);
_enterPartNoHFM.setMargin(20, 0, 0, 0);
// SAERCH BUTTON
_searchButtonField = new ButtonField(" Search ", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER);
_searchButtonField.setChangeListener(this);
_searchButtonField.setMargin(20, 0, 0, 0);
_checkForUpdates = new ButtonField(Constants.Button_checkForUpdates, ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER);
_checkForUpdates.setMargin(10, 0, 0, 0);
_checkForUpdates.setChangeListener(this);
_mainVFM.add(_headingHFM);
_mainVFM.add(_menuMainHFM);
_mainVFM.add(_orLabelField);
_mainVFM.add(_SearchByPartNumberField);
_mainVFM.add(_enterPartNoHFM);
_mainVFM.add(_searchButtonField);
_mainVFM.add(_checkForUpdates);
add(_mainVFM);
}
Now I am using following code. But no progress, I am at the same stage.
public HomeScreen() {
super(MainScreen.USE_ALL_WIDTH | USE_ALL_HEIGHT | VERTICAL_SCROLL);
_dbOperations = new DBOperations();
_mainVFM = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH);
_mainVFM.setMargin(10, 10, 40, 10);
((VerticalFieldManager) getMainManager()).setBackground(BackgroundFactory.createSolidBackground(Constants.BgColorCode));
// HEADING
_headingHFM = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH);
_headingLabelField = new LabelField(Constants.Text_heading);
_headingLabelField.setFont(ApplicationFont.labelFont);
_headingHFM.add(_headingLabelField);
_headingHFM.setMargin(0, 0, 20, 0);
// IMPORTANT CALCULATION
String productLineString = Constants.Text_ProductLine;
int charWidth = 0;
for (int i = 0; i < productLineString.length(); i++) {
charWidth = Font.getDefault().getAdvance(String.valueOf(productLineString.charAt(i)));
totalWidth += charWidth;
System.out.println(totalWidth + "===>>>>>>total width" + i);
}
System.out.println(totalWidth + "===>>>>>>total width");
totalWidth = totalWidth + 10;
// _productLine LINE
_productLineLabelField = new LabelField(Constants.Text_ProductLine);
_productLineLabelField.setFont(ApplicationFont.explainationFont);
_productLineLabelField.setMargin(8, 0, 0, 0);
int plSetTo1 = 0;
_productLineOCF = new ObjectChoiceField("", _productLineArray, plSetTo1, Field.FIELD_RIGHT);
_productLineOCF.setFont(ApplicationFont.explainationFont);
_productLineOCF.setChangeListener(this);
_productHFM = new HorizontalFieldManager(USE_ALL_WIDTH | FOCUSABLE);
_productHFM.add(_productLineLabelField);
_productHFM.add(_productLineOCF);
// HP
_hp1 = new VerticalFieldManager();
_HPLabelField = new LabelField(Constants.Text_HP);
_HPLabelField.setFont(ApplicationFont.explainationFont);
_HPLabelField.setMargin(25, 0, 0, 0);
_hp1.add(_HPLabelField);
_hp2 = new VerticalFieldManager();
int hpSetTo1 = 0;
String data_HP[] = { " Select HP" };
_HP_OCF = new ObjectChoiceField("", data_HP, hpSetTo1, Field.FIELD_RIGHT | USE_ALL_WIDTH);
_HP_OCF.setMinimalWidth(totalWidth + 50);
_HP_OCF.setFont(ApplicationFont.explainationFont);
_HP_OCF.setChangeListener(this);
_hp2.add(_HP_OCF);
_hpHFM = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH) {
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(maxWidth, maxHeight);
int x = totalWidth;
setPositionChild(_hp2, x, 0);
}
};
_hpHFM.add(_hp1);
_hpHFM.add(_hp2);
_RPM_HFM = new HorizontalFieldManager();
// RPM
_RPMLabelField = new LabelField(Constants.Text_RPM);
_RPMLabelField.setFont(ApplicationFont.explainationFont);
_RPMLabelField.setMargin(25, 0, 0, 0);
_RPM_HFM.add(_RPMLabelField);
int rpmSetTo1 = 0;
String data_RPM[] = { "Select RPM" };
_RPM_OCF = new ObjectChoiceField("", data_RPM, rpmSetTo1, Field.FIELD_RIGHT);
_RPM_OCF.setMinimalWidth(totalWidth + 50);
_RPM_OCF.setChangeListener(this);
_RPM_HFM.add(_RPM_OCF);
_voltageHFM = new HorizontalFieldManager();
// VOLTAGE
_voltageLabelField = new LabelField(Constants.Text_voltage);
_voltageLabelField.setFont(ApplicationFont.explainationFont);
_voltageLabelField.setMargin(25, 0, 0, 0);
_voltageHFM.add(_voltageLabelField);
int volSetTo1 = 0;
String data_voltage[] = { "Select Voltage" };
_voltageOCF = new ObjectChoiceField("", data_voltage, volSetTo1, Field.FIELD_RIGHT);
_voltageOCF.setMinimalWidth(totalWidth + 50);
_voltageOCF.setChangeListener(this);
_voltageHFM.add(_voltageOCF);
_frameHFM = new HorizontalFieldManager();
// FRAME SIZE
_frameSizeLabelField = new LabelField(Constants.Text_frameSize);
_frameSizeLabelField.setFont(ApplicationFont.explainationFont);
_frameSizeLabelField.setMargin(25, 0, 0, 0);
_frameHFM.add(_frameSizeLabelField);
int fSetTo1 = 0;
_frameSizeOCF = new ObjectChoiceField("", _frameArray, fSetTo1, Field.FIELD_RIGHT);
_frameSizeOCF.setMinimalWidth(totalWidth + 50);
_frameSizeOCF.setChangeListener(this);
_frameHFM.add(_frameSizeOCF);
// -OR- LABEL
_orLabelField = new LabelField(Constants.Text_or);
_orLabelField.setMargin(20, 0, 0, 0);
_orLabelField.setFont(ApplicationFont.labelFont);
// SEARCH BY PART NUMBER
_SearchByPartNumberField = new LabelField(Constants.Text_PartNoLabel);
_SearchByPartNumberField.setMargin(20, 0, 0, 0);
_SearchByPartNumberField.setFont(ApplicationFont.labelFont);
// ENTER PART NUMBER
_enterPartNoHFM = new HorizontalFieldManager();
_enterPartNoLabelField = new LabelField("Enter Part Number: ");
_partNumberEditField = new EditField();
_partNumberEditField.setBorder(roundedBorder);
_enterPartNoHFM.add(_enterPartNoLabelField);
_enterPartNoHFM.add(_partNumberEditField);
_enterPartNoHFM.setMargin(20, 0, 0, 0);
// SAERCH BUTTON
_searchButtonField = new ButtonField(" Search ", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER);
_searchButtonField.setChangeListener(this);
_searchButtonField.setMargin(20, 0, 0, 0);
_checkForUpdates = new ButtonField(Constants.Button_checkForUpdates, ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER);
_checkForUpdates.setMargin(10, 0, 0, 0);
_checkForUpdates.setChangeListener(this);
add(_pageHeaderHFM);
_mainVFM.add(_headingHFM);
_mainVFM.add(_productHFM);
_mainVFM.add(_hpHFM);
_mainVFM.add(_RPM_HFM);
_mainVFM.add(_voltageHFM);
_mainVFM.add(_frameHFM);
_mainVFM.add(_orLabelField);
_mainVFM.add(_SearchByPartNumberField);
_mainVFM.add(_enterPartNoHFM);
_mainVFM.add(_searchButtonField);
_mainVFM.add(_checkForUpdates);
add(_mainVFM);
}
I am fine with the width of the OCF but height and the position of the OCF must be settled as required. This is the output I am getting with this code.
This answer is based on following links:
Align Field in HorizontalFieldManager.
BlackBerry HorizontalFieldManager alignment.
Try following code:
class HomeScreen extends MainScreen {
public HomeScreen() {
super(Field.USE_ALL_WIDTH | Field.USE_ALL_HEIGHT | MainScreen.VERTICAL_SCROLL);
VerticalFieldManager vfmMain = new VerticalFieldManager(Field.USE_ALL_WIDTH);
// LabelField
LabelField lblFOne = new LabelField("LabelField One", Field.FIELD_LEFT | Field.FIELD_VCENTER);
// ObjectChoiceField
VerticalFieldManager vfmChoiceFieldContainerOne = new VerticalFieldManager(Field.USE_ALL_WIDTH | Field.FIELD_RIGHT);
final String[] options = new String[] { "Choice One - A very long string.", "Choice B", "Choice C" };
ObjectChoiceField ocfOne = new ObjectChoiceField("", options, 0, Field.FIELD_RIGHT | ObjectChoiceField.FORCE_SINGLE_LINE);
vfmChoiceFieldContainerOne.add(ocfOne);
// Contain a row of LabelField and ObjectChoiceField
HorizontalFieldManager hfmRowOne = new HorizontalFieldManager(Field.USE_ALL_WIDTH | Field.FOCUSABLE);
hfmRowOne.setBackground(BackgroundFactory.createSolidBackground(Color.GREEN));
hfmRowOne.add(lblFOne);
hfmRowOne.add(vfmChoiceFieldContainerOne);
vfmMain.add(hfmRowOne);
add(vfmMain);
}
}

Resources