0% found this document useful (0 votes)
27 views

c7 - User Input, Variables and Operations

Uploaded by

Rheden Gimena
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

c7 - User Input, Variables and Operations

Uploaded by

Rheden Gimena
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Mobile Programming

In this module, we will see how the Android Resources function works. We will also
learn to use Spinners and Hints. And lastly, we will have an idea on the importance of using
Spinners over Radio Buttons in an android application in providing lists.

Objectives:

At the end of the session, the student should be able to:

1. Understand the uses of Spinner Controls and Hint Property Controls;

2. Know how to utilize the Android Resources Functions;

3. Test and run an android application program that will feature Spinner List and Hints; and

4. Familiarize himself with the terms relevant to the topic;

USER INPUT, VARIABLES AND 1


OPERATIONS
A. Spinner Controls
Spinners are drop-down menus that allow the user
to pick one item from a group of choices. They provide
similar functionality to radio buttons, but they take up less
space on the screen and make it easier to see the selected
item. For these reasons, it is advised to use a spinner
instead of radio buttons if you’re offering more than four or
five options for a single field.
While they may offer similar functionality as radio
buttons, spinners require an entirely different API. Their
items are populated using an Adapter, which makes
working with them more like working with list views and
grid views rather than radio buttons or checkboxes.

In this section, we’ll learn how to create a Spinner,


populate it with an ArrayAdapter, and handle user input
with an OnItemSelectedListener. This is almost the exact
pattern we used to configure list views and grid views
earlier in this book. The activity_spinner.xml layout file
and the SpinnerActivity.java class demonstrate how to set a
TextView’s color with a Spinner.
Populating the Spinner Programmatically

First, we create an ArrayList to represent the


options:

Then, we use this ArrayList to create an


ArrayAdapter. Remember from our work with list views
that an ArrayAdapter is what converts the data items into
View objects for display by the spinner. Android provides a
built-in spinner item resource, accessible via
android.R.simple_spinner_item. But, since spinners are
dropdown widgets, we also need to set the adapter’s
dropDownViewResource property. The built-in android.R.
simple_spinner_dropdown_item is the preferred resource to
use for this:

Then, we can give the adapter to the Spinner that


we defined in the layout file:
If you compile the project at this point, you
should be able to see the selected item in the spinner
and be able to tap it to see the dropdown menu. But,
to make it actually do something, we need to
implement a selection handler. It’s important not to
confuse the OnItemSelected Listener class that we
need for the spinner with the OnItemClickListener
that we used with the ListView and GridView in the
previous chapter. The former has an additional
method that we need to implement (though it
doesn’t necessarily need to do anything):
The onItemSelected() method is where the
click is handled. The above code fetches the
selected String using the adapter’s getItemAt
Position() method. We then pass it off to a method
called setTextColor(), which looks like this:

This takes the String values displayed in the


spinner, turns them into hex values, and updates the
TextView’s color accordingly. This is all that’s required to
get our spinner working.

Populating the Spinner Dynamically


If you don’t like defining the list items
programmatically, it’s possible to put them in an XML
resource file and load it into an adapter dynamically. This
is a better practice than hardcoding values in activity
classes, as it keeps all of your text values in XML files.
Since it’s easy to load different resource files based on the
device and the user’s locale, this makes it a breeze to
translate your app into other languages
First we need to define a string array in strings.xml:

To load these items into the spinner, all you have to


do is replace the current ArrayAdapter with one created
from the resource file:

R.array.spinnerColors is the ID of the string array


we just created, and the static
ArrayAdapter.createFromResource() method takes care of
everything else for us. This will have the exact same effect
as hardcoding the ArrayList.
B. Hint Property Controls
Editable text fields or Hints have a distinct
appearance and behavior from static ones. They use
an underline and a hint to show the user that it is
meant for collecting input, and when the user taps
an editable text field, an on-screen keyboard—also
called a soft keyboard (opposed to a hardware
keyboard)—appears.

Instead of the <TextView> element, editable


text views are created with the <EditText> element.
For example, the text view in the above screenshot
was created from the following XML:
Editable text field hints are one of the most
important attributes to set. They function as labels,
telling the user what kind of input is expected.

The android:inputType Function

The android:inputType attribute lets you


specify what kind of input you’re expecting. This
can have a drastic impact on usability, since it
determines what type of soft keyboard is displayed.
For example, if you only wanted to collect a phone
number, you would use phone for this value. Instead
of a full keyboard, this will make Android display a
dial pad, making it much easier to enter the desired
input.

There is a plethora of built-in input types,


and many of them can be combined to give app
developers refined control over the user experience.
Some of the most common values for
android:inputType values are listed below (see the
android:inputType documentation for all of the
available options):
text – Use a normal text keyboard.
textEmailAddress – Use a text keyboard
with the @ character readily available.

textUri – Use a text keyboard with the /


character readily available.

number – Use a number keypad without


traditional dial pad letters.

phone – Use a number keypad with


traditional dial pad letters (e.g., the 2 key
also displays ABC).

textCapWords – Capitalize each word


that the user types.

textCapSentences – Capitalize the first


letter of each sentence.

TextAutoCorrect – Auto correct


misspelled words using Android’s built-
in dictionary.

textPassword – Hide characters after


they have been typed.

datetime – Use a number keypad with a


/ character readily available.

Some of these values can be


combined using a bitwise operator (|). For
example, if you wanted an <EditText>
element to use a text input, capitalize
sentences, and auto correct misspelled
words, you would use the following:
Another way to customize the soft
keyboard is with the android:imeOptions
attribute. This defines what is used as the
Done button. For example, if you wanted to
display Send as the final action after the user
is finished entering input, you would add the
following line to the the <EditText>
element:

The resulting keyboard is shown in


the following screenshot. Notice how the
Done button from the previous examples
turned into a Send button.

The most common values for android:


imeOptions are: actionDone, actionSend,
actionSearch, and actionNext, all of which are
self-explanatory.
C. How to Utilize the Android Resources Function

It takes more than just code to build a great


app. Resources are the additional files and static
content that your code uses, such as bitmaps, layout
definitions, user interface strings, animation
instructions, and more.

The Android resource function keeps track


of all non-code assets associated with an
application. You can use this class to access your
application's resources. You can generally acquire
the Resources instance associated with your
application with getResources().

The Android SDK tools compile your


application's resources into the application binary at
build time. To use a resource, you must install it
correctly in the source tree (inside your
project's res/ directory) and build your application.
As part of the build process, the SDK tools generate
symbols for each resource, which you can use in
your application code to access the resources.

Using application resources makes it easy to


update various characteristics of your application
without modifying code, and—by providing sets of
alternative resources—enables you to optimize your
application for a variety of device configurations
(such as for different languages and screen sizes).
This is an important aspect of developing Android
applications that are compatible on different types
of devices.
(Note: Please check the Module 7 video
tutorial for the demonstration of all topics discussed
in user input, variables and operations).

Reference:

Rahman K. (2013). Android Development Tools for


Eclipse. (1st ed.) Packt Publishing

Hodson R. (2014). Android Programming


Succinctly (1st ed.) www.syncfusion.com

http://www.developer.android.com

You might also like