BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 09-13-2010, 09:47 AM   #1 (permalink)
Knows Where the Search Button Is
 
Join Date: Jul 2008
Location: India
Model: 9000
Carrier: AirTel
Posts: 29
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Custom Editfield for BlackBerry applications

Please Login to Remove!

Here is a custom editfield that i created . It has options to render the text.
Code:
package customfields;

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class CustomEditField extends HorizontalFieldManager implements DrawStyle
    {
    	private int _managerWidth	= 0;
    	private int _managerHeight	= 0;
    	private BasicEditField _editField	= null;
    	HorizontalFieldManager _hfm = null;
    	boolean _optionalFlag	= false;
    	Graphics _graphics 		= null;
    	int _autoScrollPos		= 0;
    	int _scrollAmount		= 0;
    	VerticalFieldManager _scollableManager = null;
    	String OPTIONAL_STRING = "(Optional)";
    	long _fieldStyle = 0;
    	/**
    	 * @param width width of the field
    	 * @param height height of the field
    	 * @param optional wether this is an optional field or not
    	 * @param scrollPos scrolling position index. 1 for 1st field. 2 for second field
    	 * @param scollableManager vertical field manager where the fields are laying.
    	 * @param fieldStyle preffered field style.
    	 */
    	public CustomEditField(int width, int height, boolean optional,int scrollPos, VerticalFieldManager scollableManager ,long fieldStyle) 
    	{
    		super(HorizontalFieldManager.NO_HORIZONTAL_SCROLL);
    		_managerWidth 	= width;
    		_managerHeight 	= height;
    		_optionalFlag   = optional;
    		_autoScrollPos	= scrollPos;
    		_scrollAmount	= _autoScrollPos * 40;
    		_scollableManager = scollableManager;
    		_fieldStyle		= fieldStyle;
    		_hfm = new HorizontalFieldManager(HorizontalFieldManager.HORIZONTAL_SCROLL);
    		
    		_editField = new BasicEditField("", "", 50,FIELD_HCENTER | EditField.NO_NEWLINE | _fieldStyle)
    		{
    			public void paint(Graphics graphics) 
    			{
    				if(getText().trim().length() > 0)
    				{
    					_graphics = graphics;
    					graphics.setColor(Color.BLACK);
    					if(isFocus())
    					{
    						graphics.drawText(getText(), 0, 0);
    					}
    					else 
        				{
        					graphics.drawText(getText(), 0, 0,DrawStyle.ELLIPSIS,_managerWidth);
        				}
    				}
    				else if(_optionalFlag)
    				{
    					graphics.setColor(Color.GRAY);
    					
    					graphics.drawText(OPTIONAL_STRING, 0, 0);
    					
    					if(!isFocus() && getText().trim().length() > 0)
    					{
    						graphics.drawText(getText(), 0, 0,DrawStyle.ELLIPSIS,_managerWidth);
    					}
    				}
    			}
    			protected boolean keyDown(int keycode, int time) 
    	    	{
    				char keyChar = Keypad.map(keycode);
    	    		if(keyChar == Characters.ENTER && _scollableManager != null)
    	    		{
    	    			_scollableManager.setVerticalScroll(_scrollAmount);
    	    		}
    	    		return super.keyDown(keycode, time);
    	    	}
    			
    		};
    		_hfm.add(_editField);
    		
    		add(_hfm);
    	}
    	
    	public void setEditable(boolean editable) 
    	{
    		_editField.setEditable(editable);
    	}
    	
    	public void setMaxChar(int maxChar)
    	{
    		_editField.setMaxSize(maxChar);
    	}
    	
    	public void setFocus() 
    	{
    		_editField.setFocus();
    	}
    	
    	public void focusChangeNotify(int arg0) 
    	{
    		if(!_editField.isFocus() )
    		{
    			if(_editField.getText().trim().length() > 0)
    			{
    				_hfm.deleteAll();
    				_editField.setText(_editField.getText().trim());
    				_hfm.add(_editField);
    			}
    			
    		}
    		else
    		{
    			if(_scollableManager != null)
    				_scollableManager.setVerticalScroll(_scrollAmount);
    		}
    		super.focusChangeNotify(arg0);
    	}
    	
    	public void sublayout(int width, int height) 
    	{
    		if (_managerWidth == 0) 
    			_managerWidth = 260;
    		
    		if (_managerHeight == 0) 
    			_managerHeight = 25;
    		
    		super.sublayout(_managerWidth, _managerHeight);
    		setExtent(_managerWidth, _managerHeight);
    	}
    	
		protected void paint(Graphics graphics) 
		{
			graphics.clear(0, 0, _managerWidth, _managerHeight);
			super.paint(graphics);
		}
		
    	public String getTextString() 
    	{
    		return _editField.getText();
    	}

    	public void setTextString(String text)
    	{
    		_editField.setText(text);
    	}
    }
You can use this for multiple purpose. Sample image of the field is attached.
Attached Images
File Type: png textfield.png (9.0 KB, 89 views)
__________________
Praveen K

Last edited by pravipravi : 09-13-2010 at 09:50 AM. Reason: constructor name changed for simplicity
Offline  
Old 09-13-2010, 09:50 AM   #2 (permalink)
Knows Where the Search Button Is
 
Join Date: Jul 2008
Location: India
Model: 9000
Carrier: AirTel
Posts: 29
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Attached image is not clear please try out the code ...
__________________
Praveen K
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.