UI Controls
• Input controls are the interactive components in your app's user interface.
• Android provides a wide variety of controls you can use in your UI, such as
buttons, text fields, seek bars, check box, zoom buttons, toggle buttons,
and many more.
UI Controls
• A View is an object that draws something on the screen that the user can
interact with and a ViewGroup is an object that holds other View (and
ViewGroup) objects in order to define the layout of the user interface.
UI Controls
[Link]. UI Control & Description
1 TextView This control is used to display text to the user.
2 EditText EditText is a predefined subclass of TextView that includes rich editing
capabilities.
3 AutoCompleteTextView The AutoCompleteTextView is a view that is similar to
EditText, except that it shows a list of completion suggestions automatically while the
user is typing.
4 Button A push-button that can be pressed, or clicked, by the user to perform an
action.
5 ImageButton An ImageButton is an AbsoluteLayout which enables you to specify the
exact location of its children. This shows a button with an image (instead of text) that
can be pressed or clicked by the user.
6 CheckBox An on/off switch that can be toggled by the user. You should use check
box when presenting users with a group of selectable options that are not mutually
exclusive.
UI Controls
[Link]. UI Control & Description
7 ToggleButton An on/off button with a light indicator.
8 RadioButton The RadioButton has two states: either checked or unchecked.
9 RadioGroup A RadioGroup is used to group together one or
more RadioButtons.
10 ProgressBar The ProgressBar view provides visual feedback about some
ongoing tasks, such as when you are performing a task in the background.
11 Spinner A drop-down list that allows users to select one value from a set.
12 TimePicker The TimePicker view enables users to select a time of the day, in
either 24-hour mode or AM/PM mode.
13 DatePicker The DatePicker view enables users to select a date of the day.
Android RadioButton
• RadioButton is a two states button which is either checked or unchecked.
If a single radio button is unchecked, we can click it to make checked
radio button.
• Once a radio button is checked, it cannot be marked as unchecked by
user.
• RadioButton is generally used with RadioGroup. RadioGroup contains
several radio buttons, marking one radio button as checked makes all
other radio buttons as unchecked.
Android Spinner
• Android Spinner is like the combox box of AWT or Swing. It can be used to
display the multiple options to the user in which only one item can be
selected by the user.
• Android spinner is like the drop down menu with multiple values from
which the end user can select only one value.
• Android spinner is associated with AdapterView. So you need to use one
of the adapter classes with spinner.
• Android Spinner class is the subclass of AsbSpinner class.
Android Spinner
Android DatePicker
• Android DatePicker is a widget to select date.
• It allows you to select date by day, month and year. Like DatePicker,
android also provides TimePicker to select time.
• The [Link] is the subclass of FrameLayout class.
Android Input Events (Event Listeners, Event Handling)
• In android, Input Events are used to capture the events, such
as button clicks, edittext touch, etc. from the View objects that defined
in a user interface of our application, when the user interacts with it.
• To handle input events in android, the views must have in place an event
listener. The View class, from which all UI components are derived
contains a wide range event listener interfaces and each listener interface
contains an abstract declaration for a callback method. To respond to an
event of a particular type, the view must register an appropriate event
listener and implement the corresponding callback method.
• For example, if a button is to respond to a click event it must register
to [Link] event listener and implement the
corresponding onClick() callback method. In application when
a button click event is detected, the Android framework will call
the onClick() method of that particular view.
Android Input Events (Event Listeners, Event Handling)
• In android, Input Events are used to capture the events, such
as button clicks, edittext touch, etc. from the View objects that defined
in a user interface of our application, when the user interacts with it.
• To handle input events in android, the views must have in place an event
listener.
• The View class, from which all UI components are derived contains a wide
range event listener interfaces and each listener interface contains an
abstract declaration for a callback method.
• To respond to an event of a particular type, the view must register an
appropriate event listener and implement the corresponding callback
method.
Android Event Listeners
In android, Event Listener is an interface in the View class that contains a
single call-back method. These methods will be called by the Android
framework when the View which is registered with the listener is triggered by
user interaction with the item in UI.
Events are a useful way to collect data about a user's interaction with
interactive components of Applications. Like button presses or screen touch
etc. The Android framework maintains an event queue as first-in, first-out
(FIFO) basis.
Android Event Listeners
• There are following three concepts related to Android Event Management
−
• Event Listeners − An event listener is an interface in the View class that
contains a single callback method. These methods will be called by the
Android framework when the View to which the listener has been
registered is triggered by user interaction with the item in the UI.
• Event Listeners Registration − Event Registration is the process by which
an Event Handler gets registered with an Event Listener so that the
handler is called when the Event Listener fires the event.
• Event Handlers − When an event happens and we have registered an
event listener for the event, the event listener calls the Event Handlers,
which is the method that actually handles the event.
Android Event Listeners
Event Handler Event Listener & Description
OnClickListener()
This is called when the user either clicks or touches or focuses upon any
onClick()
widget like button, text, image etc. You will use onClick() event handler to
handle such event.
OnLongClickListener()
This is called when the user either clicks or touches or focuses upon any
onLongClick()
widget like button, text, image etc. for one or more seconds. You will use
onLongClick() event handler to handle such event.
OnFocusChangeListener()
This is called when the widget looses its focus ie. user goes away from the
onFocusChange()
view item. You will use onFocusChange() event handler to handle such
event.
OnFocusChangeListener()
This is called when the user is focused on the item and presses or releases
onKey()
a hardware key on the device. You will use onKey() event handler to handle
such event.
OnTouchListener()
This is called when the user presses the key, releases the key, or any
onTouch()
movement gesture on the screen. You will use onTouch() event handler to
handle such event.
OnMenuItemClickListener()
onMenuItemClick() This is called when the user selects a menu [Link]
will use onMenuItemClick() event handler to handle such event.
Android - Event Handling and Event Listeners
Events are a useful way to collect data about a user's interaction with interactive
components of Applications. Like button presses or screen touch etc. The Android
framework maintains an event queue as first-in, first-out (FIFO) basis.
There are following three concepts related to Android Event Management −
∙ Event Listeners − An event listener is an interface in the View class that
contains a single callback method. These methods will be called by the
Android framework when the View to which the listener has been registered
is triggered by user interaction with the item in the UI.
∙ Event Listeners Registration − Event Registration is the process by which
an Event Handler gets registered with an Event Listener so that the handler
is called when the Event Listener fires the event.
∙ Event Handlers − When an event happens and we have registered an event
listener for the event, the event listener calls the Event Handlers, which is the
method that actually handles the event.
Event Handler Event Listener & Description
OnClickListener()
onClick() This is called when the user either clicks or touches or
focuses upon any widget like button, text, image etc.
You will use onClick() event handler to handle such
event.
OnLongClickListener()
This is called when the user either clicks or touches or
onLongClick() focuses upon any widget like button, text, image etc. for
one or more seconds. You will use onLongClick() event
handler to handle such event.
OnFocusChangeListener()
onFocusChange() This is called when the widget looses its focus ie. user
goes away from the view item. You will use
onFocusChange() event handler to handle such event.
OnFocusChangeListener()
This is called when the user is focused on the item and
onKey()
presses or releases a hardware key on the device. You
will use onKey() event handler to handle such event.
OnTouchListener()
onTouch() This is called when the user presses the key, releases
the key, or any movement gesture on the screen. You
will use onTouch() event handler to handle such event.
OnMenuItemClickListener()
onMenuItemClick() This is called when the user selects a menu item. You
will use onMenuItemClick() event handler to handle
such event.
onCreateContextMenuItemListener()
onCreateContextMenu(
This is called when the context menu is being built(as
)
the result of a sustained "long click)
Event Listeners Registration
Event Registration is the process by which an Event Handler gets registered with an
Event Listener so that the handler is called when the Event Listener fires the event.
Though there are several tricky ways to register your event listener for any event,
but I'm going to list down only top 3 ways, out of which you can use any of them
based on the situation.
∙ Using an Anonymous Inner Class
∙ Activity class implements the Listener interface.
∙ Using Layout file activity_main.xml to specify event handler directly.
Below section will provide you detailed examples on all the three scenarios −
Touch Mode
Users can interact with their devices by using hardware keys or buttons or touching
the [Link] the screen puts the device into touch mode. The user can then
interact with it by touching the on-screen virtual buttons, images, [Link] can check
if the device is in touch mode by calling the View class’s isInTouchMode() method.
Focus
A view or widget is usually highlighted or displays a flashing cursor when it’s in
focus. This indicates that it’s ready to accept input from the user.
∙ isFocusable() − it returns true or false
∙ isFocusableInTouchMode() − checks to see if the view is focusable in touch
mode. (A view may be focusable when using a hardware key but not when
the device is in touch mode)
android:foucsUp="@=id/button_l"
onTouchEvent()
public boolean onTouchEvent(MotionEvent event){
switch([Link]()){
case TOUCH_DOWN:
[Link](this,"you have clicked down Touch
button",Toast.LENTH_LONG).show();
break();
case TOUCH_UP:
[Link](this,"you have clicked up touch
button",Toast.LENTH_LONG).show();
break;
case TOUCH_MOVE:
[Link](this,"you have clicked move touch
button"Toast.LENTH_LONG).show();
break;
}
return [Link](event) ;
Event Handling Examples
}
Event Listeners Registration Using an Anonymous Inner Class
Here will create an anonymous implementation of the listener and will be useful if
each class is applied to a single control only and you have advantage to pass
arguments to event handler. In this approach event handler methods can access
private data of Activity. No reference is needed to call to Activity.
But if you applied the handler to more than one control, you would have to cut and
paste the code for the handler and if the code for the handler is long, it makes the
code harder to maintain.
Following are the simple steps to show how we will make use of separate Listener
class to register and capture click event. Similar way you can implement your
listener
Ste for any other required event type.
Description
p
1 You will use Android studio IDE to create an Android application and name it
as myapplication under a package [Link] as explained in
the Hello World Example chapter.
2 Modify src/[Link] file to add click event listeners and handlers for the
two buttons defined.
3 Modify the detault content of res/layout/activity_main.xml file to include Android UI
controls.
4 No need to declare default string [Link] studio takes care default
constants.
5 Run the application to launch Android emulator and verify the result of the changes
done in the aplication.
Following is the content of the modified main activity file
src/[Link]/[Link]. This file can include each of
the fundamental lifecycle methods.
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends ActionBarActivity {
private ProgressDialog progress;
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
progress = new ProgressDialog(this);
b1=(Button)findViewById([Link]);
b2=(Button)findViewById([Link].button2);
[Link](new [Link]() {
@Override
public void onClick(View v) {
TextView txtView = (TextView)
findViewById([Link]);
[Link](25);
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
TextView txtView = (TextView)
findViewById([Link]);
[Link](55);
}
});
}
Following will be the content of res/layout/activity_main.xml file −
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event Handling "
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome "
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="@+id/imageButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small font"
android:id="@+id/button"
android:layout_below="@+id/imageButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Font"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_alignRight="@+id/button"
android:layout_alignEnd="@+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:textSize="25dp" />
<?xml version="1.0" encoding="utf-8"?>
</RelativeLayout>
<resources>
<string name="app_name">myapplication</string>
Following will be the content of res/values/[Link] to define two new constants
</resources>
–
Following is the default content of [Link] −
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="[Link]
package="[Link]" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="[Link]"
android:label="@string/app_name" >
<intent-filter>
<action android:name="[Link]" />
<category
android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>