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
Posts: 71
Registered: ‎11-23-2009
My Device: Not Specified

How to draw Custom Focusable Color/Style for a focusable field?

I have custom drawn fields which are focusable. Normally the default focus color is Blue which obviously doesn't match to every theme. So can you give me ideas to change the color of the focus? Thanks

Developer
Posts: 528
Registered: ‎10-06-2009
My Device: 9800,BB Dev Alpha,z10 limited edition
My Carrier: Vodafone

Re: How to draw Custom Focusable Color/Style for a focusable field?

You can override OnFocus() method of the field ,then do the appropriate changes in paint() of that field

Rujuta Trivedi
Developer
Posts: 16,986
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to draw Custom Focusable Color/Style for a focusable field?

unless you change it the focus color is also determined by the theme.

----------------------------------------------------------
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
Posts: 71
Registered: ‎11-23-2009
My Device: Not Specified

Re: How to draw Custom Focusable Color/Style for a focusable field?

Can u help me with some hint how to paint onfocus and onunfocus?

 

 protected void onFocus(int direction) {
          super.onFocus(direction);
          Application.getApplication().invokeLater(new Runnable() {
                  public void run() {
                        focus=true;
                  }
          });
  }

	  protected void onUnFocus() {
		  super.onUnfocus();
          Application.getApplication().invokeLater(new Runnable() {
                  public void run() {
                          focus=false;
                  }
          });
  }

	protected void paint(Graphics graphics) {
		if (focus=true){
			graphics.setColor(Color.ORANGE);
		}else{
		graphics.setColor(Color.BLACK);
		}
		graphics.fillRoundRect(2, 0, 316, 46, 14, 10);
}

 

Developer
Posts: 528
Registered: ‎10-06-2009
My Device: 9800,BB Dev Alpha,z10 limited edition
My Carrier: Vodafone

Re: How to draw Custom Focusable Color/Style for a focusable field?

static boolean focus;

 

 protected void onUnfocus() {
        focus=false;
       
        invalidate();
    }

  

   

  
   
   
   
    protected void paint(Graphics graphics) {
      
           if(focus)
        graphics.setColor(Color.ORANGE);
        else
         graphics.setColor(Color.BLACK);
      
         graphics.fillRect(0,0 , 40, 40);
    }
}

 

 

Try this,this code works for me

Rujuta Trivedi
Developer
Posts: 71
Registered: ‎11-23-2009
My Device: Not Specified

Re: How to draw Custom Focusable Color/Style for a focusable field?

I needed smthing like to change the colors on focus n unfocus.

So that I may now the focus transfer. But its painting it only once.

Its not shifting the colors a/t focus change

Highlighted
Developer
Posts: 528
Registered: ‎10-06-2009
My Device: 9800,BB Dev Alpha,z10 limited edition
My Carrier: Vodafone

Re: How to draw Custom Focusable Color/Style for a focusable field?

I have made one custom field ,check this out

 


package com;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.*;
import java.util.*;

public class BigButtonField extends Field {

    private int backgroundColour = 16770273;
    private Bitmap button;
    private Bitmap on = Bitmap.getBitmapResource("Information.png");
    private Bitmap off = Bitmap.getBitmapResource("Information2.png");
   
    private int fieldWidth = Graphics.getScreenWidth();
    private int fieldHeight = 31; //31;
    private int buffer = 3;//(Graphics.getScreenWidth() - 105)/3;//(Graphics.getScreenWidth() - 105)/2;
    private String text;
    private int menuIndex;
    private Font fieldFont;
    private boolean focus;
   
    public String getLabel(){
        return text;
    }
    public int getMenuIndex(){
        return menuIndex;
    }
    public void setMenuIndex(int mnuIndex){
        this.menuIndex = mnuIndex;
    }
   
    public BigButtonField(String _text) {
        super(Field.FOCUSABLE);
        text = _text;
        button = off;
        fieldFont = FieldFont();
    }
   
    protected boolean navigationClick(int status, int time) {
        fieldChangeNotify(1);
        return true;
    }

    protected void onFocus(int direction) {
        button = on;
        focus = true;
        invalidate();
    }

    protected void onUnfocus() {
        button = off;
        focus = false;
        invalidate();
    }

    public int getPreferredWidth() {
        return fieldWidth;
    }

    public int getPreferredHeight() {
        return fieldHeight;
    }

    protected void layout(int arg0, int arg1) {
        setExtent(getPreferredWidth(), getPreferredHeight());
    }

    public static Font FieldFont() {
    try {
            FontFamily theFam = FontFamily.forName("SYSTEM");
            return theFam.getFont(net.rim.device.api.ui.Font.BOLD, 16);
         } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
         }
        return null;
     }

        protected void drawFocus(Graphics graphics, boolean on) {
            //
        }
   
        protected void fieldChangeNotify(int context) {
            try {
                this.getChangeListener().fieldChanged(this, context);
            } catch (Exception exception) {
            }
        }

    protected void paint(Graphics graphics) {
        graphics.setColor(Color.GRAY);
        graphics.fillRect(0, 0, fieldWidth, fieldHeight);
        graphics.drawBitmap(300, 3, fieldWidth, fieldHeight, button, 0, 0);
        if(focus)
            {graphics.setColor(Color.WHITE);

           graphics.fillRect(0, 0, fieldWidth, fieldHeight);}

        else

            {graphics.setColor(Color.BLACK);

           graphics.fillRect(0, 0, fieldWidth, fieldHeight);}           
        graphics.setFont(fieldFont);
            graphics.drawText(text, 0, (fieldHeight - fieldFont.getHeight()) / 2);
    }
}

It changes BGcolor and textcolor on foucs on n off

Rujuta Trivedi
Developer
Posts: 466
Registered: ‎03-04-2009
My Device: 8520
My Carrier: some

Re: How to draw Custom Focusable Color/Style for a focusable field?

To change focus color override the drawFocus(Graphics graphics, boolean b) method:

 

 

 

new CheckboxField() {
            protected void drawFocus(Graphics graphics, boolean b) {
		// put your code here to play with graphics
                super.drawFocus(graphics, b);
            }

 

 


 

Got resolved! Press kudo icon!
Developer
Posts: 50
Registered: ‎09-01-2009
My Device: Not Specified

Re: How to draw Custom Focusable Color/Style for a focusable field?

hi

i have done this try following code:

protected void drawFocus(Graphics graphics, boolean on) {
                    //System.out.println("drawFocus ignored: " + on);
                return;