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
Regular Contributor
musmankhan90
Posts: 92
Registered: ‎03-09-2013
My Device: bold 9800

Image Alignment Issue in Manager Field

Hello Every One,

                            I am making an application in which images lie in button fields. But the problem is that i want to make my images centrally aligned in the center of button. My code is given below with the IMAGE Diagram. Kindly help me.

Thanks in advance.

 

 

 

 

BlogPic.PNG


public class TabHorizontalManager extends HorizontalFieldManager// implements FieldChangeListener

{
    /*int dynamicWidth = 0;
    Bitmap a;*/
    public TabHorizontalManager(String label)
    {
        super(Manager.HORIZONTAL_SCROLL | Manager.HORIZONTAL_SCROLLBAR | USE_ALL_WIDTH | USE_ALL_HEIGHT);
       
        ButtonField bf  = new ButtonField(ButtonField.CONSUME_CLICK)
        {
             boolean _inFocus = false;        
             
            protected void layout(int width, int height)
                {
                    super.layout(Display.getWidth()/4,80);
                    super.setExtent(Display.getWidth()/4,80);                        
                }
             
            public void onFocus(int direction)
                {
                     _inFocus = true;
                     this.invalidate();
                }
            
            public void onUnfocus()
                {//
                     _inFocus = false;
                     this.invalidate();
                }
            
            protected void paint(Graphics graphics)
            {
                    graphics.setGlobalAlpha(0);
                    graphics.setGlobalAlpha(255);
                      
               
                 if(_inFocus)
                      {//my_account_shaded.png
                              graphics.drawBitmap(MyScreen.dynamicWidth,0,getWidth(),getHeight(),Bitmap.getBitmapResource("my_account_shaded.png"),0,0);
                      }
                  else
                      {
                      
                                graphics.drawBitmap(MyScreen.dynamicWidth,0,getWidth(),getHeight(),MyScreen.a,0,0);
                      }

                  super.paint(graphics);

            }
            
            protected void drawFocus(Graphics g, boolean on)
                {
                }
             
            protected boolean navigationClick(int status, int time)
                {
                    fieldChangeNotify(0);
                    
                    return true;
                    
                }
         
        };
             bf.setBackground(BackgroundFactory.createSolidTransparentBackground(0, 0));
             bf.setBorder(BorderFactory.createSimpleBorder(new XYEdges(0,1,0,1), new XYEdges(0,0xA7A7A7,0,0xA7A7A7), Border.STYLE_SOLID));
             bf.setChangeListener(new FieldChangeListener()
             {
                   public void fieldChanged(Field field,int context)
                       {
                     Dialog.alert("sgasgdaggsd!");
                    UiApplication.getUiApplication().pushScreen(new SecondScreen());
                     /* if(TimeCalculator.isSessionExpired())
                           {    
                            Dialog.alert("Session has been expired!");
                               UiApplication.getUiApplication().pushScreen(new AgAppRegistration());    
                           }
                        else
                          UiApplication.getUiApplication().pushScreen(new AgAppRegistration());*/        
                          }
              });

        add(bf);
//=================================
        ButtonField bf1  = new ButtonField(ButtonField.CONSUME_CLICK)
        {
             boolean _inFocus = false;        
             
            protected void layout(int width, int height)
                {
                    super.layout(Display.getWidth()/4,80);
                    super.setExtent(Display.getWidth()/4,80);                        
                }
             
            public void onFocus(int direction)
                {
                     _inFocus = true;
                     this.invalidate();
                }
            
            public void onUnfocus()
                {
                     _inFocus = false;
                     this.invalidate();
                }
            
            protected void paint(Graphics graphics)
            {
                    graphics.setGlobalAlpha(0);
                    graphics.setGlobalAlpha(255);
                      
               
                 if(_inFocus)
                      {
                              graphics.drawBitmap(Display.getWidth()/18,0,getWidth(),getHeight(),Bitmap.getBitmapResource("transfers_shaded.png"),0,0);
                      }
                  else
                      {
                                graphics.drawBitmap(Display.getWidth()/18,0,getWidth(),getHeight(),Bitmap.getBitmapResource("transfers.png"),0,0);
                      }

                  super.paint(graphics);

            }
            
            protected void drawFocus(Graphics g, boolean on)
                {
                }
             
            protected boolean navigationClick(int status, int time)
                {
                    fieldChangeNotify(0);
                    
                    return true;
                    
                }
         
        };
             bf1.setBackground(BackgroundFactory.createSolidTransparentBackground(0, 0));
             bf1.setBorder(BorderFactory.createSimpleBorder(new XYEdges(0,1,0,1), new XYEdges(0,0xA7A7A7,0,0xA7A7A7), Border.STYLE_SOLID));
       
             bf1.setChangeListener(new FieldChangeListener()
             {
                   public void fieldChanged(Field field,int context)
                       {
                     Dialog.alert("Button 2 is Pressed!");
                     /* if(TimeCalculator.isSessionExpired())
                           {    
                            Dialog.alert("Session has been expired!");
                               UiApplication.getUiApplication().pushScreen(new AgAppRegistration());    
                           }
                        else
                          UiApplication.getUiApplication().pushScreen(new AgAppRegistration());        
                          }*/
                       }
              });
        add(bf1);
        
        ButtonField bf2  = new ButtonField(ButtonField.CONSUME_CLICK)
        {
             boolean _inFocus = false;        
             
            protected void layout(int width, int height)
                {
                    super.layout(Display.getWidth()/4,80);
                    super.setExtent(Display.getWidth()/4,80);                        
                }
             
            public void onFocus(int direction)
                {
                     _inFocus = true;
                     this.invalidate();
                }
            
            public void onUnfocus()
                {
                     _inFocus = false;
                     this.invalidate();
                }
            
            protected void paint(Graphics graphics)
            {
                    graphics.setGlobalAlpha(0);
                    graphics.setGlobalAlpha(255);
                      
               
                 if(_inFocus)
                      {
                              graphics.drawBitmap(Display.getWidth()/16,0,getWidth(),getHeight(),Bitmap.getBitmapResource("payments_shaded.png"),0,0);
                      }
                  else
                      {
                                graphics.drawBitmap(Display.getWidth()/16,0,getWidth(),getHeight(),Bitmap.getBitmapResource("payments.png"),0,0);
                      }

                  super.paint(graphics);

            }
            
            protected void drawFocus(Graphics g, boolean on)
                {
                }
             
            protected boolean navigationClick(int status, int time)
                {
                    fieldChangeNotify(0);
                    
                    return true;
                    
                }
         
        };
             bf2.setBackground(BackgroundFactory.createSolidTransparentBackground(0, 0));
             bf2.setBorder(BorderFactory.createSimpleBorder(new XYEdges(0,1,0,1), new XYEdges(0,0xA7A7A7,0,0xA7A7A7), Border.STYLE_SOLID));
       
             bf2.setChangeListener(new FieldChangeListener()
             {
                   public void fieldChanged(Field field,int context)
                       {
                     Dialog.alert("Button 3 is Pressed!");
                     /* if(TimeCalculator.isSessionExpired())
                           {    
                            Dialog.alert("Session has been expired!");
                               UiApplication.getUiApplication().pushScreen(new AgAppRegistration());    
                           }
                        else
                          UiApplication.getUiApplication().pushScreen(new AgAppRegistration());        
                          }*/
                       }
              });
        add(bf2);
        
        ButtonField bf3  = new ButtonField(ButtonField.CONSUME_CLICK)
        {
             boolean _inFocus = false;        
             
            protected void layout(int width, int height)
                {
                    super.layout(Display.getWidth()/4,80);
                    super.setExtent(Display.getWidth()/4,80);                        
                }
             
            public void onFocus(int direction)
                {
                     _inFocus = true;
                     this.invalidate();
                }
            
            public void onUnfocus()
                {
                     _inFocus = false;
                     this.invalidate();
                }
            
            protected void paint(Graphics graphics)
            {
                    graphics.setGlobalAlpha(0);
                    graphics.setGlobalAlpha(255);
                      
               
                 if(_inFocus)
                      {
                              graphics.drawBitmap(Display.getWidth()/16,0,getWidth(),getHeight(),Bitmap.getBitmapResource("contact_us_shaded.png"),0,0);
                      }
                  else
                      {
                                graphics.drawBitmap(Display.getWidth()/16,0,getWidth(),getHeight(),Bitmap.getBitmapResource("contact_us.png"),0,0);
                      }

                  super.paint(graphics);

            }
            
            protected void drawFocus(Graphics g, boolean on)
                {
                }
             
            protected boolean navigationClick(int status, int time)
                {
                    fieldChangeNotify(0);
                    
                    return true;
                    
                }
         
        };
        bf3.setBackground(BackgroundFactory.createSolidTransparentBackground(0, 0));
        bf3.setBorder(BorderFactory.createSimpleBorder(new XYEdges(0,1,0,1), new XYEdges(0,0xA7A7A7,0,0xA7A7A7), Border.STYLE_SOLID));
       
        bf3.setChangeListener(new FieldChangeListener()
             {
                   public void fieldChanged(Field field,int context)
                       {
                     Dialog.alert("Button 4 is Pressed!");
                     /* if(TimeCalculator.isSessionExpired())
                           {    
                            Dialog.alert("Session has been expired!");
                               UiApplication.getUiApplication().pushScreen(new AgAppRegistration());    
                           }
                        else
                          UiApplication.getUiApplication().pushScreen(new AgAppRegistration());        
                          }*/
                       }
              });
        add(bf3);
    }
    
    protected void sublayout(int maxWidth, int maxHeight)
        {    
           super.sublayout( Display.getWidth(),  50);
           setExtent( Display.getWidth(),  50);    
        }

    public void paintBackground(Graphics graphics)
        {
            graphics.setBackgroundColor(0x353535);
            graphics.clear();
          //  graphics.drawBitmap(Display.getWidth()-Bitmap.getBitmapResource("AG_logo.PNG").getWidth(), 0, Bitmap.getBitmapResource("AG_logo.PNG").getWidth(),Bitmap.getBitmapResource("AG_logo.PNG").getHeight(),Bitmap.getBitmapResource("AG_logo.PNG"),0,0);
            super.paintBackground(graphics);
        }
}

Regular Contributor
musmankhan90
Posts: 92
Registered: ‎03-09-2013
My Device: bold 9800

Re: Image Alignment Issue in Manager Field

Problem is that image alignment changes in landscape and in portrait. I want to make my image CETRALLY Alignned for that purpose.

Regular Contributor
musmankhan90
Posts: 92
Registered: ‎03-09-2013
My Device: bold 9800

Re: Image Alignment Issue in Manager Field

Issue hasbeen resolved by my own self.. I first dive the width screen by 4. Then again for the center alignment for the screen i again divide the width of that by by 4 to get the X-Alignment for image.

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Image Alignment Issue in Manager Field

calling setExtent after super.sublayout causes only trouble.

i suggest to check out this:
http://supportforums.blackberry.com/t5/Java-Development/Create-a-custom-layout-manager-for-a-screen/...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Image Alignment Issue in Manager Field

Agree with Simon. 

 

Also in paint it is possible to 'center' an image using code like the following:

 

Bitmap bitmap = Bitmap.getBitmapResource("my_account_shaded.png");

int fieldWidth = this.getWidth();

int fieldHeight = this.getHeight();

int bitmapWidth = bitmap.getWidth();

int bitmapHeight = bitmap.getHeight();

 

graphics.drawBitmap( (fieldWidth-bitmapWidth)/2, (fieldHeight - bitmapHeight) / 2, bitmapWidth, bitmapHeight, bitmap, 0, 0);