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 */)
Related
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);
}
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")));
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;
}
subManager = new VerticalFieldManager()
{
protected void sublayout( int maxWidth, int maxHeight )
{
int displayWidth = 300;
int displayHeight = 200;
super.sublayout( displayWidth, displayHeight);
setExtent( displayWidth, displayHeight);
}
};
subManager.setBackground(BackgroundFactory.createSolidTransparentBackground(9800909,200));
/// add your component to this subManager/////////
subManager.add(new LabelField("NeelInfo", FIELD_TOP));
subManager.add(new LabelField("Email id", FIELD_HCENTER));
subManager.add(new LabelField("Phone No", FIELD_BOTTOM));
//add subManager over the mainManager
mainManager.add(subManager);
mainManager.add(subManager1);
you can use the absolute field manager in such contexts
In AbsoluteFieldManager you can add the fields at specified coordinates
such as
afm is the object of AbsoluteFieldManager
afm.add(YourFieldObj,xCoord,yCoord);
add(afm) .. adding it to the screen
you can write your code this way:
class ClassName extends MainScreen
{
AbsoluteFieldManager afm;
public ClassName()
{
\\your code
afm = new AbsoluteFieldManager()
{
public void paint(Graphics g)
{
\\this will draw the background with the image for u
g.drawpaint \\method which needs a bitmap
}
};
add(afm);
afm.add(FieldObj,xCoord,yCoord);
}
}
I hope this will help you :)
I'm creating a inner class in a method. After that I am accessing some statements like,
public class Test extends MainScreen
{
HorizontalFieldManager hfm;
Bitmap bitmap[] = new Bitmap[100];
BitmapField[] bitmapField = new BitmapField[100];
int countBitmap = 0;
Test()
{
VerticalFieldManager vfm_Main = new VerticalFieldManager();
hfm = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH);
vfm_Main.add(hfm);
add(vfm_Main);
}
void drawBitmap()
{
bitmap[countBitmap] = new Bitmap(100, 100);
bitmapField[countBitmap] = new BitmapField(bitmap[countBitmap]){
public void paint(Graphics g)
{
................
................
g.drawLine(x1,y1,x2,y2);
}
}
synchronized(UiApplication.getEventLock())
{
for(int i = 0 ; i < bitmapField.length; i++)
{
if(bitmapField[i] != null)
{
bitmapField[i].setBitmap(bitmap[i]);
}
}
hfm.add(bitmapField[countBitmap]);
countBitmap++;
But here the problem is after creating the bitmap & before creating the bitmapField, control goes to
synchronized(UiApplication.getEventLock()){hfm.add(bitmapField[countBitmap]); }
So before creating the bitmapField, it adds it in hfm.
So the output is coming like "Everytime a new BitmapField is added in hfm (means at the same position by replacing the previous one)". But I want the BitmapFields come next to each other in hfm.
How to do it?
Any solution why the control goes first to hfm.add() before the new bitmapField() inner class?
first of all, several suggestions about code:
see no reason using synchronized since it's UI thread
don't setBitmap, bitmap is already passed to BitmapField constructor
actually all BitmapFields come next to each other in hfm, to make it clear, I've add number to each one.
if you want some custom constructor or new fields in BitmapField, it's better to create new class as an extension of BitmapField
class TestScr extends MainScreen {
HorizontalFieldManager hfm;
Bitmap bitmap[] = new Bitmap[100];
BitmapField[] bitmapField = new BitmapField[100];
TestScr() {
hfm = new HorizontalFieldManager();
add(hfm);
drawBitmap();
}
void drawBitmap() {
for (int i = 0; i < 5; i++) {
bitmap[i] = new Bitmap(50, 50);
Graphics graphics = new Graphics(bitmap[i]);
graphics.setColor(Color.RED);
String number = Integer.toString(i);
Font font = graphics.getFont().derive(Font.BOLD, 40, Ui.UNITS_px);
graphics.setFont(font);
int textWidth = graphics.getFont().getAdvance(number);
int textHeight = graphics.getFont().getHeight();
int x = (bitmap[i].getWidth() - textWidth) / 2;
int y = (bitmap[i].getHeight() - textHeight) / 2;
graphics.drawText(number, x, y);
bitmapField[i] = new BitmapField(bitmap[i]) {
public void paint(Graphics g) {
super.paint(g);
int width = getWidth() - 1;
int height = getHeight() - 1;
g.setColor(Color.GREEN);
g.drawLine(0, 0, width, 0);
g.drawLine(width, 0, width, height);
g.drawLine(width, height, 0, height);
g.drawLine(0, height, 0, 0);
}
};
hfm.add(bitmapField[i]);
}
}
}