Question

I am using custom EditField which given in Rim samples. With this editfield, i can input maximum characters in one line.. But the problem is that left most hidden characters are not getting when i am getting from edittext.

Following is my CustomEditField class

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

public class CustomEditfield extends Manager
{

    private final static int DEFAULT_LEFT_MARGIN = 0;
    private final static int DEFAULT_RIGHT_MARGIN = 5;
    private final static int DEFAULT_TOP_MARGIN = 0;
    private final static int DEFAULT_BOTTOM_MARGIN = 0;

    private final static int DEFAULT_LEFT_PADDING = 5;
    private final static int DEFAULT_RIGHT_PADDING = 5;
    private final static int DEFAULT_TOP_PADDING = 0;
    private final static int DEFAULT_BOTTOM_PADDING = 0;

    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 ;
    private int height ;

    private EditField editField;
    private Bitmap _currentPicture; 

    public CustomEditfield(String label,String initialValue,int maxChars,long style,Bitmap image)
    {
        super(0);
        this._currentPicture=image;

        this.width=_currentPicture.getWidth();
        this.height=_currentPicture.getHeight()+20;
        editField = new EditField(label, initialValue, maxChars,style);
//        editField.setPadding(_currentPicture.getHeight()/7, 0, 0, _currentPicture.getWidth()/40);
        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);
        graphics.drawBitmap(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), _currentPicture, 5, 0);
        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);
    }    
}

------ In my screen class---

et_fname = new CustomEditfield("", "", 50, EditField.NO_NEWLINE
            | EditField.FOCUSABLE, txt_bg);

So when i called

et_fname.getText().toString().trim()

It will return only visible characters. So how can i get all entered characters.

If any one has idea,please help.. Help will be appreciated.

Was it helpful?

Solution 2

According to Peter Strange suggestion,

I found new solution for Single line editfield which have solved my actual problem.

Following is my CustomEditField class for Single line editfield with horizontal scrolling.

 import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.container.HorizontalFieldManager;

public class CustomEditfield extends HorizontalFieldManager
{
    private int managerWidth;
    private int managerHeight;
    BasicEditField searchEdit;
    private Bitmap _currentPicture;
    private int width ;
    private int height ;
    private final static int DEFAULT_LEFT_MARGIN = 0;
    private final static int DEFAULT_RIGHT_MARGIN = 5;
    private final static int DEFAULT_TOP_MARGIN = 0;
    private final static int DEFAULT_BOTTOM_MARGIN = 0;

    private final static int DEFAULT_LEFT_PADDING = 5;
    private final static int DEFAULT_RIGHT_PADDING = 5;
    private final static int DEFAULT_TOP_PADDING = 0;
    private final static int DEFAULT_BOTTOM_PADDING = 0;

    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;

    public CustomEditfield(String label,String initialValue,int maxChars,long style,Bitmap image)
    {
        super(Manager.NO_HORIZONTAL_SCROLL);
        this._currentPicture=image;
        this.width=_currentPicture.getWidth();
        this.height=_currentPicture.getHeight()+20;

        searchEdit = new BasicEditField(label, initialValue, maxChars,style){
            public int getPreferredHeight()
            {                 
                return _currentPicture.getHeight();
            }
            public int getPreferredWidth()
            {                    
                return _currentPicture.getWidth();
            }
//            public void paint(Graphics g)
//            {
//                getManager().invalidate();
//                super.paint(g);                    
//            }
        };
        searchEdit.setPadding(_currentPicture.getHeight()/7, 0, 0, 0);

        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()
    {
        return _currentPicture.getHeight();
    }

    public String getText()
    {
        return searchEdit.getText();
    }

    public void setText(final String text)
    {
        searchEdit.setText(text);
    } 

    protected void sublayout(int maxWidth, int maxHeight)
    {
        Field currField;

        currField = this.getField(0);
        this.setPositionChild(currField,leftMargin+leftPadding, topMargin+topPadding);
        this.layoutChild(currField, this.width - totalHorizontalEmptySpace, this.height - totalVerticalEmptySpace);
        setExtent(this.width, this.height);
    }

    protected void paint(Graphics graphics)
    {
        graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
//      graphics.drawBitmap(leftMargin, topMargin, width - (leftMargin+rightMargin), height - (topMargin+bottomMargin), _currentPicture, 5, 0);
        super.paint(graphics);
//        graphics.drawRect(0, 0, this.getPreferredWidth(), this.getPreferredHeight());
    }
}

I have found this solution from Blackberry - Custom EditField Cursor

I am giving this so it can help others.

OTHER TIPS

Look at your paint method. In there you truncate the EditField so that it only contains the text that will fit. So what did you expect to happen?

You might think you can save the full text, then call super.paint() and the restore the full text, but that is actually gong to cause you other problems. A setText() on an EditField will tell the field that it has been updated and so it will try to repaint itself. So you will truncate, which will flag up to the EditField it needs to be painted, call super.paint, replace, which will flag up a paint required again, and then the paints for the EditField will kick in and that will try to paint themselves, I suspect on the way invoking the paint() for the Manager which will get recursive and messy.

This approach to painting only the visible are in the Manager is flawed and will never work.

What you seem to be trying to do is a single line EditField that scrolls left and right, The more normal approach to doing this is to have a full width HorizontalFieldManager add the EdtiField to that and have the HFM scroll the EditField left and right. I have not looked for sample code that does this, but I suspect you will find some if you look round. So have a go at that option and let us know how you get on.

the problem is when you typed character once width of field occupied with characters. it will take new character and drop old characters in field. so when you get data inside field you only get characters available on the fields.

you can reproduce this by following steps: 1. type atleast 8 characters. 2. prees back key. 3. then come to 1st character it is not that character that you entered.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top