Package totalcross.ui

Class Bar


  • public class Bar
    extends Container
    This class provides a title area and a button area (at right). The title and the button are optional, although it doesn't make sense to have a Bar without title and buttons. You can add or remove buttons, and change the title text; the title text can have an icon at left. Here's an example of how to use it, taken from the old UIGadgets sample:
     final Bar h1,h2;
     Font f = Font.getFont(true,Font.NORMAL_SIZE+2);
     h1 = new Bar("fakeboot");
     h1.canSelectTitle = true;
     h1.setFont(f);
     h1.setBackForeColors(0x0A246A,Color.WHITE);
     h1.addButton(new Image("ic_dialog_alert.png"));
     h1.addButton(new Image("ic_dialog_info.png"));
     add(h1, LEFT,0,FILL,PREFERRED); // use 0 instead of TOP to overwrite the default menu area
     
    A ControlEvent.PRESSED is sent to the caller, and the button index can be retrieved using the getSelectedIndex() method. By default, the background is shaded. You can change it to plain using h1.backgroundStyle = BACKGROUND_SOLID;.
    • Field Detail

      • drawBorders

        public boolean drawBorders
        Set to false to remove the borders
      • canSelectTitle

        public boolean canSelectTitle
        Set to true to allow the title to be selected and send events.
      • titleAlign

        public int titleAlign
        The title horizontal alignment (LEFT, CENTER, or RIGHT). Defaults to LEFT.
      • portraitPrefH

        public int portraitPrefH
        The preferred height on portrait or landscape, in pixels.
      • landscapePrefH

        public int landscapePrefH
        The preferred height on portrait or landscape, in pixels.
    • Constructor Detail

      • Bar

        public Bar()
        Constructs a Bar object without a title. Note that if you call the setTitle() method, a RuntimeException will be thrown. If you want to change the title later, use the other constructor and pass an empty string ("").
      • Bar

        public Bar​(java.lang.String title)
        Constructs a Bar object with the given title.
        Parameters:
        title - The bar title.
    • Method Detail

      • setIcon

        public void setIcon​(Image icon)
        An image icon that can be placed at the left of the title. It only shows if there's a title set. Pass null to remove the icon if it was previously set.
        Parameters:
        icon - The image icon.
      • getIcon

        public Image getIcon()
        Returns the icon set (and possibly resized) with setIcon, or null if none was set
      • setTitle

        public void setTitle​(java.lang.String newTitle)
        Changes the title to the given one.
        Parameters:
        newTitle - The bar new title.
      • getTitle

        public java.lang.String getTitle()
        Retrieves the current title.
        Returns:
        The bar title.
      • addButton

        public int addButton​(Image icon)
        Adds an image button at right.
        Parameters:
        icon - The image to the add to a button in the bar.
        Returns:
        The button index
      • addButton

        public int addButton​(Image icon,
                             boolean atRight)
        Adds an image button at the given position.
        Parameters:
        icon - The image to the add to a button in the bar.
        atRight - if true, button is added at right; if false, button is added at left.
        Returns:
        The button index
      • setButtonRepeatRate

        public void setButtonRepeatRate​(int idx,
                                        int ms)
        Sets the given button with an auto-repeat interval in the given milliseconds.
        Parameters:
        idx - The index of the button in the bar.
        ms - The auto-repeat interval in milliseconds.
      • addControl

        public int addControl​(Control c)
        Adds a control to the bar at right. Not all types of controls are supported.
        Parameters:
        c - The control to be added.
        Returns:
        The button index
      • addControl

        public int addControl​(Control c,
                              boolean atRight)
        Adds a control to the bar. Not all types of controls are supported.
        Parameters:
        atRight - if true, button is added at right; if false, button is added at left.
        c - The control to be added.
        Returns:
        The button index
      • removeButton

        public void removeButton​(int index)
        Removes a button at the given index, starting at 1.
        Parameters:
        index - The index of the button to be removed.
      • setButtonsVisible

        public void setButtonsVisible​(boolean visible,
                                      int... indexes)
        Shows or hide a set of buttons.
      • getSelectedIndex

        public int getSelectedIndex()
        Returns the selected button, or -1 if none was selected. The title always has index 0 (even if there's no title), and the button' index start at index 1.
        Returns:
        The index of the selected button.
      • initUI

        public void initUI()
        Called to initialize the user interface of this container.
        Overrides:
        initUI in class Container
      • getButtonWidth

        protected int getButtonWidth​(int idx)
        Override this method to return a customized button's width
      • onColorsChanged

        public void onColorsChanged​(boolean colorsChanged)
        Called after a setEnabled(), setForeColor(), or setBackColor(); or when a control has been added to a container. If colorsChanged is true, it was called from setForeColor()/setBackColor()/ Container.add(); otherwise, it was called from setEnabled().
        Overrides:
        onColorsChanged in class Container
        Parameters:
        colorsChanged - Indicates if the control colors have changed, which happens after a setForeColor(), setBackColor(), or Container.add().
      • getPreferredWidth

        public int getPreferredWidth()
        Returns the preferred width of this control.
        Overrides:
        getPreferredWidth in class Control
        Returns:
        The preferred width of this control.
      • getPreferredHeight

        public int getPreferredHeight()
        Returns the preferred height of this control.
        Overrides:
        getPreferredHeight in class Control
        Returns:
        The preferred height of this control.
      • startSpinner

        public void startSpinner()
        Shows and starts the spinner (if one has been assigned to the spinner field).
        See Also:
        spinner
      • updateSinner

        public void updateSinner()
        Updates the spinner; sets it visible if not yet.
      • stopSpinner

        public void stopSpinner()
        Stops and hides the spinner (if createSpinner or setSpinner was called before)
        See Also:
        spinner
      • reposition

        public void reposition()
        Repositions this control, calling again setRect() with the original parameters.
        Overrides:
        reposition in class Control
      • onFontChanged

        public void onFontChanged()
        Description copied from class: Control
        Called after a setFont
        Overrides:
        onFontChanged in class Control
      • assignBackKeyToButton

        public void assignBackKeyToButton​(int idx)
        Assigns the BACK key on Android (mapped to SpecialKeys.ESCAPE) to the given button. This can only be called after the bar has been added to a container. For example, if button 1 is assigned with totalcross.res.Resources.back, call assignBackKeyToButton(1);.
        Parameters:
        idx - The index of the bar button, starting at 1.
      • createSpinner

        public void createSpinner​(int color)
        Creates a spinner with the following color. The spinner will be placed at the right of the title (it only works if there's a title).
        Parameters:
        color - The spinner color.
      • setSpinner

        public void setSpinner​(Spinner s)
        Sets the spinner to the given one.
      • getTitleWidth

        public int getTitleWidth()