Views & View Events | Android
Hello everybody,
In this post, we are going to discuss on classes that Android provides to help build good looking and effective user interfaces.
A User interface is one which helps the user and the application to exchange information.
The UI presents a good device screen and elements to the user to see and touch the screen according to his needs.
In Android, Activities are used to display a visual user interface.
Android provides a set of classes for constructing such user interfaces.
Let us first discuss on the most commonly used interface classes
VIEW CLASS
A view class is the key building block for the UI components. Every item in a user interface is a subclass of the Android View Class.
Views occupy a rectangular space on the screen.
Views draw themselves and handle the events that are directed to them.
Android provides a set of built-in views to build a user interface and few of the standard UI Items are as follows which are also called as WIDGETS
Button
A button is a view where users can click on to start or perform an action.
ToggleButton
A togglebutton is another kind of button when pressed stays presses and when not pressed stays not pressed. It is either of the two states i.e., on or off
They also display an indicator to the user to let the user know the state of the button is in right at that moment.
We have the music player application as one the best examples for toggle buttons. When you play the music it in ON state and when you pause it the music is in OFF state.
CheckBox
Checkbox is something very common that we come across in our day-to-day life.
Now the checkbox is a two state button just like the toggle button but the difference it with the ways it’s displayed to the user.
Checkboxes display its checking area empty when the user leaves it’s not checked but shows some symbol when it’s checked.
RatingBar
It’s a view that comprises of a row of stars
This view displays a row of stars to rate anything such as a movie, restaurant, book and so on.
The user would click or drag on the stars to represent a number on them
AutoCompleteTextView
AutoCompleteTextView is a text view that is editable which means users can type in text that goes into the text view. But that’s not it, An Editable Text Field that provides complete suggestions as the user types in text and once you narrow down your list you can touch a single element from the list which will be displayed in the text view.
COMMON VIEW OPERATIONS
SET VISIBILITY: This method helps in setting the visibility of the view i.e., to show or hide the view
SET CHECKED STATE: This method helps in changing the checked state of the view
SET LISTENERS: This lets the listeners to execute or perform actions when events occur
SET PROPERTIES: The properties such as opacity, background, position are changed using this method
MANAGE INPUT FOCUS: Views can interact with the keyboard by allowing the views to take focus, request
VIEW EVENT SOURCES
Before we move on further, let’s talk about how events are invoked. Well, there are two means to them
User interaction
Touch – On touching the device screen anywhere would be regarded as input.
Keyboard/Trackball/D-Pad – Using the keypad on the screen / the device would again be considered as valid inputs.
System Interaction
Lifecycle changes –Any changes within the system can also be regarded as event sources.
HANDLING VIEW EVENTS
A significant way of handling events is by attaching the listeners to individual views.
Android defines a number of different kinds of listener interfaces.
Let’s see few of the listeners and their respective methods
VIEW LISTENER INTERFACES
OnClickListener.onClick() – onClick() method of the listener onClickListener is called whenever a view has been clicked.
onLongClickListener.onLongClick() – onLongClick() method of the listener onLongClickListener() is called whenever a view has been pressed and held for a long time
onFocusChangeListener.onFocusChange() – onFocusChange() method of the listener onFocusChangeListener is called whenever the view has received or lost focus.
onKeyListener.onKey() – onKey() method of the listener onKeyListener is called whenever a view is about to receive a hardware key press.
Displaying Views
Views are organised in a tree. There are some outermost views which in turn holds some number of child views and each of those child views can have its own children and so on.
Displaying has multiple steps
MEASURE – At the first stage, it gets the dimension of each view
LAYOUT – At the second stage, it positions each the view
DRAW– At the third stage it draws each view.
Apart from these, if you are to create your own custom view classes then you may want to override the various view methods such that you get your desired view.
These are handled by the following methods
onMeasure() – Android calls this method to determine the size of this view and its children
onLayout() – It must assign a size and position to all its children
onDraw() – This method is called to render its content.
Will keep updating more on the user interface classes in my future posts.
Related Posts
Latest posts by abirami vijayakumar (see all)
- Views & View Events | Android - March 26, 2015
- Intent Class | Android - January 2, 2015
- Android Activity Lifecycle Methods - December 19, 2014