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
kemdada
Posts: 37
Registered: ‎10-10-2008
My Device: Not Specified

Stop edit field from wrapping text

How do I stop an edit field from wrapping text?

 

I put an EditField inside a Horizontal Field Manager(with an image background). To look kind of like a google search Box. 

 

 My problem is that when the user enters more text that is larger than the size of the manager, the edit field starts wrapping into a new line. I put Manger.HorizontalScroll on the HFM but that didnt do it, instead it wraps unto the next line. 

 

Any ideas?

Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: Stop edit field from wrapping text

I think in such type situation you have to customize EditField extending Field.

 

Here is one of my CustomTextField that meets your requirement. This might help you.

 

import net.rim.device.api.ui.Manager; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.component.EditField; import net.rim.device.api.system.Display; import net.rim.device.api.system.Characters; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.Font; public class CustomTextBox extends Manager { private final static int DEFAULT_LEFT_MARGIN = 10; private final static int DEFAULT_RIGHT_MARGIN = 10; private final static int DEFAULT_TOP_MARGIN = 5; private final static int DEFAULT_BOTTOM_MARGIN = 5; private final static int DEFAULT_LEFT_PADDING = 10; private final static int DEFAULT_RIGHT_PADDING = 10; private final static int DEFAULT_TOP_PADDING = 5; private final static int DEFAULT_BOTTOM_PADDING = 5; private int topMargin = DEFAULT_TOP_MARGIN; private int bottomMargin = DEFAULT_BOTTOM_MARGIN; private int leftMargin = DEFAULT_LEFT_MARGIN; private int rightMargin = DEFAULT_RIGHT_MARGIN; private int topPadding = DEFAULT_TOP_PADDING; private int bottomPadding = DEFAULT_BOTTOM_PADDING; private int leftPadding = DEFAULT_LEFT_PADDING; private int rightPadding = DEFAULT_RIGHT_PADDING; private int totalHorizontalEmptySpace = leftMargin + leftPadding + rightPadding + rightMargin; private int totalVerticalEmptySpace = topMargin + topPadding + bottomPadding + bottomMargin; private int minHeight = getFont().getHeight() + totalVerticalEmptySpace; private int width = Display.getWidth(); private int height = minHeight; private EditField editField; public CustomTextBox() { super(0); editField = new EditField(); add(editField); } protected void sublayout(int width, int height) { Field field = getField(0); layoutChild(field, this.width - totalHorizontalEmptySpace, this.height - totalVerticalEmptySpace); setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding); setExtent(this.width, this.height); } public void setTopMargin(int topMargin) { this.topMargin = topMargin; } public void setBottomMargin(int bottomMargin) { this.bottomMargin = bottomMargin; } protected void paint(Graphics graphics) { graphics.drawRoundRect(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), 5, 5); EditField ef = (EditField)getField(0); String entireText = ef.getText(); String textToDraw = ""; Font font = getFont(); int availableWidth = width - totalHorizontalEmptySpace; if (font.getAdvance(entireText) <= availableWidth) { textToDraw = entireText; } else { int endIndex = entireText.length(); for (int beginIndex = 1; beginIndex < endIndex; beginIndex++) { textToDraw = entireText.substring(beginIndex); if (font.getAdvance(textToDraw) <= availableWidth) { break; } } } ef.setText(textToDraw); super.paint(graphics); } public int getPreferredWidth() { return width; } public int getPreferredHeight() { return height; } protected boolean keyChar(char ch, int status, int time) { if (ch == Characters.ENTER) { return true; } else { return super.keyChar(ch, status, time); } } public String getText() { return ((EditField)getField(0)).getText(); } public void setText(final String text) { ((EditField)getField(0)).setText(text); } }

 

Regards

Bikas

 

Developer
belberry
Posts: 265
Registered: ‎08-27-2009
My Device: Not Specified

Re: Stop edit field from wrapping text

Thank u..
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: Stop edit field from wrapping text

Hi Bikas, thanks for posting and helping others with your customised code.

 

 

Hi, kemdada,Try something like this... 

 

           EditField ef = new EditField(Field.NO_VERTICAL_SCROLL | Field.HORIZONTAL_SCROLL) ;

 

Note: the above code is not compiled.  

 

 

 

Thanks and Regards,

PraveenGoparaju 

Thanks and Regards,
PraveenGoparaju.
Developer
lsonu123
Posts: 88
Registered: ‎02-11-2009
My Device: Not Specified

Re: Stop edit field from wrapping text

HI bikas,

 

i have used ur code. but i  need only enter number in textbox and its scroll ,but when delete key press ,it not delete the number .how this possible,i want only enter number and it scroll.

 

 

thanks

Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: Stop edit field from wrapping text

Hi lsonu123,

 

Try with the following code. It should accept only numeric input and allow scroll.

 

 

import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Font;

public class NumericTextField extends Manager
{
   
    private final static int DEFAULT_LEFT_MARGIN = 10;
    private final static int DEFAULT_RIGHT_MARGIN = 10;
    private final static int DEFAULT_TOP_MARGIN = 5;
    private final static int DEFAULT_BOTTOM_MARGIN = 5;
    
    private final static int DEFAULT_LEFT_PADDING = 10;
    private final static int DEFAULT_RIGHT_PADDING = 10;
    private final static int DEFAULT_TOP_PADDING = 5;
    private final static int DEFAULT_BOTTOM_PADDING = 5;
    
    private int topMargin = DEFAULT_TOP_MARGIN;
    private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
    private int leftMargin = DEFAULT_LEFT_MARGIN;
    private int rightMargin = DEFAULT_RIGHT_MARGIN;
    
    private int topPadding = DEFAULT_TOP_PADDING;
    private int bottomPadding = DEFAULT_BOTTOM_PADDING;
    private int leftPadding = DEFAULT_LEFT_PADDING;
    private int rightPadding = DEFAULT_RIGHT_PADDING;
    
    private int totalHorizontalEmptySpace = leftMargin + leftPadding + rightPadding + rightMargin;
    private int totalVerticalEmptySpace = topMargin + topPadding + bottomPadding + bottomMargin;
    
    private int minHeight = getFont().getHeight() + totalVerticalEmptySpace;
    private int width = Display.getWidth();
    private int height = minHeight;
    
    
    private EditField editField;
    
    public NumericTextField()
    {
        super(0);
        
        editField = new EditField(EditField.FILTER_NUMERIC);
        add(editField);
    }    
    
    protected void sublayout(int width, int height)
    {
        Field field = getField(0);
        layoutChild(field, this.width - totalHorizontalEmptySpace, this.height - totalVerticalEmptySpace);
        setPositionChild(field, leftMargin+leftPadding, topMargin+topPadding);
        setExtent(this.width, this.height);
    }
    
    public void setTopMargin(int topMargin)
    {
        this.topMargin = topMargin;
    }
    
    public void setBottomMargin(int bottomMargin)
    {
        this.bottomMargin = bottomMargin;
    }
    
    
    protected void paint(Graphics graphics)
    {
        graphics.drawRoundRect(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), 5, 5);
        
        boolean longText = false;
        EditField ef = (EditField)getField(0);
        String entireText = ef.getText();
        
        String textToDraw = "";
        Font font = getFont();
        int availableWidth = width - totalHorizontalEmptySpace;
        if (font.getAdvance(entireText) <= availableWidth)
        {
            textToDraw = entireText;
        }
        else
        {
            int endIndex = entireText.length();
            for (int beginIndex = 1; beginIndex < endIndex; beginIndex++)
            {
                textToDraw = entireText.substring(beginIndex);
                if (font.getAdvance(textToDraw) <= availableWidth)
                {
                    longText = true;
                    break;
                }
            }
        }
        
        if (longText == true)
        {        
            ef.setText(textToDraw);
            super.paint(graphics);
            ef.setText(entireText);
        }
        else
        {
            super.paint(graphics);
        }
    }
    
    public int getPreferredWidth()
    {
        return width;
    }
    
    public int getPreferredHeight()
    {
        return height;
    }
    
    protected boolean keyChar(char ch, int status, int time)
    {
        if (ch == Characters.ENTER)
        {
            return true;
        }
        else
        {
            return super.keyChar(ch, status, time);
        }
    }
    
    public String getText()
    {
        return ((EditField)getField(0)).getText();
    }
    
    public void setText(final String text)
    {
        ((EditField)getField(0)).setText(text);
    }    
}

Please let us know if it works for you.

 

 

Regards

Bikas

 

Developer
lsonu123
Posts: 88
Registered: ‎02-11-2009
My Device: Not Specified

Re: Stop edit field from wrapping text

HI bikas,

 

It work but i want that when delete key press,it not delete the number that enter before  means delete key not works.we can only enter number in TextBox not delete that number that would be entered earlier.

 

thanks

Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: Stop edit field from wrapping text

[ Edited ]

Hi lsonu123,

 

 

protected boolean keyChar(char ch, int status, int time)
{
   //eliminate ENTER key
   if (ch == Characters.ENTER)
   {
      return true;
   }
   //eliminate delete key 
   else if (ch == '\b')
   {
      return true; 
   }
   else
   {
      return super.keyChar(ch, status, time);
   }
}

 

@Edit: typo

 

Regards

Bikas

New Developer
BBJDev
Posts: 50
Registered: ‎11-03-2009
My Device: Not Specified

Re: Stop edit field from wrapping text

Hi Experts,

 

I am using this same code for wrapping the text. But I an getting IllegalStackSizeException when the field is filled with text and I am trying to change the trackball.

 

Definetly some problem with onFocus() and onUnfocus().....

 

I dont know why.. I just want to set focus on this field first..

 

 

Thanks in advance...

Dee

New Developer
BBJDev
Posts: 50
Registered: ‎11-03-2009
My Device: Not Specified

Re: Stop edit field from wrapping text

hello,

 

need a reply on this..

 

Thanks,

Dee