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
rantravee
Posts: 28
Registered: ‎09-07-2010

Height of EditField

Hi,

 

I'm interested to know how could I set the height of a EditField  and have the text written at the middle of the editfield .

 

Thanks !

Please use plain text.
Developer
simon_hain
Posts: 15,558
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: Height of EditField

depends on what you really want to achieve.

you can use a custom layout manager to place your editfield into the middle of some empty space.

you can also set the height of an editfield by overwriting getPreferredHeight.

you cannot control where the text is written, this would require a custom field.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
patilajayr
Posts: 404
Registered: ‎07-28-2010
My Carrier: hi

Re: Height of EditField

hello,

               here is code.

 

here is the code for you

 

class MyEditField extends EditField
{
    public MyEditField (long style)
    {
        super (style);
        setBorder (BorderFactory.createSimpleBorder(new XYEdges (1, 1, 1, 1)));
    }

    public int getPreferredHeight()
    {
        return Font.getDefault.getHeight() * 3; //setting the height of edit field as 3 rows
    }

    public void layout (int width, int height)
    {
        super.layout (width, height);
        if (getExtent().height < getPreferredHeight())
            setExtent (width, getPreferredHeight());
    }
}

 

thanks,

 ajay

 accept solution if it works.

Please use plain text.
Contributor
rantravee
Posts: 28
Registered: ‎09-07-2010

Re: Height of EditField

Indeed the height of the EditField is customizable in this way , but when I write in it the text goes at the top of the EditField and not at the middle as I would like

Please use plain text.
Developer
Ekansh
Posts: 468
Registered: ‎08-07-2009
My Carrier: Blackberry Developer

Re: Height of EditField

hi,

 

 

class EditFieldClass extends EditField
{
    int width,height;
   
    EditFieldClass(long style,int width,int height)
    {
        super(style);
        this.width = width;
        this.height = height;
    }
  
     protected void layout(int width, int height)
    {
        width = this.width;
        height = this.height; 
        super.layout(width, height);
        super.setExtent(width, height);
    }
   
    public void paint(Graphics graphics)
    {
        super.paint(graphics);
        graphics.clear();
        int labelWidth = getFont().getAdvance(getLabel());
        graphics.drawRect( labelWidth, 0, getWidth() - labelWidth,getHeight());
        graphics.drawText(this.getText() ,3 , 3);
    }
   
   
}

 

 

You can also set font for the editfield.

 

Regards

Ekansh

----------------------------------------------------------
feel free to press the kudos button on the left side to thank the user that helped you.
please mark posts as solved if you found a solution.
Please use plain text.
Contributor
rantravee
Posts: 28
Registered: ‎09-07-2010

Re: Height of EditField

Great idea to redraw the text yourself . Thanks !  However in the way I do it the text is cropped at half like this :

aaa.PNG

 

The code I use is the following

EditField text = new EditField("","",4,style)
{
    protected void paint(Graphics g)
    {
	super.paint(g);
	g.clear();
       
        int fieldHeight =  getHeight();
	int fontHeight = getFont().getHeight();
	g.drawText(this.getText() ,5 , (fieldHeight-fontHeight)/2);
    }

  public int getPreferredHeight()
  {
       return  (int) (getFont().getHeight() * 1.5); 
  }
	
  public void layout (int width, int height)
   {
       super.layout (width, height);
         if (getExtent().height < getPreferredHeight())
             super.setExtent (width, getPreferredHeight());
   }
};

 

 

Please use plain text.
Regular Contributor
shaina123
Posts: 59
Registered: ‎03-01-2011

Re: Height of EditField

Using this code for customEditField i m also facing same issue . Text inside Field is not visible relevant to height

pls helpout me how to resolve

Please use plain text.
Trusted Contributor
hardikhamar
Posts: 159
Registered: ‎02-17-2011

Re: Height of EditField

 

Ur code:

  

int fieldHeight =  getHeight();

replace with:

 

     int fieldHeight = getFont().getHeight()+5;

 

     

Please use plain text.
Regular Contributor
shaina123
Posts: 59
Registered: ‎03-01-2011

Re: Height of EditField

thankz it really help
Please use plain text.
Regular Contributor
shaina123
Posts: 59
Registered: ‎03-01-2011

Re: Height of EditField

When editfield gets focus to input text then Cursor is not visible so its not clear either to start typing or not

pls help how to show cursor (Stright Vertical line blinking t)

Please use plain text.