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
Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Why is EditField acting strange ?

 

Hi,

 

    I am adding a text field and a button in FlowFieldManager added to a VerticalFieldManager. Regardless of howmuch size of EditField I give, it occupies the whole width of the screen and shows the button on next line only. I have other FlowField also where I have added 2 buttons and it shows perfectly well as extected. But this one reacts weird. The Code is :

 

public class InitialManager extends VerticalFieldManager {
 
     public InitialManger() {
          super(USE_ALL_WIDTH | USE_ALL_HEIGHT | VERTICAL_SCROLLBAR | VERTICAL_SCROLL);
     
        searchffm = new FlowFieldManager();
        searchffm.setBackground(BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("res/top_btm_bar.png")));
        searchTxt = new EditField("Search for ", "", 20, Field.FIELD_LEFT | Field.FIELD_VCENTER);
        searchTxt.setBorder(BorderFactory.createRoundedBorder(new XYEdges(2, 2, 2, 2), Color.WHITE, Border.STYLE_FILLED));
        searchBtn = new ImageButton(Bitmap.getBitmapResource("res/meters.png"), Bitmap.getBitmapResource("res/meters.png")); 
        searchffm.add(searchTxt);
        searchffm.add(searchBtn);

        add(searchffm);  
   }
}

 

Result is

Untitled.jpg

 

I expect the editField & button to be on same line. I tried using various style to editfield, but no success. Can any one tell what is the issue. Where am I going wrong & the resutls are not as expected.

 

Any help guidance is highly appreciated.

 

Thanks

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Why is EditField acting strange ?

EditField unfortunately has the behavior that it asks for as much width as is available. There isn't a style setting that overrides this. (RichTextField has USE_TEXT_WIDTH, but that isn't appropriate for an editable field.)

 

You can subclass EditField and override getPreferredWidth() and layout() so that it leaves room for the button at the right. Perhaps something like this:

 

 

private static class MyEdit extends EditField {
  Field button;
  public MyEdit(Field button) {
    this.button = button;
  }
  public int getPreferredWidth() {
    return super.getPreferredWidth() - button.getPreferredWidth();
  }
  protected void layout(int width, int height) {
    width = Math.min(getPreferredWidth(), width);
    super.layout(width, height);
  }
}

 

 

Alternatively, you can use a custom layout manager instead of a FlowFieldManager. The latter solution is perhaps cleaner, since it puts the layout logic inside the manager, where it belongs.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Highlighted
Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Why is EditField acting strange ?

I have the same issue but I used a HorizantalFieldManager and added the manager to a MainScreen's banner but it should be the same concept.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Why is EditField acting strange ?

[ Edited ]

 

Thanks Ted_Hopp,

 

   For this scenario using other custom FieldManager helped me. HorizontalFieldManager also didn't helped.

But with other EditFields, can I set size of editfield using your MyEdit class. So I can set size of various edit fields.

 

Thanks


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.