john
john

Reputation: 11

Blackberry and Editfields

This is a very basic thing but I cant seem to find the answer searching then net.

Basically Im having issues in displaying two EditFields next to each other on a HorizontalFieldManager.

I believe from what Ive read the EditField uses the maximium width of the container it is in. With this in mind I thought adding the editFields to seperate VerticalFieldManagers and then adding these to my HorizontalFieldManager would work. Below is moreoless the code im doing this. But unfortuntely the userName show fine but the password field fails to dispplay. Any ideas on what im doing wrong?

            HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.USE_ALL_WIDTH));        
    VerticalFieldManager vfmUser = new VerticalFieldManager();
    VerticalFieldManager vfmPassw = new VerticalFieldManager(;

    userName = new EditField("Username:", "", 5, EditField.FIELD_LEFT);
    password = new PasswordEditField("Pass: ", "", 5, PasswordEditField.FIELD_RIGHT);

    vfmUser.add(userName);
    vfmPassw.add(password);

    hfm.add(vfmUser);
    hfm.add(vfmPassw);
    add(hfm);

Upvotes: 1

Views: 979

Answers (2)

Ankit Bajaj
Ankit Bajaj

Reputation: 9

HorizontalFieldManager hfm = new HorizontalFieldManager();

       userName = new EditField("Username:", "", 5, EditField.FIELD_LEFT)
       {
           protected void layout(int maxWidth, int maxHeight) {
                super.layout(maxWidth, maxHeight);
                this.setExtent(Display.getWidth()/2, this.getHeight());
            }
       }
       ;
       password = new PasswordEditField("Pass: ", "", 5, PasswordEditField.FIELD_RIGHT)

       {
           protected void layout(int maxWidth, int maxHeight) {
                super.layout(maxWidth, maxHeight);
                this.setExtent(Display.getWidth()/2, this.getHeight());
            }
       };


       hfm.add(userName);
       hfm.add(password);

       add(hfm);

try this code. i will be the solution of your problem.

Upvotes: 0

Laepdjek
Laepdjek

Reputation: 1809

The problem is that the EditField will still try to use all of the width, which causes the VerticalFieldManager to also use the entire width available to it, effectively shoving off the password field.

The only way I know of to do something like this would be to create classes that explicitly set their width to 50% of the screen. There are two ways of doing this: either create subclasses of EditField and PasswordEditField, or create a subclass of HorizontalFieldManager. The former option will be easier and more reliable across the different platforms, but will require two classes instead of one.

To do this, you'll want to create a subclass of EditField and override its layout method to look something like:

protected void layout(int maxWidth, int maxHeight) {
    super.layout(maxWidth, maxHeight);
    this.setExtent(Display.getWidth()/2, this.getHeight());
}

Similarly for the PasswordEditField.

Depending on how you want it to behave, you might have to override other methods as well. For instance, this code will (I'm pretty certain, haven't tested it specifically) cause any extra characters to be truncated if they dont fit in the desired area. If that's not what you want, you might have to override paint or change the layout based on the current text. Also, you should test it when tilting the storm screen to ensure the size updates correctly - it should work in most cases, but there is a bug that causes it to not update layouts/sizes if you have certain configurations of managers on the screen.

If you decide to go the manager route (which, again, I don't especially recommend, as you'll probably run into many more issues), you'll want to extend either Manager or HorizontalFieldManager (might be easier to do the former, so you don't have to deal with some of the quirks of the HFM), and override its sublayout method. In this method, you'll want to call layoutChild and setPositionChild (in that order) for every field in the manager (in your case, the user field and the password field). Then, at the end, you'll want to call setExtent. I would refer you to the blackberry docs on this matter, but in all honesty you're better off searching for how to implement blackberry custom managers, or asking questions on SO.

All that said, you might also want to reconsider why you want to do this. I can guarantee you from experience that there will be some phones where the UI does not look right - we tried a similar approach once, and eventually scrapped it because we couldn't get it to work from a UI standpoint on all devices. There are lots of different resolutions - some very narrow, some very wide - and if you go with this UI then some devices will have very cramped logins. IMHO, it would be preferable to be consistent with the other BlackBerry applications, and have username and password on different lines.

Upvotes: 1

Related Questions