BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 11-17-2008, 04:25 PM   #1 (permalink)
New Member
 
Join Date: Nov 2008
Model: 8800
PIN: N/A
Carrier: TMobile
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Alternate for setPadding for a TextField

Please Login to Remove!

I have a text field that uses an image for the background. My problem is that without using the undocumented field.setPadding() method I am unable to position the label and cursor in the correct place. Can anyone suggest a better way to do this because I need to be able to build this application for 4.5

Code:
public class ImageTextField extends BasicEditField {
    protected ResourceBundle bundle = ResourceBundle.getBundle(RimLocatorResource.BUNDLE_ID,
    		RimLocatorResource.BUNDLE_NAME);
    
    private String label;
    private int labelHeight;
    private int labelWidth;
    private Font font;
    
    private Bitmap currentPicture;
    private Bitmap offFocus;
    private Bitmap onFocus;
    
    private AddressRecord addRecord;
        
    /**
     * Constructor.
     * @param text - the text to be displayed on the button
     * @param style - combination of field style bits to specify display
           attributes 
     */
    public ImageTextField(String text, String initVal, String imageType, int maxNumChars, long style) 
    {
        super(text, initVal, maxNumChars, style);
       
        font = getFont();
       
        label = text;
        //labelHeight = font.getHeight();
        //labelWidth = font.getAdvance(label);
        
        ImageSelector imageSel = new ImageSelector(imageType);
        offFocus = imageSel.getOffFocusBitmap();
        onFocus = imageSel.getOnFocusBitmap();
        
        currentPicture = offFocus;
        Background background = BackgroundFactory.createBitmapBackground(currentPicture);
        //setBackground(background);
        setPadding(getTopTextPadding(), 0, 0, getLeftTextPadding() + 10);
        
    }
    

	/**
     * Field implementation.
     * @see net.rim.device.api.ui.Field#getPreferredHeight()
     */
    public int getPreferredHeight() 
    {
        return currentPicture.getHeight();
    }

    /**
     * Field implementation.
     * @see net.rim.device.api.ui.Field#getPreferredWidth()
     */
    public int getPreferredWidth() 
    {
        return currentPicture.getWidth();
    }
    
    /**
     * Field implementation.
     * @see net.rim.device.api.ui.Field#layout(int, int)
     */
    

    protected void layout(int width, int height) 
    {
        
    	super.layout(width, height);
    	setExtent(Display.getWidth(), currentPicture.getHeight());
    	
    }
    


    /**
     * Field implementation.
     * @see net.rim.device.api.ui.Field#paint(Graphics)
     */
    
    protected void paintBackground(Graphics graphics) 
    {   
    	//System.out.println("Painting button field for "+ getText());
        // First draw the background colour and picture
        //graphics.setColor(Color.LIGHTPINK);
        //graphics.fillRect(0, 0, getWidth(), getHeight());
		
		if(getAddRecord() != null){
			if(label.equals(bundle.getString(RimLocatorResource.ZIP_CODE))){
				System.err.println("Setting zip: "+ getText());
				addRecord.setZip(getText());
			}
		}
        
        graphics.drawBitmap(getLeftTextPadding(), 0, currentPicture.getWidth(), currentPicture.getHeight(), currentPicture, 0, 0);
   
        // Then draw the text
        //graphics.setColor(Color.BLACK);
        //graphics.setFont(font);
        //graphics.drawText(label, getRightTextPadding() + 10, 15, (int)getStyle(), 
        //		getWidth());
            //(int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ),
            //getWidth() - 6 );
    }

    
/*	
	public int drawText(Graphics graphics, int offset, int length, int x,
			int y, DrawTextParam drawTextParam) {
		
		
		
		return super.drawText(graphics, offset, length, getRightTextPadding() + 10, 30, drawTextParam);
	}
*/

	public int getLeftTextPadding(){
		
		int hres = Display.getWidth();
    	int ltPadding = (hres - currentPicture.getWidth()) / 2;
    	
    	return ltPadding;
		
	}
	
	public int getTopTextPadding(){

		int topPadding = (currentPicture.getHeight() - getFont().getHeight()) / 2;
		return topPadding;
	}
Thanks for the help.
Offline  
Old 11-17-2008, 11:45 PM   #2 (permalink)
New Member
 
Join Date: Nov 2008
Model: 8800
PIN: N/A
Carrier: TMobile
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Has anyone else created an image for a background of an edit or textField? This is driving me crazy I can't seem to be able to get it right. I can't see to position the label or cursor in the middle of my image field. Any pointers for this?

Thanks again.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.