The Switch control was introduced in API Level 14 Using CheckBox and

The switch control was introduced in api level 14

This preview shows page 50 - 56 out of 99 pages.

The Switch control was introduced in API Level 14.
Image of page 50

Subscribe to view the full document.

Using CheckBox and ToggleButton Controls (Cont’d) <Switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch me?" android:textOn="Wax On" android:textOff="Wax Off" />
Image of page 51
Using RadioGroup and RadioButton You often use radio buttons when a user should be allowed to select only one item from a small group of items. For instance, a question asking for gender can give three options: male, female, and unspecified. Only one of these options should be checked at a time. The RadioButton objects are similar to CheckBox objects. They have a text label next to them, set via the text attribute, and they have a state (checked or unchecked). However, you can group RadioButton objects inside a RadioGroup that handles enforcing their combined states so that only one RadioButton can be checked at a time. If the user selects a RadioButton that is already checked, it does not become unchecked. You can provide the user with an action to clear the state of the entire RadioGroup so that none of the buttons are checked.
Image of page 52

Subscribe to view the full document.

Using RadioGroup and RadioButton (Cont’d) <RadioGroup android:id="@+id/RadioGroup01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/RadioButton01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" /> <RadioButton android:id="@+id/RadioButton02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" /> <RadioButton android:id="@+id/RadioButton03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 3" /> <RadioButton android:id="@+id/RadioButton04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 4" /> </RadioGroup>
Image of page 53
Using RadioGroup and RadioButton (Cont’d) final RadioGroup group = (RadioGroup)findViewById(R.id.RadioGroup01); final TextView tv = (TextView) findViewById(R.id.TextView01); group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged( RadioGroup group, int checkedId) { if (checkedId != -1) { RadioButton rb = (RadioButton) findViewById(checkedId); if (rb != null) { tv.setText("You chose: " + rb.getText()); } } else { tv.setText("Choose 1"); } } });
Image of page 54

Subscribe to view the full document.

Using RadioGroup and RadioButton (Cont’d) You can handle actions on these RadioButton objects through the RadioGroup object. The following example shows registering for clicks on the RadioButton objects within the RadioGroup and setting the text of a TextView called TextView01 , which is defined elsewhere in the layout file. As previous layout example demonstrates, there is nothing special you need to do to make the RadioGroup and internal RadioButton objects work properly. The preceding code illustrates how to register to receive a notification whenever the RadioButton selection changes.
Image of page 55
Image of page 56
  • Summer '16

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes