For instance if users are going to enter the name of a state you might as well

For instance if users are going to enter the name of

This preview shows page 34 - 39 out of 99 pages.

For instance, if users are going to enter the name of a state, you might as well limit them to only the valid states, because this is a known set. Although you could do this by letting them type something and then blocking invalid entries, you can also provide similar functionality with a Spinner control. As with the autocomplete method, the possible choices for a Spinner can come from an Adapter . You can also set the available choices in the layout definition by using the entries attribute with an array resource. Specifically, this is a string array that is referenced as something, such as @array/state-list . The Spinner control isn’t actually an EditText , although it is frequently used in a similar fashion.
Image of page 34

Subscribe to view the full document.

Giving Users Choices Using Spinner Controls (Cont’d) <Spinner android:id="@+id/Spinner01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/colors" android:prompt="@string/spin_prompt" /> Here is an example of the XML layout definition for a Spinner control for choosing a color. There are a couple of things to notice here. First, the entries attribute is set to the value of a string array resource, referred to here as @array/colors . Second, the prompt attribute is defined as a string resource. Unlike some other string attributes, this one is required to be a string resource. The prompt displays when the Spinner control is opened and all selections are displayed. The prompt can be used to tell the user what kinds of values can be selected.
Image of page 35
Giving Users Choices Using Spinner Controls (Cont’d)
Image of page 36

Subscribe to view the full document.

Giving Users Choices Using Spinner Controls (Cont’d) Because the Spinner control is not a TextView but a list of TextView objects, you can’t directly request the selected text from it. Instead, you have to retrieve the specific selected option (each of which is a TextView control) and extract the text directly from it: final Spinner spin = (Spinner) findViewById(R.id.Spinner01); TextView text_sel = (TextView)spin.getSelectedView(); String selected_text = text_sel.getText().toString(); Alternatively, we could have called the getSelectedItem() or getSelectedItemId() method to deal with other forms of selection.
Image of page 37
Selections: Buttons, Check Boxes, Switches, Radio Groups Other common UI elements are the basic Button , CheckBox , ToggleButton , and RadioButton . A basic Button is often used to perform some sort of action, such as submitting a form or confirming a selection; A basic Button control can contain a text or image label. A CheckBox is a button with two states checked and unchecked; You often use CheckBox controls to turn a feature on or off or to pick multiple items from a list. A ToggleButton is similar to a CheckBox , but you use it to show the state visually. The default behavior of a toggle is like that of a power on/off button.
Image of page 38

Subscribe to view the full document.

Image of page 39
  • 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