Lecture 6 - UI Components
Lecture 6 - UI Components
Lecture 6
1
Wobetu Shiferaw ([email protected])
2 TextView
Example:
With XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/hello" />
</LinearLayout>
4 TextView
android:height
Makes the TextView be exactly this tall.
android:hint
Hint text to display when the text is empty.
8 Attribute
android:textColor
Text color.
android:textAppearance
Base text color, typeface, size, and style.
android:typeface
Typeface (normal, sans, serif, monospace) for the text.
9 EditText
<EditText
android:id="@+id/plain_text_input"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="text"/>
10 EditText
getAccessibilityClassName()
Return the class name of this object to be used for accessibility
purposes.
getFreezesText()
Return whether this text view is including its entire text contents in frozen
icicles.
getText()
Return the text that EditText is displaying
selectAll()
13 Buttons
A user interface element the user can tap or click to
perform an action.
To display a button in an activity, add a button to the
activity's layout XML file:
<Button
android:id="@+id/button_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/self_destruct" />
14 Buttons
To specify an action when the button is pressed, set a click listener
on the button object in the corresponding activity code:
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id);
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_icon"
... />
:
18 Buttons
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:drawableLeft="@drawable/button_icon"
... />
19 Responding to Click Events
When a user clicks a button, the Button object receives
an on-click event
To define the click event handler for a button, add
the android:onClick attribute to the <Button> element in
your XML layout.
The value for this attribute must be the name of the
method you want to call in response to a click event.
The Activity hosting the layout must then implement the
corresponding method.
20 Responding to Click Events
<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
21 Responding to Click Events
You can control exactly how your controls are styled using a theme
that you apply to your entire application.
For instance, to ensure that all devices running Android 4.0 and
higher use the Holo theme in your app,
declare android:theme="@android:style/Theme.Holo" in your
manifest's <application> element.
To customize individual buttons with a different background, specify
the android:background attribute with a drawable or color
resource.
26 Styling Your Button
Alternatively, you can apply a style for the button, which works in a
manner similar to HTML styles to define multiple style properties such
as the background, font, size, and others.
27 Borderless button
style="?android:attr/borderlessButtonStyle"
28 ToggleButton
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="OFF Me"
android:textOff="ON Me"/>
29 Attributes
30 Public Methods
getAccessibilityClassName()
CharSequence getAccessibilityClassName ()
Return the class name of this object to be used for accessibility
purposes.
getTextOff()
Returns the text for when the button is not in the checked state.
Returns text
getTextOff()
Returns the text for when the button is in the checked state.
Returns text
31 Public Methods
setBackground(drawable)
Sets background image for the ToggleButton object
setChecked()
void setChecked (boolean checked)
Changes the checked state of this button.
32 Public Methods
setTextOff()
Sets the text for when the button is not in the checked state.
setTextOn()
Sets the text for when the button is in the checked state.
33 CheckBox
A specific type of two-states button that can be either checked or
unchecked.
Can be utilized as:
setContentView(R.layout.content_layout_id);
Checkboxes allow the user to select one or more options from a set.
Typically, you should present each checkbox option in a vertical list.
35 CheckBox
Within the Activity that hosts this layout, the following method handles the click
event for both checkboxes:
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
38
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_meat:
if (checked)
// Put some meat on the sandwich
else
// Remove the meat
break;
case R.id.checkbox_cheese:
if (checked)
// Cheese me
else
// I'm lactose intolerant
break;
// TODO: Veggie sandwich
}
}
39 Radio Buttons
Radio buttons allow the user to select one option from a set.
You should use radio buttons for optional sets that are mutually
exclusive if you think that the user needs to see all available options
side-by-side.
40 Radio Buttons
android:onClick="onRadioButtonClicked"/>
41 Radio Buttons: Example
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
42 Radio Buttons: Example
Within the Activity that hosts this layout, the following method handles
the click event for both radio buttons:
public void onRadioButtonClicked(View view) {
43 // Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
You can add a spinner to your layout with the Spinner object. You should
usually do so in your XML layout with a <Spinner> element. For example:
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
47
The choices you provide for the spinner can come from
any source,
but must be provided through an SpinnerAdapter, such as
an ArrayAdapter if the choices are available in an array
or a CursorAdapter if the choices are available from a
database query.
49 Populate the Spinner with User Choices
With an array such as this one, you can use the following code in
your Activity or Fragment to supply the spinner with the array using an instance
of ArrayAdapter:
items.
The list items are automatically inserted to the list using
an Adapter
From array or
From database query
58 ScrollView