Help Center

Local Navigation

Create a radio button

You can create a group of radio buttons by using the RadioButtonGroup class. A user can select only one option from a group of radio buttons.
  1. Import the required classes and interfaces.
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
  2. Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The MyUiScreen class, which is described in step 3, represents the custom screen.
    public class MyUi extends UiApplication 
    { 
        public static void main(String[] args) 
        { 
            MyUi theApp = new MyUi(); 
            theApp.enterEventDispatcher(); 
        } 
        public MyUi() 
        { 
            pushScreen(new MyUiScreen()); 
        } 
    }
  3. Create the custom screen for the application by extending the MainScreen class. In the screen constructor, invoke setTitle() to specify the title for the screen.
    class MyUiScreen extends MainScreen 
    {
        public MyUiScreen() 
        {
            setTitle("UI Component Sample"); 
        }
    }
  4. In the screen constructor, create a group of radio buttons by using the RadioButtonGroup class. Create the radio buttons that you want to add to the group by using the RadioButtonField class. In the RadioButtonField constructor, specify the label for the radio button, the group, and a Boolean value to indicate the default selection (for example,true indicates that by default the radio is selected). Invoke add() to add the radio buttons to the screen.
    RadioButtonGroup rbg = new RadioButtonGroup();
    add(new RadioButtonField("Option 1",rbg,true));
    add(new RadioButtonField("Option 2",rbg,false));
  5. To override the default functionality that prompts the user to save changes before the application closes, in the extension of the MainScreen class, override the MainScreen.onSavePrompt() method. In the following code sample, the return value is true which indicates that the application does not prompt the user before closing.
    public boolean onSavePrompt()
    {
        return true;
    }

Code sample: Creating a radio button

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

public class MyUi extends UiApplication 
{ 
    public static void main(String[] args) 
    { 
        MyUi theApp = new MyUi(); 
        theApp.enterEventDispatcher(); 
    } 
    public MyUi() 
    { 
        pushScreen(new MyUiScreen()); 
    } 
}
class MyUiScreen extends MainScreen 
{
    public MyUiScreen() 
    {
        setTitle("UI Component Sample"); 
        RadioButtonGroup rbg = new RadioButtonGroup();
        add(new RadioButtonField("Option 1",rbg,true));
        add(new RadioButtonField("Option 2",rbg,false));
    }
    public boolean onSavePrompt()
    {
        return true;
    }
}

Was this information helpful? Send us your comments.