CHAPTER 7
MORE
EVENT HANDLING
&
STANDARD UI ELEMENTS
Handling Multiple Sources
If we have set multiple sources(like buttons) for a single event
then we use the method getId() of the View class whose object
is passed as an argument to the method onClick() .
This method returns the id of the widget which triggered the
event. The prototype of the method is:
public int getId()
This id can then be compared with [Link] values to determine the
specific widget.
EXERCISE
Modify previous app to display 2 Buttons titled
“Increment” and “Decrement” which when clicked
increment and decrement the counter accordingly.
SOLUTION(activity_main
SOLUTION( activity_main))
<RelativeLayout
.>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:id=“@+id/textView” />
<Button
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“Increment”
android:textAllCaps=“false”
android:id=“@+id/btnincr”
android:layout_below=“@+id/textView”
android:layout_alignLeft=“@id/textView”
/>
SOLUTION(activity_main
SOLUTION( activity_main))
<Button
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“Decrement”
android:textAllCaps=“false”
android:id=“@+id/btndecr”
android:layout_below=“@+id/textView”
android:layout_toRightOf=“@id/btnincr”
/>
</RelativeLayout>
SOLUTION([Link]
SOLUTION( [Link]))
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity implements
OnClickListener {
private TextView tv;
private Button btn1;
private Button btn2
SOLUTION([Link]
SOLUTION( [Link]))
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link]);
tv=(TextView)findViewById([Link]);
btn1=(Button)findViewById([Link]);
[Link](this);
btn2=(Button)findViewById([Link]);
[Link](this);
}
SOLUTION([Link]
SOLUTION( [Link]))
@Override
public void onClick(View v) {
int source=[Link]();
switch(source) case [Link].button2:
{ --count;
case [Link].button1: break;
}
++count;
[Link]([Link](count));
break; }
}
Event Handling Using XML
Rather than using the OnClickListener we have an alternate
way of handling events also using XML file. It’s as follows:
1. Assign a method to your button in the XML layout, using the
android:onClick attribute. For example:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text=“Click Me"
android:onClick=“myClick" />
Event Handling Using XML
2. Now, when a user clicks the button, the Android system
calls the activity's myClick(View) method. In order for this to
work, the method must be public and accept a View as its
only parameter. For example:
public void myClick(View v) {
// code
}
The View passed into the method is a reference to the
widget that was clicked.
Standard UI Elements
A User Interface is essentially a collection of visual objects
arranged on the screen; the user can see them and interact
with them.
In an Android app UI is built using a hierarchy
of View and ViewGroup objects.
View And ViewGroups
View objects are the basic building blocks of User
Interface(UI) elements in Android.
View is a simple rectangle box which responds to the user's
actions.
Examples are EditText, Button, CheckBox etc..
View refers to the [Link] class, which is the
base class of all UI classes.
View And ViewGroups
ViewGroup is the invisible container. It holds the View and
other ViewGroups.
For example, LinearLayout is the ViewGroup that contains
Button(View), and other Layouts also.
ViewGroup is the base class for Layouts.
View And ViewGroups
The View Class
An Android user interface is built around an object called a View.
A View describes every interactive visual control object that
appears on an application screen.
This means that every control object in an Android user interface is a
subclass of the Android View class, or more
precisely [Link].
Some very common sub-classes of View are Button , TextView,
CheckBox , RadioButton, EditText, ImageView, ListView, Spinner
The TextView Class
One of the most basic user interface elements, or controls,
in the Android SDK is the TextView control.
We use it simply, to display fixed text strings or labels.
As with most of the user interface elements, it is derived
from the class View and is within the [Link]
package.
Important Properties Of TextView
1. The “text” Property
To display text in TextView we use it’s “android:text
android:text””
attribute in XML and there are 3 ways to set it:
a. As raw text
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=“Welcome To Android" />
Important Properties Of TextView
b. Using “[Link]”
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=“@string/message" />
Important Properties Of TextView
c. Using java code
To change the text displayed programmatically we call the
setText()
setText () method on the TextView object.
Retrieving the text is done with the getText
getText()
() method.
Important Properties Of TextView
2. The “typeface”
“ ” Property
To change font type of text in TextView we use it’s
“android:typeface
android:typeface”” attribute in XML and there are 4 values
for it: normal
normal,sans
sans,serif
serif,monospace
monospace
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some sample text here“
android:typeface=“serif” />
Important Properties Of TextView
To change the font type programmatically we call the
setTypeface(Typeface)
setTypeface (Typeface) method on the TextView object.
Eg::
Eg
[Link]((Typeface.SANS_SERIF
[Link] Typeface.SANS_SERIF); );
The argument to the method is static object SANS_SERIF of
the class “Typeface”.
Others are SERIF,DEFAULT,MONOSPACE
Important Properties Of TextView
3. The “textStyle” Property
To change font style of text in TextView we use it’s
“android:textStyle
android:textStyle”” attribute in XML and there are 3 values
for it: normal
normal,bold
bold,italic
italic
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some sample text here“
android:textStyle=“bold" />
Important Properties Of TextView
To change the font style programmatically we call the
setTypeface((Typeface,int
setTypeface Typeface,int)) method on the TextView object.
Eg::
Eg
[Link]((null,[Link]
[Link] null,[Link]););
This method accepts 2 arguments . The first argument is font type
and second is font style.
Values for 1st arg are SERIF,SANS_SERIF,DEFAULT,MONOSPACE
Values for 2nd arg are BOLD,ITALIC,BOLD_ITALIC,NORMAL
Important Properties Of TextView
4. The “textColor” Property
To change font color in TextView we use it’s
“android:textColor
android:textColor”” attribute in XML and the values are
passed in hexadecimal format which is “#rgb”.
For eg: “#000000” , ”#FF0000”
<TextView
android:id="@+id/
android:id ="@+id/textView
textView""
android:layout_width="
android:layout_width ="wrap_content
wrap_content""
android:layout_height="
android:layout_height ="wrap_content
wrap_content""
android:text="Some
android:text ="Some sample text here“
android:textColor=“#00FF00" />
Important Properties Of TextView
To change the font color programmatically we call the
setTextColor((int
setTextColor int)) method on the TextView object.
Eg::
Eg
[Link]([Link]);
This method accepts the integer representation of a
Color constant .
These color constants are static members of Color class.
Some possible values are BLUE,RED,GREEN,BLACK,WHITE etc
Important Properties Of TextView
To change the font color programmatically we call the
setTextColor((int
setTextColor int)) method on the TextView object.
Eg::
Eg
[Link]([Link]);
This method accepts the integer representation of a
Color constant .
These color constants are static members of Color class.
Some possible values are BLUE,RED,GREEN,BLACK,WHITE etc
Creating Contextual Links
If our text contains references to email addresses, web pages,
phone numbers, or even street addresses, we might want to consider
using the attribute autoLink .
This attribute creates links to a particular application that can act on
the clicked value.
For example , if it is set to web then any URL contained in our text
will automatically act as clickable link and when the user taps on it
the browser application will be launched and the respective page
will open.
Creating Contextual Links
<TextView
android:id="@+id/TextView01"
android:id ="@+id/TextView01"
android:layout_width="
android:layout_width ="wrap_content
wrap_content""
android:layout_height="
android:layout_height ="wrap_content
wrap_content""
android:text=“@string/
android:text =“@string/sample_text
sample_text““
android:autoLink=“
android:autoLink =“web|email|phone
web|email|phone"" />
Creating Contextual Links
Creating Contextual Links
Creating Contextual Links
Creating Contextual Links
Following are the values for the autoLink attribute:
• none:
none Disables all linking.
• web:
web Enables linking of URLs to web pages.
• email:
email Enables linking of email addresses to the mail client with
the recipient filled.
• phone:
phone Enables linking of phone numbers to the dialer
application with the phone number filled out, ready to be dialed.
• map:
map Enables linking of street addresses to the map application
to show the location.
• all:
all Enables all types of linking.
Creating Contextual Links
To set the autoLink property programmatically we call the
setAutoLinkMask((int
setAutoLinkMask int)) method on the TextView object.
Eg::
Eg
[Link]((Linkify.PHONE_NUMBERS|[Link]
[Link]
B_URLS);
B_URLS );
This method accepts the integer argument which are the
static members of the class Linkify
It’s possible values are PHONE_NUMBERS, WEB_URLS,
MAP_ADDRESSES, EMAIL_ADDRESSES,ALL
End Of Lecture
For any queries mail us @: scalive4u@[Link]
Call us @ : 0755-4271659, 7879165533
Agenda for Next Lecture:
1. Using EditText
2. Using Toast
3. Using ImageView