views:

548

answers:

3

Hi, I am new to Blackberry Development.This is my first Question for you people. I am creating a search box for my project. But it looks like blackberry doesn't have an internal api for creating single line Edit field. I have created a Custom Field by extending BasciEditField overriding methods like layout, paint. In paint i am drawing a rectangle with getpreferred width and height. But the cursor is coming at default position (top-left) in Edit Field. Can any body tell me how i can draw it where my text is(i.e in middle of Edit Field by calling drwaText()). Thanks,

A: 

I am not getting your question but.

  1. you can create single line edit field.

    BasicEditField editField = new BasicEditField(BasicEditField.NO_NEWLINE);

  2. you can set cursor position.

    editField.setCursorPosition(offset);

Vivart
setCursorPosition will set position of cursor, but not handle it continuous draw in the middle of field
Max Gontar
A: 

You can draw the cursor where you want it to rewriting the onFocus method of the editField.

        protected void onFocus(int direction) {
            super.onFocus(direction);
            this.setCursorPosition(this.getTextLength());
            invalidate();
        }

After getting the Focus, the field will position the cursor at the end of the text. The editField has setCursorPosition and also getTextLength.

Hope it helps.

timoto
A: 

Thank you guys, i got one fine solution from Blackberry Journal. public class ScrollingSearchBox extends HorizontalFieldManager {
private int managerWidth; private int managerHeight; public ScrollingSearchBox() {
super(Manager.NO_HORIZONTAL_SCROLL); searchEdit = new BasicEditField(){
public int getPreferredHeight() {

                                                            return ret;
                                            }
                                            public int getPreferredWidth()
                                            {

                                                            return ret;
                                            }
                                            public void paint(Graphics g) 
                                            {
                                                            getManager().invalidate();
                                                            super.paint(g);

                                            }
                            };

                            HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL)
                            {
                                            public void sublayout(int width, int height)
                                            {
                                                            if (managerWidth == 0) {
                                                                            managerWidth = searchEdit.getPreferredWidth();
                                                            }
                                                            if (managerHeight == 0) {
                                                                            managerHeight = searchEdit.getPreferredHeight();
                                                            }
                                                            super.sublayout(managerWidth, managerHeight);
                                                            setExtent(managerWidth,managerHeight);
                                            }
                                            public void paint(Graphics g) {
                                                            super.paint(g);
                                            }
                            };  
                            searchEdit.setMaxSize(70);
                            hfm.add(searchEdit);
                            add(hfm);
            }

            public int getPreferredHeight()
                            {
                            int ret = 0;

                                            return ret;
                            }
                            protected void sublayout(int maxWidth, int maxHeight) 
                            {
                                            int currX = 0;
                                            int currY = 0;
                                            Field currField;

                                            currField = this.getField(0);
                                            switch (ScreenConfig.getInstance().getScreen())
                                            {
                                            case ScreenConfig.SCREEN_320_240:
                                                            currX = 5;
                                                            currY = 3;
                                                            break;
                                            case ScreenConfig.SCREEN_480_360:
                                            case ScreenConfig.SCREEN_480_320:
                                                            currX = 5;
                                                            currY = 1;
                                                            break;
                                            }
        this.setPositionChild(currField, currX, currY);
        this.layoutChild(currField, currField.getPreferredWidth(), 
                                                                            currField.getPreferredHeight());
        setExtent(this.getPreferredWidth(), this.getPreferredHeight());
                            }

                            protected void paint(Graphics graphics) 
                            {
                                            super.paint(graphics);
                                            graphics.drawRect(0, 0, this.getPreferredWidth(), this.getPreferredHeight());
                            }
}

I am giving this so it can help others. Keep sharing your codes. Thanks.

varun