Label caption is not set in label background center - blackberry

I am working on a logic where I have to align the label caption in vertically center but it's not working.
I have tried to set text padding but the whole label with background is shifting.Is there any way to solve this.
Here is my code:
final Bitmap tabBackGroundImage1 = Bitmap.getBitmapResource("box_img.png");
homeContentManager.setBorder( BorderFactory.createBitmapBorder(
new XYEdges(16,16,16,16), tabBackGroundImage));
//homeContentManager.setBorder(roundedBourder);
//HOME SCREEN HORIZONTAL MANAGER
buddiesLabel=new LabelField("Buddies");
_bitmap = EncodedImage.getEncodedImageResource("buddies.png");
final BitmapField buddiesBmp = new BitmapField(Constant.sizePic(_bitmap,
_bitmap.getHeight(), _bitmap.getWidth()));
buddiesBmp.setMargin(3,5,0,5);
if(BuddyListField.onlineBuddyCount < 1000) {
buddyCount=new LabelField(" "+BuddyListField.onlineBuddyCount+" "){
/*public void setText(String text,int offset,int length){
text = String.valueOf(BuddyListField.onlineBuddyCount);
offset = 200;
length = 5;
super.setText(text, offset, length);
}*/
public int getPreferredHeight() {
return tabBackGroundImage1.getHeight();
}
public int getPreferredWidth() {
return tabBackGroundImage1.getWidth();
}
protected void layout(int width, int height) {
super.layout(width, height);
setExtent(Math.min(width, tabBackGroundImage1.getWidth()), Math.min(height, tabBackGroundImage1.getHeight()));
}
protected void paint(Graphics graphics) {
int deviceWidth=net.rim.device.api.system.Display.getWidth();
int deviceHeight=net.rim.device.api.system.Display.getHeight();
graphics.clear();
graphics.drawBitmap(0, 0, deviceWidth,
deviceHeight, tabBackGroundImage1, 0, 0);
super.paint(graphics);
}
};
This is the output:
But I need to set zero to vertically center in background.

Try like this
protected void paint(Graphics graphics) {
int deviceWidth=net.rim.device.api.system.Display.getWidth();
int deviceHeight=net.rim.device.api.system.Display.getHeight();
graphics.clear();
graphics.drawBitmap(0, 0, deviceWidth,
deviceHeight, tabBackGroundImage1, 0, 0);
graphics.drawText((getPreferredWidth()-getText())/2,(getPreferredHeight()-graphics.getFont().getHeight())/2,getText);
// super.paint(graphics);
}

Related

Put FieldManager at center of the screen in a BlackBerry app

I know about how to put field on the center of the screen with horizontally and vertically.
However I got success with the field, but I want to set the VerticalFieldManager or HorizontalFieldManage to the center of the screen.
My code is like below, but I am not able to set it at center of the screen.
final Bitmap scale = new Bitmap(Display.getWidth(),Display.getHeight());
_backgroundBitmap.scaleInto(scale, Bitmap.FILTER_LANCZOS);
//==============================
// this manager is used for the static background image
mainManager = new VerticalFieldManager(Manager.USE_ALL_HEIGHT | Manager.USE_ALL_WIDTH | Manager.VERTICAL_SCROLL) {
public void paint(Graphics graphics) {
graphics.clear();
graphics.drawBitmap(0, 0, deviceWidth, deviceHeight,scale, 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);
}*/
};
VerticalFieldManager dataManager = new VerticalFieldManager(){};
dataManager.setMargin(20, 20, 20, 20);
//CategoryListLayout
//===============================================================================
//====================================
HorizontalFieldManager categoryLayout = new HorizontalFieldManager(FIELD_VCENTER | USE_ALL_WIDTH | FIELD_HCENTER){
protected void paint(Graphics graphics) {
graphics.setColor(0xFFFFFFFF);
super.paint(graphics);
}
};
LabelField cateName = new LabelField("Category", FIELD_VCENTER){
protected void layout(int width, int height) {
super.layout(width, height);
this.setExtent(150, this.getHeight());
}
};
categoryLayout.setBorder(myBorder);
//choice_country.setMinimalWidth(30);
choice_country.setMargin(10, 10, 10, 10);
categoryLayout.add(cateName);
categoryLayout.add(new BitmapField(Bitmap.getBitmapResource("vertical_line.png"), FIELD_VCENTER));
categoryLayout.add(choice_country);
dataManager.add(categoryLayout);
//===============================================================================
//DistanceListLayout
//===============================================================================
HorizontalFieldManager distanceLayout = new HorizontalFieldManager(FIELD_VCENTER | USE_ALL_WIDTH | FIELD_HCENTER){
protected void paint(Graphics graphics) {
graphics.setColor(0xFFFFFFFF);
super.paint(graphics);
}
};
LabelField distName = new LabelField("Distance", FIELD_VCENTER){
protected void layout(int width, int height) {
super.layout(width, height);
this.setExtent(150, this.getHeight());
}
};
distanceLayout.setBorder(myBorder);
//choice_distance.setMinimalWidth(300);
choice_distance.setMargin(10, 10, 10, 10);
distanceLayout.add(distName);
distanceLayout.add(new BitmapField(Bitmap.getBitmapResource("vertical_line.png"), FIELD_VCENTER));
distanceLayout.add(choice_distance);
dataManager.add(distanceLayout);
//===============================================================================
listNames = new Vector();
listImage = new Vector();
//new GetList().execute(null);
ButtonField b_search = new ButtonField("Search", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER);
b_search.setMargin(10,10,10,10);
b_search.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if (choice_country.getSelectedIndex() != 0
|| choice_distance.getSelectedIndex() != 0) {
new SubmitSearch().execute(null);
} else {
Dialog.alert("Select atleast One of Two(Category/Distance).");
}
}
});
dataManager.add(b_search);
removeAllScreen();
subManager.add(dataManager);
mainManager.add(subManager);
add(mainManager);
What's wrong in my code?
Update
Here I am using mainManager to display the Background of the app.
subManager is just for container. datamanager is to include mane HorizontalFieldManager.
Now I want is the datamanager is to be display vertically at center of the screen. It not depend on the HorizontalLayout I am going to add in it.
VerticalManager will ignore any vertical modificators like FIELD_VCENTER.
You have to implement your own Manager which will put submanager to center of the screen. Something like this:
protected void sublayout(int maxWidth, int maxHeight) {
submanager.sublayout(displayWidth, displayHeight);
//after sublayout submanager knows his real dimensions
int submanagerWidth = submanager.getWidth();
int submanagerHeight = submanager.getHeight();
int x = (displayWidth - submanagerWidth) >> 1;
int y = (displayHeight - submanagerHeight) >> 1;
setPositionChild(submanager, x, y);
setExtent(displayWidth, displayHeight);
}
In example I assumed that submanager is only one child. But is there are several it's clear how to modify example.
UPDATE
If there are several children (for example 2):
protected void sublayout(int maxWidth, int maxHeight) {
int height = 0;
for (int i = 0; i < 2; i++) {
submanager[i].sublayout(displayWidth, displayHeight);
height += submanager[i].getHeight();
}
int y = (displayHeight - height) >> 1;
if (y < 0)
y = 0;
for (int i = 0; i < 2; i++) {
int submanagerWidth = submanager[i].getWidth();
int submanagerHeight = submanager[i].getHeight();
int x = (displayWidth - submanagerWidth) >> 1;
setPositionChild(submanager[i], x, y);
y += submanagerHeight;
}
setExtent(displayWidth, max(height, submanagerHeight));
}
Why don't you use something like this?
mainManager = new VerticalFieldManager(Manager.USE_ALL_HEIGHT |
Manager.USE_ALL_WIDTH |
Manager.VERTICAL_SCROLL |
DrawStyle.HCENTER /* or DrawStyle.VCENTER */)

How i cn set Backgroungd image in Blackberry?

i want to set background image in Vertical field manager. i had try but it not fill with height you can see that in my screen. and Following in my code what i am doing mistake please help me.
vfmCenter = new VerticalFieldManager(FIELD_HCENTER)
{
public void paint(Graphics graphics)
{
graphics.clear();
graphics.drawBitmap(0, 0,deviceWidth,deviceHeight,newimg,0,0);
super.paint(graphics);
}
protected void sublayout( int maxWidth, int maxHeight)
{
int width = Display.getWidth();
int height = Display.getHeight();
super.sublayout( width, height);
setExtent( width, height);
}
};
vfmMain.add(vfmCenter);
this.add(vfmMain);
I was also facing the same issue earlier.. I solved my problem with this..
class MyClass extends MainScreen
{
// function for scaling your image to fit the screen
public EncodedImage scaleImage(EncodedImage source, int requiredWidth, int requiredHeight)
{
int currentWidthFixed32 = Fixed32.toFP(source.getWidth());
int requiredWidthFixed32 = Fixed32.toFP(requiredWidth);
int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
int currentHeightFixed32 = Fixed32.toFP(source.getHeight());
int requiredHeightFixed32 = Fixed32.toFP(requiredHeight);
int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);
return source.scaleImage32(scaleXFixed32, scaleYFixed32);
}
public MyClass
{
ei = EncodedImage.getEncodedImageResource("res/background_image");
ei1= scaleImage(ei,requires_width,required_height);
vfm= new VerticalFieldManager(VerticalFieldManager.USE_ALL_HEIGHT|VerticalFieldManager.USE_ALL_WIDTH);
vfm.setBackground(BackgroundFactory.createBitmapBackground(ei1.getBitmap()));
vfm.add(new LabelField("hello notice the background behind me");
add(vfm);
}
}
Try this. I think it will work for you!!
Try like this:
VerticalFieldManager vertical=new VerticalFieldManager()
{
protected void paint(Graphics g)
{
g.drawBitmap(0, 0,Display.getWidth(), Display.getHeight(), bitmap, 0, 0);
super.paint(g);
}
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(Display.getWidth(),Display.getHeight());
setExtent(Display.getWidth(),Display.getHeight());
}
};
add(vertical);
you can get.
This is the simple using getMainManager but whole background not a particular Field Manager
getMainManager().setBackground(BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("sample.png")));

BlackBerry 6.0 bitmap background using paintBackground issues with scrolling

I'm having a very strange problem with a background behind a VerticalFieldManager.
I have a Manager (NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL) that draws the background in paintBackground method which has 1 child:
VerticalFieldManager(VERTICAL_SCROLL | VERTICAL_SCROLLBAR) which stores the fields that need to be scrolled.
When the VerticalFieldManager scrolls the Manager background starts moving (and its choppy). When I use the setBackground to set the background image everything works fine, but I need the support of 4.5+. Anyone experienced this before?
my screen class:
public class RMainScreen extends MainScreen {
EncodedImage fon = EncodedImage.getEncodedImageResource("background_480x360.png");
HorizontalFieldManager content;
public RMainScreen() {
super(USE_ALL_WIDTH | USE_ALL_HEIGHT | NO_VERTICAL_SCROLL | NO_VERTICAL_SCROLLBAR);
content = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH | HorizontalFieldManager.NO_VERTICAL_SCROLL | HorizontalFieldManager.NO_VERTICAL_SCROLLBAR) {
protected void paintBackground(Graphics g) {
g.drawImage(0, 0, fon.getWidth(), fon.getHeight(), fon, 0, getLeft(), getTop());
}
};
VerticalFieldManager list = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH | VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR);
for(int i=0; i<40; i++) {
list.add(new LabelField("item from list " + i, LabelField.FOCUSABLE) {
protected void paint(Graphics g) {
g.setColor(Color.WHITE);
super.paint(g);
}
});
}
content.add(list);
add(content);
}
}
Try this one, use this code in your class constructor.
Bitmap topBg = Bitmap.getBitmapResource(ImageName.topbar);
final Bitmap topBg1 = resizeBitmap(topBg, SCREEN_WIDTH, topBg.getHeight());
VerticalFieldManager vfmTop = new VerticalFieldManager(Field.USE_ALL_WIDTH | USE_ALL_WIDTH)
{
protected void paintBackground(Graphics graphics)
{
graphics.drawBitmap(0,0,SCREEN_WIDTH,topBg1.getHeight(), topBg1,0,0 );
super.paint(graphics);
}
protected void sublayout(int maxWidth, int maxHeight) {
// TODO Auto-generated method stub
super.sublayout(topBg1.getWidth(), topBg1.getHeight());
setExtent(topBg1.getWidth(), topBg1.getHeight());
}
};
public static Bitmap resizeBitmap(Bitmap image, int width, int height)
{
int rgb[] = new int[image.getWidth() * image.getHeight()];
image.getARGB(rgb, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
int rgb2[] = rescaleArray(rgb, image.getWidth(), image.getHeight(), width, height);
Bitmap temp2 = new Bitmap(width, height);
temp2.setARGB(rgb2, 0, width, 0, 0, width, height);
return temp2;
}

Why does my rectangular bitmap get clipped to a square when I draw it?

I’m creating a custom field to make a image button. The image is drawn as a box with the w and h equal, but the image's height is almost double the width. I ran the debugger and the correct w,h are being put in g.drawBitmap(0, 0, w, h, image, 0, 0). Is thee a way to fix this?
public class cPictureButton extends Field{
private Bitmap image;
public cPictureButton( Bitmap image, long style)
{
super(style);
this.image=image;
}
public int getPreferredHeight()
{
return image.getHeight();
// return getFont().getHeight();
}
public int getPreferredWidth()
{
return image.getWidth();
// return getFont().getAdvance(label)+8;
}
protected void drawFocus(Graphics g, boolean on)
{
}
protected void paint(Graphics g)
{
int w=image.getWidth();
int h=image.getHeight();
g.drawBitmap(0, 0, w, h, image, 0, 0);
if (isFocus() )
g.drawRect(0,0,image.getWidth(), image.getHeight());
}
protected void layout(int width, int height) {
setExtent(Math.min(width, getPreferredWidth()),
Math.min(height, getPreferredWidth()));
}
public boolean isFocusable() {
return true;
}
protected boolean navigationClick(int status, int time)
{
fieldChangeNotify(0);
return true;
}
}
Copy and paste probably did you in. Second argument to setExtent should call getPreferredHeight():
setExtent(Math.min(width, getPreferredWidth()),
Math.min(height, getPreferredWidth()))

Touch screen keyboard not displaying correctly

When the user hovers over editfield below the touch screen keyboard does not display. What is displayed is a white area that is same size as keyboard. This is an issue on BlackBerry Torch. I would expect the touch screen keyboard to appear correctly using below code ?
BasicEditField nameField =
new BasicEditField(
" "+Local.getInfo8()+ " : ", "", 100, BasicEditField.FILTER_DEFAULT)
{
private int iRectX = getFont().getAdvance(getLabel());
private int iRectWidth = backgroundButtonBitmap.getWidth() - iRectX - 4;
public int getPreferredHeight() {
return backgroundButtonBitmap.getHeight() / 2;
}
public void layout(int width, int height) {
//setExtent(width, getPreferredHeight());
super.layout(width, getPreferredHeight());
}
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.setBackgroundColor(Color.WHITE);
//g.drawRect(iRectX, 0, iRectWidth, 20);
super.paint(g);
}
};
On the display screen, the method sublayout was being overriden -
protected void sublayout( int maxWidth, int maxHeight ) {
super.sublayout( maxWidth, maxHeight );
setExtent(maxWidth,Constants.BACKGROUND_IMAGE.getHeight());
}
The line - setExtent(maxWidth,Constants.BACKGROUND_IMAGE.getHeight()); is causing the issue.

Resources