CustomTextBox align

Hi all

I use code kindly offered online by Dorian.

Now, I am trying to align this text box toward the Center.

Does anyone know what I need to change to achieve this please?

import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.system.Characters;
import net.rim.device.api.math.Fixed32;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;

public class CustomTextBox2 extends Manager{    /**     * Default margins     */
    private final static int DEFAULT_LEFT_MARGIN = 0;

private final static int DEFAULT_RIGHT_MARGIN = 10;
private final static int DEFAULT_TOP_MARGIN = 5;
private final static int DEFAULT_BOTTOM_MARGIN = 5;        /**     * Default paddings     */
private final static int DEFAULT_LEFT_PADDING = 10;
private final static int DEFAULT_RIGHT_PADDING = 10;
private final static int DEFAULT_TOP_PADDING = 5;
private final static int DEFAULT_BOTTOM_PADDING = 5;
/**     * Margins around the text box     */
private int topMargin = DEFAULT_TOP_MARGIN;
private int bottomMargin = DEFAULT_BOTTOM_MARGIN;
private int leftMargin = DEFAULT_LEFT_MARGIN;
private int rightMargin = DEFAULT_RIGHT_MARGIN;
/**     * Padding around the text box     */
private int topPadding = DEFAULT_TOP_PADDING;
private int bottomPadding = DEFAULT_BOTTOM_PADDING;
private int leftPadding = DEFAULT_LEFT_PADDING;
private int rightPadding = DEFAULT_RIGHT_PADDING;
/**     * Amount of empty space horizontally around the text box     */
private int totalHorizontalEmptySpace = leftMargin + leftPadding
        + rightPadding + rightMargin;
/**     * Amount of empty space vertically around the text box     */
private int totalVerticalEmptySpace = topMargin + topPadding
        + bottomPadding + bottomMargin;
/**     * Minimum height of the text box required to display the text entered     */
private int minHeight = getFont().getHeight() + totalVerticalEmptySpace;
/**
 * Width of the text box     */
private int width;
/**     * Height of the text box     */
private int height = 45;
/**     * Background image for the text box     */
private EncodedImage backgroundImage;
/**     * Bitmap version of the backgroundImage.
 * Needed to reduce the calculation overhead incurred by
 * scaling of the given image
 * and derivation of Bitmap from the
 * EncodedImage every time it is needed.
 */
private Bitmap bitmapBackgroundImage;
/**
 * The core element of this text box
 */
private EditField editField;
MainScreen theScreen;
//private BasicEditField editField;
//private String entireText;
public CustomTextBox2(int width2, int height, MainScreen aScreen)
{
     super(0);
    theScreen = aScreen;
    // Let the super class initialize the core

    width = width2 + 20;
    // An edit field is the sole field of this manager.
    editField = new EditField();
    //editField = new CustomEditField();
    add(editField);
}

/*
public CustomTextBox2(EncodedImage backgroundImage)
{
    this();

setBackgroundImage(backgroundImage);
    }
 * */

    public void setSize(int width, int height)
    {
        boolean isChanged = false;
        if (width > 0 // Ignore invalid width
                && this.width != width)
        {
            this.width = width;
            isChanged = true;
        }
        // Ignore the specified height if it is less
        // than the minimum height required to display the text.
        if (height > minHeight && height != this.height)
        {
            this.height = height;
            isChanged = true;
        }
        // If width/height has been changed and background image
        // is available, adapt it to the new dimension
        if (isChanged == true && backgroundImage != null)
        {
            bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
                    this.width - (leftMargin+rightMargin),
                    this.height - (topMargin+bottomMargin));

        }
    }

    public void setWidth(int width)
    {
        if (width > 0 && width != this.width)
        {
            this.width = width;
            // If background image is available, adapt it to the new width
            if (backgroundImage != null)
            {
                bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
                        this.width - (leftMargin+rightMargin),
                        this.height - (topMargin+bottomMargin));
            }
        }
    }
    public void setHeight(int height)
    {
        // Ignore the specified height if it is
        // less than the minimum height required to display the text.
        if (height > minHeight)
        {
            this.height = height;
            // If background image is available, adapt it to the new width

            if (backgroundImage != null)
            {
                bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
                        this.width - (leftMargin+rightMargin),
                        this.height - (topMargin+bottomMargin));
            }
        }
    }
    public void setBackgroundImage(EncodedImage backgroundImage)
    {
        this.backgroundImage = backgroundImage;
        // Consider the height of background image in
        // calculating the height of the text box.
        // setHeight() does not ensure that specified
        // height will be in effect, of course, for valid reasons.
        // So derivation of Bitmap image in the setHeight() method is not sure.
        setHeight(backgroundImage.getHeight() + topMargin + bottomMargin);
        if (bitmapBackgroundImage == null)
        {
            bitmapBackgroundImage = getScaledBitmapImage(backgroundImage,
                    this.width - (leftMargin+rightMargin),
                    this.height - (topMargin+bottomMargin));
        }
    }
    /**     * Generate and return a Bitmap Image scaled according
     * to the specified width and height.
     *
     * @param image     EncodedImage object
     * @param width     Intended width of the returned Bitmap object
     * @param height    Intended height of the returned Bitmap object
     * @return Bitmap object     */
    private Bitmap getScaledBitmapImage(EncodedImage image, int width, int height)
    {
        // Handle null image
        if (image == null)
        {
            return null;
        }
        int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
        int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
        int requiredWidthFixed32 = Fixed32.toFP(width);
        int requiredHeightFixed32 = Fixed32.toFP(height);
        int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32);
        int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32);
        image = image.scaleImage32(scaleXFixed32, scaleYFixed32);
        return image.getBitmap();
    }
    protected void sublayout(int width, int height)
    {
        // We have one and only child - the edit field.
        // Place it at the appropriate place.
        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)    {
        // Draw background image if available, otherwise draw a rectangle
        if (bitmapBackgroundImage == null)
        {
            //graphics.drawRect(leftMargin, topMargin,                                 width - (leftMargin+rightMargin),                                 height - (topMargin+bottomMargin));
            graphics.setColor(0x000000);
            graphics.drawRoundRect(leftMargin, topMargin,
                    width - (leftMargin+rightMargin),
                    height - (topMargin+bottomMargin), 15, 15);
            graphics.setColor(0xffffff);
            graphics.fillRoundRect(leftMargin, topMargin,
                    width - (leftMargin+rightMargin),
                    height - (topMargin+bottomMargin), 15, 15);
        }
        else
        {
            graphics.drawBitmap(leftMargin, topMargin,
                    width - (leftMargin+rightMargin),
                    height - (topMargin+bottomMargin),
                    bitmapBackgroundImage, 0, 0);        }
        // Determine the rightward text that can fit into the visible edit field
        EditField ef = (EditField)getField(0);
        String entireText = ef.getText();
        boolean longText = false;
        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)
                {
                    longText = true;
                    break;
                }            }        }
        if (longText == true)
        {
            // Force the edit field display only the truncated text
            ef.setText(textToDraw);
            // Now let the components draw themselves
            super.paint(graphics);
            // Return the text field its original text
            ef.setText(entireText);

        }
        else
        {
            super.paint(graphics);
        }
    }
    public int getPreferredWidth()
    {
        return width;
    }
    public int getPreferredHeight()
    {
        return height;
    }
    protected boolean keyChar(char ch, int status, int time)
    {
       Log.info("KEYCHAR " + ch);
        switch(ch){

            case Characters.ENTER:
                try
        {    Log.info("KEYCHAR 1");
                   // theScreen.textButton.getChangeListener().fieldChanged(theScreen.textButton, theScreen.textButton.getIndex());
   Log.info("KEYCHAR 2");
          // f.getChangeListener().fieldChanged(f, f.getIndex());
        }
        catch (Exception e)
        {
        Log.info("Error at fcn" + e);
        }
                return true;

        }
Log.info("KEYCHAR 0");
//theScreen.textButton.getChangeListener().fieldChanged(theScreen.textButton, theScreen.textButton.getIndex());

        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);
    }
   }

You are missing a centerpiece - the ability to set the bits of style on this field. If you add a constructor that accepts a parameter of long style and has great (style) as the first operator, you should be able to create with the FIELD_HCENTER style bit and add it to a Manager honor this indicator (for example, VerticalFieldManager).  Then it will be automatically centered horizontally by the parent Manager.

It is the same on FIELD_VCENTER and, say, HorizontalFieldManager.

Tags: BlackBerry Developers

Similar Questions

  • Move the 3 letters for word-how to align results in Keynote?

    As you can see, I am to spoil it enormously. What step I got?

    Thank you.

    In your example, I can't tell what you are trying to do.

    I'll assume that the word "cat" is your difficulty. If you have each of the letters in an individual text box and then select all and choose align > way. If the letters are not text boxes you have set "basic" for the letter t and maybe "character spacing" to the other letters. Enter these words in the help.

  • How to change the default alignment of text in the cells in a table on the demand for numbers?

    I'm new to Mac. I own a MacBook pro MF839HN/A and currently using the 3.6.2 release NUMBERS (2577). I want to know if I can change the default alignment of text in the cell in a table of NUMBERS application? Also, when I select all the cells in a table to change their alignment, I can only change the horizontal alignment of the text and not the vertical alignment. To change the vertical alignment of the text in a cell, I have to select them individually. Help me with two questions.

    The only way I know is to create a table that is set up as you like, then save the empty document as a template customized by using the menu item "file > save as template:

  • moving text, very slightly to align with precision

    Hello

    I do a menu in pages and I would like to move some of the prices on the right side, so they all line up nicely. I don't want to choose all the 'align right' as then the food not all vertically aligned. Is there a command or action that allows me to just move the desired text by small steps?

    Thank you

    There is no adjustment convenient to move the text in small increments. Maybe with InDesign, Quark, or Scribus, but not in Pages.

    If you want your menu items and prices aligned differently and especially perfectly aligned to the right, then use a table that has cell formatting for this purpose.

  • Paging in the poorly aligned table of contents

    As you can see in the screenshot below, the pagination is right-aligned, sometimes in the table of contents, and sometimes it seems all wrong. What should I do to fix this?

    Hello joachim,.

    Pages 5.6.2 on OS X El Capitan 10.11.5

    Menu > view > show rules.

    You can select (click) each level (Style) of the Table of contents and view the parameters of the rule.

    With the second level selected, look in Control Panel of Format > text > tabs

    Please call with questions.

    Kind regards

    Ian.

  • Facebook ask for enable cookies when I already did. Page is white and blue just link on the left-aligned words.

    Facebook appears on white background and text, some with links left alignment.

    allowed, history, cookies, cache, everything I searched on google.

    cookies are already enabled.

    all other websites are ok except fb.

    pls help.

    Apparently, facebook could have updated their ads stuff that make my anti-ad on my phone does not not with facebook. fuck Facebook.

  • How can I display a grid of horizontal alignment in Pages.

    How can I display a grid of horizontal alignment in pages. I can show a vertical, but there seems to be no option to show a horizontal. I use El Capitan with maps, version 5.6.2 Pages.

    Pages v5.6.2 has horizontal and vertical guides, but no grid. The vertical guide is enabled in the menu Pages: preferences: sovereign.

    With the two visible leaders after menu display: display the rule, place the cursor of the black needle on the edge of the respective sovereign and then click. When you see the following icon, drag the new guide in your document. Several vertical and horizontal guides may be present. The view menu has an element of Guides to hide menu or erased.

    • Indicator of horizontal ruler guide
    • Indicator of vertical ruler guide
  • Advice needed on the alignment of the text in an e-mail.

    I'm developing an email with a small logo (with a web link) with a descriptive text to the right. I tried to put a picture here, but it won't download for some reason any so posted it here: http://imgur.com/gallery/lHRD8VC/new

    I can align the text on the image, but when it falls in the second line, it drops below the logo image and left justified. I want it all back right of the logo to look neat. I know that I can use the tap and space to get what I want in the email, but she is shown in a smaller window when it is created, all goes pear-shaped.

    Any help would be appreciated.

    use a table. 1 row end on 2 columns. Picture in the text first, in the second.

  • alignment of the footer

    Im trying to align (justify) of text in the footer of the pages, but the buttons is not active.

    What can I do?

    Hi horisize,

    Insert the text into a text box in the body of the document. Better if the text box has no borders. Check the box and the alignment buttons will be active. Justify. Cut and paste the box in the page footer.

    Kind regards

    Ian.

  • Why, suddenly, my desktop icons are difficult to align because they can be moved very small distances?

    For years, my desktop icons have acted as if they have so many 'slots' to enter in on the office. But this has never been a problem. They were easier to align because they were right next to each other, or a 'slot' above/below/apart.

    Now, for some reason, I can travel distances of small icons on the desktop, as if they were scalar elements which can be minimally pushed open. Suddenly there are not more "slots", and sort of paradoxically icons are more difficult to assemble and align because I have to keep their repositioning so that they are straight. Before, they were either straight, or a whole notch offline.

    It is NOT correct. Right-click and select Show , and then turn on Align

  • Questions about css vertical-align

    given a list of the CSA page: Technologies of Mac OS X

    I do not understand vertical-align.  I will like to center the text in the vertical alignment.  I don't know what basis means, or intermediate. Where Ref base?  background of the characters or what?  I don't know how to work the percentages.

    In addition to all this lack of knowledge, 5% of the centers of communities Word for some reason I don't know.  'Contact support' appears to be centered for some reason any.

    with this css

          /* top line:
             Communities                Contact support */
          header#j-header nav#j-globalNav-bg {
              background-color: tan !important;
              margin-top: -15px !important;
             }
           /* Communities */
          div#body-apple header div#apple-site-title a {
            
              font-size: 20px !important;
              background-color: yellow !important;
              vertical-align: 5% !important;
             }
          /* Contact Support */
          div#body-apple header ul#contact-support-link a {background-color: pink !important;}
    
  • Whenever I go to hotmail my s and outs are not aligned if I open another tab and go bask in hotmail is fine if I open an e-mail and then back in the box of

    When I go on my hotmail all titles half e-mail superior is readable when I open another tab that come back, it's very readable, if I open an e-mail once again only half is showing again fine go to another tab/site back and all it is all aligned. This is not the case with Safari

    Start Firefox in Safe Mode to check if one of the extensions (Firefox/tools > Modules > Extensions) or if hardware acceleration is the cause of the problem.

    • Put yourself in the DEFAULT theme: Firefox/tools > Modules > appearance
    • Do NOT click on the reset button on the startup window Mode safe

    You can have zoomed pages by accident.
    Reset the zoom of page on pages that are causing problems.

    • View > Zoom > reset (Ctrl / command + 0 (zero))
  • Why do Garageband and align myself a copy and paste the track with the original track

    That's what it's going to go

    When you copy and paste a region must be inserted exactly where the crosshair is positioned. Have you tried?

    Or, to align a region when copying from another track, hold option key while dragging the region towards the destination track. He should be perfectly aligned.

    I saw the same problem sometimes that a region refuses to be shifted. Then save the project, and restarting of GarageBand should help.

  • iMovie 10.1.2 Photo alignment

    I'm trying to align my photos on the left side of the preview screen. In this way, I'll be able to easily add text to the right. How to achieve this?

    Thank you very much for the help!

    Align your photos to left is to use the split function on a black background (or whatever color), with text superimposed on the background.  Scrolling title can be adapted to write the text and then you can take a screenshot of it for a static effect, import the screen shot in your project and then superimpose your photo, above, then split screen by selecting the text overlay function (the button that looks like to the overlap of the places in the toolbar at the top of your screen).

  • Motion - Final Cut title - related text alignment

    I created a title Final Cut in Motion 5.

    Then I copied the text to a different layer and created an animation.

    The text of the first, is published, so I can customize later for Final Cut Pro X.

    So the second text corresponds to the text of the edited user, I've added a link to the text using the source: Object.Text, Object.Text destination.

    When I use it in Final Cut, the text is changed and I can even looking fonts, size etc.

    The problem is, that the second text alignment is left, I even specified that it is centered.

    How can solve this problem?

    I can link somehow the alignment of another text?

    Here's what it looks like in Motion 5:

    Here is the title in Final Cut Pro X, where the animated text left aligned instead of centered:

    Want to make a copy of the Clone of the text layer, not bind.

Maybe you are looking for

  • ESES does not recognize the locations of photos

    Hi allI have improved my 5s Iphone to IOS 10, go to the photo-album > section, in the places folder, there is no pictures.can someone help me?

  • How can I reactivate swipe left/right UX (Safari-like) in every night?

    Hit swiping itself works, but the animation is gone completely. Is there a way to reactivate it? I think that the feature has gone since mid-December. It makes FF so more friendly user, I hope that the disappearance is only temporary (and not caused

  • Tecra R940 - deleted partition - how to install the system?

    Hi guys,. I have wiped the partition on my Tecra laptop and I was wondering how should I now go to the installation of the OS.It is not possible to download windows8 pro microsoft. Could you please help? Thank you Message was edited: the name of the

  • How to change the size of the screen

    original title: my screen is very large. I checked to customize settings to reduce its size & sound always very large? I can noy seem to fix the size if the photo & icons with customize settings. Is there something different that can be sugessted to

  • I want to buy a new flash drive, help me choose please :(

    Hello guys! )) I am looking to buy a new flash drive, I transfer a lot of files and folders that exceed the limit of 4 GB to fat32 I liked the Extreme Sandisk 64 GB, but I did some research and apparently has two versions, one that reads 190 MB/s and