Android Programming Lesson 3
Android Programming Lesson 3
Android Programming Lesson 3
LESSON 3
Agenda
• ImageButton
• Switch
• ProgressBar
• SeekBar
• UI Events
• Option menu and Context Menu
• Inflate XML for Menu
ImageButton
• Create a button with a custom image instead of text,
using the ImageButton widget
Switch (On/Off)
• In Android, Switch is a two-state toggle switch widget that can select
between two options. It is used to display checked and unchecked state
of a button providing slider control to user. Switch is a subclass of
CompoundButton. It is basically an off/on button which indicate the
current state of Switch. It is commonly used in selecting on/off in Sound,
Bluetooth, WiFi etc.
UI Events
• Define an event listener and register it with the View.
The View class contains a collection of nested interfaces named
OnxxxListener, each with a callback method called On().
For example:
• View.OnClickListener: for handling "clicks" on a View.
• View.OnTouchListener: for handling touch screen events in a View.
• View.OnKeyListener: for handling device key presses within a View.
• So if we want our View to be notified when it is "clicked" (such
as when a button is selected),
• implement OnClickListener
• define its onClick() callback method (where we perform the action upon
click)
• register it to the View with setOnClickListener().
UI Events
• Override an existing callback method for the View.
This is what we should do when we've implemented our own
View class and want to listen for specific events that occur
within it.
• Example events we can handle include:
• onTouchEvent()
Called when the touchscreen is pressed or released, or when it detects
movement.
• onTrackballEvent()
Called when the device's trackball is moved.
• onKeyDown()
Called when any device key is pressed; includes the D-pad, keyboard, hang-up,
call, back, and camera buttons.
• onKeyUp()
Called when a user releases a pressed key.
ProgressBar
• Progress bar is useful to tell user that the task is takes longer time to
finish.
• The key to use progress bar is using “Thread” to run your time consume
task and another “Thread” to update the progress bar status accordingly.
SeekBar
• A SeekBar is an extension of ProgressBar that adds a draggable thumb.
The user can touch the thumb and drag left or right to set the current
progress level or use the arrow keys.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Menu Item 1" />
<item android:title="Menu Item 2" >
<menu >
<item android:title="Menu Item 2.1" />
<item android:title="Menu Item 2.2" />
</menu>
</item>
<item android:title="Menu Item 3" />
</menu>
Excercise 1
• Develop an application:
• With GUI the same with Picture
• Click on an option to change the picture
• Tap on the Hide Image switch option to toggle the
display of the image
Exercise 2
• Develop an application:
• With GUI the same with Picture
• Image Buttons:
• Full: all progress of seekbar reach 100%
• Clear ALL: all progress of seekbar return 0%
• Option Menu:
• Half: all progress of seekbar reach 50%
• Full: all progress of seekbar return 100%
• Long press on “Show Popup Menu” button: Hide
other buttons, Show other buttons, Say Goodbye