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
Trusted Contributor
gopi
Posts: 126
Registered: ‎10-15-2010
My Carrier: Not specified
Accepted Solution

Auto capitalize text

Hi to all, is there any possibility to auto capitalize any word that begins after a space in the AutoTextEditField or in Editfield.
Example text:
Company Is Blackberry


Or I should customize the Editfield please suggest your ideas.

Please use plain text.
Developer
peter_strange
Posts: 19,510
Registered: ‎07-14-2008

Re: Auto capitalize text

I am not aware of a Field that provides this functionality.  I think you will have to do this yourself.

 

I suspect you can do this using a TextFilter and if you can, then this would be the most efficient, but I have never tried doing anything like this. 

 

I have done something similar in a FieldChangeListener in the fieldChanged() method - be careful to ignore recursive calls. 

Please use plain text.
Trusted Contributor
gopi
Posts: 126
Registered: ‎10-15-2010
My Carrier: Not specified

Re: Auto capitalize text

[ Edited ]

Okay peter, i will try using Textfilter. Now I'm trying by fieldchangeListener and let you know if i succeeded.

Please use plain text.
Trusted Contributor
gopi
Posts: 126
Registered: ‎10-15-2010
My Carrier: Not specified

Re: Auto capitalize text

Thank you for the suggestions, I tried autocaps using fieldchange listener and the output is like this only
CompanyIsBlackberry
I can't able to replace empty space in setText, please suggest me where i was wrong.

class AutoCapsTextbox extends Manager {
		private int managerWidth;
		private int managerHeight;
		private int arcWidth;		
		private VerticalFieldManager vfm = new VerticalFieldManager(
				NO_VERTICAL_SCROLL | USE_ALL_WIDTH );
		private AutoTextEditField editField;
		private Bitmap bagBitmap;		
		int fntSize=26,txt_color=Color.BLACK;		

		AutoCapsTextbox(final int width, final int height, long style,boolean braces) {
			super(style | NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL);
			managerWidth = width;
			managerHeight = height;		

			long innerStyle = style & (READONLY | FOCUSABLE_MASK); // at least
			if (innerStyle == 0) {
				innerStyle = FOCUSABLE;
			}			
			EncodedImage en = EncodedImage.getEncodedImageResource("images/email_box.png");
			bagBitmap = Utilities.getScaledBitmapImage(en, width, height);
			editField = new AutoTextEditField("", "", 40, innerStyle){
				public void paint(Graphics g) {
					FontFamily fontFamily[] = FontFamily.getFontFamilies();
					g.setColor( txt_color);
					this.setFont(fontFamily[Globals.VersionFont].getFont(FontFamily.SCALABLE_FONT,fntSize).derive(Font.PLAIN, fntSize,Ui.UNITS_px));
					super.paint(g);
				}
			};
			editField.setChangeListener(CapsListener);
			editField.getPaddingLeft();
			arcWidth = 10; // make it even
			add(vfm);
			vfm.add(editField);
		}

		public String getText() {
			return editField.getText();
		}
		public void setText(String newText) {
			editField.setText(newText);
		}
		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);
			super.paint(g);
		}
		FieldChangeListener CapsListener = new FieldChangeListener() {		
			public void fieldChanged(Field field, int context) {
				int i = editField.getText().length();			
				if(i > 2){			//Since first char is autocaps				
					char a = editField.getText().charAt(editField.getCursorPosition() - 2);
					if(a == ' '){
						char upr = 	CharacterUtilities.toUpperCase(editField.getText().charAt(editField.getCursorPosition()-1));
						String txt = editField.getText().substring(0, editField.getTextLength()-2);
						editField.setText(txt+upr);						 
					}
				}		 
			}
		};
	}

 If i did like this means am getting stackover flow error

editField.setText(txt+" "+upr);	
Please use plain text.
Developer
peter_strange
Posts: 19,510
Registered: ‎07-14-2008

Re: Auto capitalize text

I suspect this is because your FieldChangeListener does not distinguish between changes that your program makes and changes that your user makes. 

 

Probably the best way for you to prevent this in this case, is to ignore your checking process if the change was generated by your program,  You can do that quite easily by just adding the following lines at the beginning of your Listener code.

 

if ( context == FieldChangeListener.PROGRAMMATIC ) {
    return;
}

 

Next time you get a StackOverflowError, just have a look at the stack trace - it will normally very quickly tell you what recursive processing your have in your application. 

 

Please use plain text.
Trusted Contributor
gopi
Posts: 126
Registered: ‎10-15-2010
My Carrier: Not specified

Re: Auto capitalize text

Thanks a lot peter, its working fine:Clap:.

Please use plain text.