Code example: Handling display orientation changes for a CustomItem

The following code example shows how to use the Orientation API to handle the display orientation changes for a CustomItem.

import com.nokia.mid.ui.orientation.Orientation;
import com.nokia.mid.ui.orientation.OrientationListener;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.CustomItem

public class CustomItemOrientationChanges extends CustomItem implements OrientationListener{

  /** Used for locking when accessing Graphics object */
  private static Object drawLock = new Object( );

  public CustomItemOrientationChanges(){
    super(“CustomItemOrientationTest”);
    Orientation.addOrientationListener( this );	
  }
  
  /** Orientation listener callback */
  public void displayOrientationChanged( int newDisplayOrientation ){

    /** Check display orientation */
    switch( newDisplayOrientation ){
      case Orientation.ORIENTATION_PORTRAIT:
      case Orientation.ORIENTATION_PORTRAIT_180:
        /** Change MIDlet UI orientation to portrait */
        synchronized( drawLock ){
          Orientation.setAppOrientation( Orientation.ORIENTATION_PORTRAIT );
        }
        break;

      case Orientation.ORIENTATION_LANDSCAPE:
      case Orientation.ORIENTATION_LANDSCAPE_180:
        /** Change MIDlet UI orientation to landscape */
        synchronized( drawLock ){
          Orientation.setAppOrientation( Orientation.ORIENTATION_LANDSCAPE );
        }

        break;
    }    
  }

  /** This is called by Java platform when the UI orientation has been changed */
  protected void sizeChanged(int w, int h){
    /** handle new dimensions */
  }

  /** Inherited from CustomItem */
  protected int getMinContentHeight(){
    
    /** CustomItem height depends on the app orientation */
    if ( Orientation.getAppOrientation() == Orientation.ORIENTATION_PORTRAIT ){  
      /** return height for portrait layout */
    }else{
      /** return height for landscape layout */
    }
  }

  /** Inherited from CustomItem */
  protected int getMinContentWidth(){

    /** CustomItem width depends on the app orientation */
    if ( Orientation.getAppOrientation() == Orientation.ORIENTATION_LANDSCAPE ){  
      /** return width for landscape layout */
    }else{
      /** return width for portrait layout */
    }
  }

  /** Inherited from CustomItem */
  protected int getPrefContentHeight( int width ){
    return getMinContentHeight();
  }

  /** Inherited from CustomItem */
  protected int getPrefContentWidth( int height ){
      return getMinContentWidth();
  }

  /**  Inherited from CustomItem */
  protected void paint( Graphics g, int width, int height ){
    /** Render screen content */
  }
}