bitrazor.com ...THE source for mediocre content                                      

How to Create a Scrolling Text Item             

Keywords: tivo hme btext blist bananas scrolling text

Here's how you can create a scrolling text item in HME.  Since BText doesn't provide any scrolling capabilities, you can use a BList to simulate it.  Here is some example code.

/* BTextExample.java */

package btextexample;

import com.tivo.hme.bananas.BApplication;
import com.tivo.hme.interfaces.IContext;

public class BTextExample extends BApplication {
   public void init(IContext context) {
      try {
         super.init(context);
         MyScreen screen = new MyScreen(this);
         push(screen, TRANSITION_LEFT);
      } catch (Exception e) {
      }
   }
}

 

/* MyList.java */
package btextexample;

import com.tivo.hme.bananas.BList;
import com.tivo.hme.bananas.BText;
import com.tivo.hme.bananas.BView;

public class MyList extends BList {

   public MyList(BView parent, int x, int y, int width,
    int height, int rowHeight) {
      super(parent, x, y, width, height, rowHeight);
   }

   protected void createRow(BView parent, int index) {
      BText text = new BText(parent, 0, 0, parent.getWidth(), 
       parent.getHeight());
      text.setFlags(RSRC_HALIGN_LEFT);
      text.setValue((String)get(index));
      text.setFont("default-24.font");
   }
}

 

/* MyScreen.java */

package btextexample;

import com.tivo.hme.bananas.BApplication;
import com.tivo.hme.bananas.BScreen;

public class MyScreen extends BScreen {

   public MyScreen(BApplication app) {
      super(app);

      MyList list = new MyList(this.getNormal(), 100, 100, 400, 350, 35);
      list.add("Don't worry if you don't understand any of");
      list.add(" the above code. This is simply the ");
      list.add("standard pattern to follow when creating");
      list.add("a new application. This code creates");
      list.add("the application, calls the first ");
      list.add("screen (called 'InitialScreen' -- we'll get");
      list.add("to that in a minute), and handles");
      list.add("killing the application properly.");
      list.add("Also, at this point, Eclipse should");
      list.add("show one error (for 'InitialScreen';");
      list.add("you can tell by the little marker to the");
      list.add("left of the code). We'll fix that");
      list.add("in a minute.");
      setFocusDefault(list);
   }
}

In MyScreen,java, the example shows a bunch of list.add() statements.  This is not the most efficient way to do this, but the example is a little easier to follow this way.  Google "The following code illustrates the preferred method for adding rows to a list" to see the Bananas documentation with the preferred approach (adding them all to a Vector first).

You'll also notice that I took a block of text and chopped it up manually, line by line.  How would you automate this?  Well, you'd need to know the width of each character, and write some code to chop the string up into the appropriate size.  For more information on this topic, see "How to Determine the Width of a Font".

                                                         Last updated: February 19, 2007