Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
lostInTransit
Posts: 55
Registered: ‎03-26-2009
Accepted Solution

Custom Button with a background image had white area around it

Hi

 

I have a MainScreen with a black background and I am trying to add a custom button on top of it with an image as its background. But the custom button has a white border around it.

 

Is there any way to remove that white border or make it black?

 

Thanks

Please use plain text.
Developer
mantaker
Posts: 1,477
Registered: ‎12-30-2008

Re: Custom Button with a background image had white area around it

Override paint method of that button and paint black background and see if it works..

 

Cheers... 

--
Manimaran Selvan
Co-Founder, Tech Lead,
Equity Markets Research Group
Please use plain text.
Developer
lostInTransit
Posts: 55
Registered: ‎03-26-2009

Re: Custom Button with a background image had white area around it

Thanks mantaker

 

Tried that already before posting the question. It doesn't make any difference. Even the getPreferredHeight and width methods return the exact height and width of the background image. But the border still persists. 

Please use plain text.
Developer
mantaker
Posts: 1,477
Registered: ‎12-30-2008

Re: Custom Button with a background image had white area around it

Can you post the piece of code so that I can test it my side?

 

Cheers.. 

--
Manimaran Selvan
Co-Founder, Tech Lead,
Equity Markets Research Group
Please use plain text.
Developer
lostInTransit
Posts: 55
Registered: ‎03-26-2009

Re: Custom Button with a background image had white area around it

Thanks. Am away from my workstation at the moment. Will do that as soon as I get access to my machine.
Please use plain text.
Developer
mantaker
Posts: 1,477
Registered: ‎12-30-2008

Re: Custom Button with a background image had white area around it

Your MainScreen background is Black and you have a custom button(button with image) on which the border is shown white.. Am I right?

 

I will try to reproduce this issue on my side..

 

Cheers.. 

--
Manimaran Selvan
Co-Founder, Tech Lead,
Equity Markets Research Group
Please use plain text.
Developer
lostInTransit
Posts: 55
Registered: ‎03-26-2009

Re: Custom Button with a background image had white area around it

Posting the code

This is the custom main screen (created to have a black background)

 

public class CustomMainScreen extends MainScreen { private VerticalFieldManager container; public CustomMainScreen(String title) { super(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR); setTitle(title); VerticalFieldManager internalManager = new VerticalFieldManager( Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR) { public void paintBackground(Graphics g) { g.clear(); int color = g.getColor(); g.setColor(Color.BLACK); g.fillRect(0, 0, Display.getWidth(), Display.getHeight()); g.setColor(color); } protected void sublayout(int maxWidth, int maxHeight) { /*super.sublayout(maxWidth, maxHeight); XYRect extent = getExtent(); int width = Math.max(extent.width, Display.getWidth()); int height = Math.max(extent.height, Display.getHeight()); setExtent(width, height);*/ Field titleField = getMyTitleField(); int titleFieldHeight = 0; if(titleField != null){ titleFieldHeight = titleField.getHeight(); } int displayWidth = Display.getWidth(); int displayHeight = Display.getHeight(); super.sublayout(displayWidth, displayHeight - titleFieldHeight); setExtent(displayWidth, displayHeight - titleFieldHeight); } }; container = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR); internalManager.add(container); super.add(internalManager); } public void add(Field field) { container.add(field); } private Field getMyTitleField() { Manager delegate = getDelegate(); Field titleField = null; try{ titleField = delegate.getField(0); } catch(Exception e){ e.printStackTrace(); } return titleField; } }

 

 

Here's the Main screen which is actually displayed

 

public class MyMainScreen extends CustomMainScreen implements FieldChangeListener{ MyApplication _uiApp; private VerticalFieldManager hfm = new VerticalFieldManager(VerticalFieldManager.FIELD_VCENTER | Manager.FIELD_HCENTER); public MyMainScreen(){ super("Test Title"); _uiApp = (MyApplication) UiApplication.getUiApplication(); Bitmap myBitmap = Bitmap.getBitmapResource("icImg.png"); BitmapField icImg = new BitmapField(myBitmap, BitmapField.NON_FOCUSABLE); hfm.add(icImg); RichTextField rtf = new RichTextField(Field.NON_FOCUSABLE){ public void paint(Graphics graphics){ graphics.setBackgroundColor(Color.BLACK); graphics.clear(); super.paint(graphics); } public int getPreferredHeight(){ return 50; } }; hfm.add(rtf); CustomButton installBtn = new CustomButton("btn_over.png", "btn.png", Field.FOCUSABLE); installBtn.setChangeListener(this); hfm.add(installBtn); add(hfm); } public boolean keyChar(char key, int status, int time) { boolean retval = false; switch (key) { case Characters.ESCAPE: close(); retval = true; break; } return retval; } public boolean onClose() { // exit the application if user chooses to close it from main menu System.exit(0); return true; } public void fieldChanged(Field field, int context) { if (field instanceof CustomButton) { //go to next screen where we start installing the tones OtherScreen scr = new OtherScreen(); _uiApp.pushScreen(scr); } } }

 

 

This is the custom button class

 

public class CustomButton extends Field { private Bitmap _currentPicture; private Bitmap _onPicture; private Bitmap _offPicture; CustomButton (String onImage, String offImage, long style){ super(style); _offPicture = Bitmap.getBitmapResource(offImage); _onPicture = Bitmap.getBitmapResource(onImage); _currentPicture = _onPicture; } public int getPreferredHeight(){ return 45; } public int getPreferredWidth(){ return 170; } protected void onFocus(int direction){ _currentPicture = _offPicture; invalidate(); } protected void onUnfocus(){ _currentPicture = _onPicture; invalidate(); } protected void layout(int width, int height) { setExtent(Math.min( width, getPreferredWidth()), Math.min( height, getPreferredHeight())); } protected void paint(Graphics graphics) { graphics.setBackgroundColor(Color.BLACK); graphics.clear(); graphics.fillRect(0, 0, getWidth(), getHeight()); graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0); } protected boolean navigationClick(int status, int time){ fieldChangeNotify(1); return true; } }

 

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008

Re: Custom Button with a background image had white area around it

Is the custom button the only focusable field, or do you see it also when it does not have focus? Try overriding drawFocus() if it is only when there is focus.
Please use plain text.
Developer
lostInTransit
Posts: 55
Registered: ‎03-26-2009

Re: Custom Button with a background image had white area around it

[ Edited ]

Thanks Martin. That works.

 

Can someone also please tell me why HCENTER and VCENTER don't work with the above code. My fields are all left aligned no matter what style I give for the VerticalFieldManager in MyMainScreen.

 

Thanks again

Message Edited by lostInTransit on 04-19-2009 02:37 AM
Please use plain text.
Developer
dkumar
Posts: 113
Registered: ‎04-09-2009

Re: Custom Button with a background image had white area around it

Hi, I have exactly same problem. I tried to override drawFocus(), but I still see a black color when the custom button is on focus. Can you share the working piece of code ? Thanks in advance.. Regards, -DK
Please use plain text.