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
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: CustomEditField Content Alignment issue

Please show your full layout() and sublayout() code - every single line there is extremely important.

 

When you layout the field, change the width you pass into super.layout(), call it and do nothing else! setExtent before or after super.sublayout() is a huge mistake in most cases unless you know exactly what you are doing (for example, override paint() to properly draw the place which is know about but the superclass doesn't).

 

In addition, you should restrict the width either in layoutChild() in Manager's sublayout (and extend Manager instead of HorizontalFieldManager) or in layout() of the field, not in both places. I know this matter is very confusing and can lead to some hard to diagnose problems, so this is one place where I always respond with a code sample and am ready to give very detailed explanations.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
tklanilkumar
Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800

Re: CustomEditField Content Alignment issue

//In my extended BasicEditField's layout()
public int getPreferredWidth ()
{
if(this.getText() != null && this.getText().length() > 0)
return font.getAdvance(this.getText()) + 10;// 10 pixels for the cursor when the focus is on the last char
else
return (2 * font.getAdvance("W"));
}

public int getPreferredHeight ()
{
return font.getHeight();
}

public void layout(int nWidth, int nHeight)
{
if(this.getText().length() >0)
{
nWidth = getPreferredWidth();
nHeight = getPreferredHeight();
}

super.layout(nWidth, nHeight);
setExtent(nWidth, nHeight);
}

//In my extended HorizontalFieldManager's sublayout()

public void sublayout(int nWidth, int nHeight)
{
field = this.getField(0);
int textWidth = field.getPreferredWidth();

layoutChild(field, textWidth, globalHeight);
setPositionChild(field, (this.globalWidth - textWidth)>0?(this.globalWidth - textWidth):0, 0);

}
*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: CustomEditField Content Alignment issue

Having looked at your layout and sublayout, I see that you can drop the field's layout with no harm at all. However, your Manager should be done totally differently then. Since you micro-manage your field's size and position, you should not extend HorizontalFieldManager! I still think you did not show the full sublayout code because I don't see any setExtent there - and, like I said, every line there and the order of the statements are extremely important!

 

Here is how your custom Manager might look:

Manager myMgr = new Manager(HORIZONTAL_SCROLL) {
  public void sublayout(int nWidth, int nHeight)
    {
      field = this.getField(0);
      int textWidth = field.getPreferredWidth();

      layoutChild(field, textWidth, globalHeight); 
      setPositionChild(field, (globalWidth - textWidth)>0?(this.globalWidth - textWidth):0, 0);
      setExtent(field.getHeight(), globalWidth);
    }
  }
};

Of course, you might want to create an explicitly named class extending Manager and having this sublayout override within. Make sure you create it with HORIZONTAL_SCROLL flag.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
tklanilkumar
Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800

Re: CustomEditField Content Alignment issue

[ Edited ]

Hi arkadyz

 

I tried like below after your valuable inputs but till im not getting

 

import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.container.MainScreen;

public class MyTextBoxScreen extends MainScreen {

 
 public MyTextBoxScreen() {
 
      setTitle("My TextBox Title");
         MyEditfield me = new MyEditfield();  
      MyManager mmg = new MyManager(HORIZONTAL_SCROLL);
      mmg.add(me);
      add(mmg);
   
 }
 
 class MyEditfield extends BasicEditField
    {
     public MyEditfield() {

     }
     
     public int getPreferredWidth() {
     
      if(this.getText()!=null)
       return getFont().getAdvance(this.getText())+10;
      else
       return (2 * getFont().getAdvance("W"));
     }
     
     public int getPreferredHeight() {
     
      return getFont().getHeight();
     }
     protected void layout(int width, int height) {
     
      super.layout(getPreferredWidth(), height);
      setExtent(getPreferredWidth(), height);
     }
    }
    
    
    class MyManager extends Manager
    {
     public MyManager(long l) {
      super(heart);
  }
     
  protected void sublayout(int width, int height) {
   
   int text_width = getField(0).getPreferredWidth();
   
   layoutChild(getField(0), text_width, height);
   setPositionChild(getField(0), (width - text_width)>0?(width - getField(0).getPreferredWidth()):0, 0);
   
   setExtent(width, height);
   
  }
     
    }
}

*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: CustomEditField Content Alignment issue

This one is probably a typo but I'll start with it:

What is "super(heart)" doing in your Manager's constructor? I would expect super(l) there. Otherwise your HORIZONTAL_SCROLL will not propagate to the superclass.

 

And now for the more serious stuff:

1) fully remove layout override from your MyEditField: this job is done by layoutChild in your Manager.

2) change setExtent in your Manager's sublayout to something like setExtent(width, getField(0).getHeight()) - otherwise your Manager will grab all possible vertical room.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!