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
Contributor
guganmsc
Posts: 37
Registered: ‎07-23-2011
My Device: 9700
Accepted Solution

Horizontal scroll for textfiled

I have created the textfield which have used image for focus,unfocus.I could't able to move the cursor horizontal scroll on textfield.If i removed the focus method to below constructor  it's work fine for both cursor postion and horizontal scroll . Please help me to set horizontal scroll(if the typing character exceeds textfield width).

 

class Textbox extends Manager {
	private int managerWidth;
	private int managerHeight;
	private int arcWidth;
	private VerticalFieldManager vfm = new VerticalFieldManager(
			NO_VERTICAL_SCROLL | USE_ALL_WIDTH |HORIZONTAL_SCROLL);
	private AutoTextEditField  editField;
	private Bitmap bagBitmap;
	int fntSize=26;
	String strhint,hint,str_img;
	Textbox(String _hint, final String image,final String focusImage, long style) {
		super(style | NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL);
		managerWidth = 300;
		managerHeight = 40;
		hint = _hint;
		str_img = image;
		long innerStyle = style & (READONLY | FOCUSABLE_MASK); // at least
		if (innerStyle == 0) {
			innerStyle = FOCUSABLE;
		}
		bagBitmap =Bitmap.getBitmapResource(str_img); // initialize the image to draw
		editField = new AutoTextEditField("", "", 50, innerStyle|AutoTextEditField.NO_NEWLINE){
				public void paint(Graphics g) {
					getManager().invalidate();
					FontFamily fontFamily[] = FontFamily.getFontFamilies();
					this.setFont(fontFamily[1].getFont(FontFamily.SCALABLE_FONT,fntSize).derive(Font.SIZE_MEDIUM, fntSize,Ui.UNITS_px));
					super.paint(g);
				}
				protected void onFocus(int direction){	
					str_img = focusImage;				
					bagBitmap = Bitmap.getBitmapResource(str_img);
					invalidate();
				}
				protected void onUnfocus(){	
					bagBitmap = Bitmap.getBitmapResource(str_img);
					invalidate();
				}
		};		
		arcWidth = 15;		
		vfm.add(editField);
		add(vfm);		
	}
	public void setFont(net.rim.device.api.ui.Font font) {
		FontFamily fontFamily[] = FontFamily.getFontFamilies();
		this.setFont(fontFamily[1].getFont(FontFamily.SCALABLE_FONT,fntSize).derive(Font.SIZE_MEDIUM, fntSize,Ui.UNITS_px));
		editField.setFont(fontFamily[1].getFont(FontFamily.SCALABLE_FONT,fntSize).derive(Font.SIZE_MEDIUM, fntSize,Ui.UNITS_px));
		arcWidth = editField.getFont().getHeight() & 0xFFFFFFFE;
		updateLayout();
	}	
	public int getPreferredWidth() {
		return managerWidth;
	}
	public int getPreferredHeight() {
		return managerHeight;
	}
	protected void sublayout(int w, int h) {
		if (managerWidth == 0) {
			managerWidth = w;
		}
		if (managerHeight == 0) {
			managerHeight = h;
		}
		int actWidth = Math.min(managerWidth, w);
		int actHeight = Math.min(managerHeight, h);
		layoutChild(vfm, actWidth - arcWidth, actHeight - arcWidth);
		setPositionChild(vfm, arcWidth / 2, arcWidth / 2);
		setExtent(actWidth, actHeight);
	}
	protected void paint(Graphics g) {
		g.drawBitmap(0, 0, getWidth(), getHeight(), bagBitmap, 0, 0);
		strhint = editField.getText();
		if ( strhint == null || strhint.length() < 1 ) {
			g.setColor( 0xAFAFAD );
			FontFamily fontFamily[] = FontFamily.getFontFamilies();
			net.rim.device.api.ui.Font font = 	fontFamily[1.getFont(FontFamily.SCALABLE_FONT,fntSize).derive(Font.SIZE_MEDIUM, fntSize,Ui.UNITS_px);
			g.setFont(font);
			g.drawText(hint, 10, 10, 30, 250);
		}
		super.paint(g);
	}
}

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Horizontal scroll for textfiled

I don't understand your problem so could you please explain this again.   There doesn't seem to be any focus/unfocus code in there?

 

But before you do, there are a few issues with your code that you should fix. 

 

In no particular order:

 

a) I would paint the 'hint' in the EditField rather than in this Field.  That way it will appear where the user types rather than being wherever "10, 10" is on your Field.

 

b) Move this code:

     FontFamily fontFamily[] = FontFamily.getFontFamilies();
this.setFont(fontFamily[1].getFont(FontFamily.SCALABLE_FONT,fntSize).derive(Font.SIZE_MEDIUM, fntSize,Ui.UNITS_px));

from your paint routine and put it in sublayout.  That is the point that it is needed.  Doing it in paint() means it gets done too frequently and is probably recursively called. 

 

As a result of doing this you should be able to remove

getManager().invalidate();

from paint() and

updateLayout();

from setFont().

 

c) When painting your hint, don't derive the font everytime:

   FontFamily fontFamily[] = FontFamily.getFontFamilies();
net.rim.device.api.ui.Font font = fontFamily[1.getFont(FontFamily.SCALABLE_FONT,fntSize).derive(Font.SIZE_MEDIUM, fntSize,Ui.UNITS_px);

This is inefficient, you only need to do it once and save a reference to it for use when painting your hint

 

Do all of these you will have a better Field and perhaps your problem will be resolved.  But if not, then please explain your problem again with more detail and repost your fixed code. 

 

 

 

Trusted Contributor
gopi
Posts: 126
Registered: ‎10-15-2010
My Device: Torch 9850

Re: Horizontal scroll for textfiled

Follow peter's suggestion first and use the below code for temporary solution. Remove the onfocus and unfocus method from the editfield and place it commonly to the manager.

 

class Textbox extends Manager {
	private int managerWidth;
	private int managerHeight;
	private int arcWidth;
	private VerticalFieldManager vfm = new VerticalFieldManager(
			NO_VERTICAL_SCROLL | USE_ALL_WIDTH |HORIZONTAL_SCROLL);
	private AutoTextEditField  editField;
	private Bitmap bagBitmap;
	int fntSize=26;
	String strhint,hint,str_img;
	Textbox(String _hint, final String image,final String focusImage, long style) {
		super(style | NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL);
		managerWidth = 300;
		managerHeight = 40;
		hint = _hint;
		str_img = image;
		long innerStyle = style & (READONLY | FOCUSABLE_MASK); // at least
		if (innerStyle == 0) {
			innerStyle = FOCUSABLE;
		}
		bagBitmap =Bitmap.getBitmapResource(str_img); // initialize the image to draw
		editField = new AutoTextEditField("", "", 50, innerStyle|AutoTextEditField.NO_NEWLINE){
				public void paint(Graphics g) {
					getManager().invalidate();
					FontFamily fontFamily[] = FontFamily.getFontFamilies();
					this.setFont(fontFamily[1].getFont(FontFamily.SCALABLE_FONT,fntSize).derive(Font.SIZE_MEDIUM, fntSize,Ui.UNITS_px));
					super.paint(g);
				}
				
		};		
		arcWidth = 15;		
		vfm.add(editField);
		add(vfm);		
	}
	public void setFont(net.rim.device.api.ui.Font font) {
		FontFamily fontFamily[] = FontFamily.getFontFamilies();
		this.setFont(fontFamily[1].getFont(FontFamily.SCALABLE_FONT,fntSize).derive(Font.SIZE_MEDIUM, fntSize,Ui.UNITS_px));
		editField.setFont(fontFamily[1].getFont(FontFamily.SCALABLE_FONT,fntSize).derive(Font.SIZE_MEDIUM, fntSize,Ui.UNITS_px));
		arcWidth = editField.getFont().getHeight() & 0xFFFFFFFE;
		updateLayout();
	}	
	protected void onFocus(int direction){	
		str_img = focusImage;				
		bagBitmap = Bitmap.getBitmapResource(str_img);					
	}
protected void onUnfocus(){ bagBitmap = Bitmap.getBitmapResource(str_img); super.onUnfocus(); } public int getPreferredWidth() { return managerWidth; } public int getPreferredHeight() { return managerHeight; } protected void sublayout(int w, int h) { if (managerWidth == 0) { managerWidth = w; } if (managerHeight == 0) { managerHeight = h; } int actWidth = Math.min(managerWidth, w); int actHeight = Math.min(managerHeight, h); layoutChild(vfm, actWidth - arcWidth, actHeight - arcWidth); setPositionChild(vfm, arcWidth / 2, arcWidth / 2); setExtent(actWidth, actHeight); } protected void paint(Graphics g) { g.drawBitmap(0, 0, getWidth(), getHeight(), bagBitmap, 0, 0); strhint = editField.getText(); if ( strhint == null || strhint.length() < 1 ) { g.setColor( 0xAFAFAD ); FontFamily fontFamily[] = FontFamily.getFontFamilies(); net.rim.device.api.ui.Font font = fontFamily[1.getFont(FontFamily.SCALABLE_FONT,fntSize).derive(Font.SIZE_MEDIUM, fntSize,Ui.UNITS_px); g.setFont(font); g.drawText(hint, 10, 10, 30, 250); } super.paint(g); } }

 

Contributor
guganmsc
Posts: 37
Registered: ‎07-23-2011
My Device: 9700

Re: Horizontal scroll for textfiled

thnaks peter .now I removed the font declaration on paint method as your suggestion and focus and unfocus method has been replaces commonly.now It's work fine.

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Horizontal scroll for textfiled

Excellent!

 

And apologies, I completely missed the focus methods in the EditField.  No idea how....  My bad.