You are on page 1of 42

Android development

Standard
Android
Controls
(TextView,EditText and ImageView)


Displaying Text to Users with
TextView
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 android.widget package.

General Properties Of TextView
1. The text Property
To display text in TextView we use its
android:text attribute in XML and there are 2
ways to set it:
a. As raw text
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some sample text here" />
General Properties Of TextView
b. Via strings.xml
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=@string/sample_text" />

To change the text displayed programmatically we
call the setText() method on the TextView object.
Retrieving the text is done with the getText()
method.
General Properties Of TextView
2. The typeface Property
To change font type of text in TextView we use its
android:typeface attribute in XML and there are
4 values for it: normal,sans,serif,monspace
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some sample text here
android:typeface=serif" />
General Properties Of TextView
To change the font type programmatically we call
the setTypeface() method on the TextView object.
Eg:
tv.setTypeface(Typeface.SANS_SERIF);

The argument to the method is static object
SANS_SERIF of the class Typeface.
Others are
SERIF,DEFAULT,MONOSPACE
General Properties Of TextView
3. The textStyle Property
To change font style of text in TextView we use its
android:textStyle attribute in XML and there are
3 values for it: normal,bold,italic
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some sample text here
android:textStyle=bold" />
General Properties Of TextView
To change the font style programmatically we call the
setTypeface() method on the TextView object.
Eg:
tv.setTypeface(null,Typeface.BOLD);
This method accepts 2 arguments . The first
argument is font type and second is font style.
Values for 1
st
arg are
SERIF,SANS_SERIF,DEFAULT,MONOSPACED
Values for 2
nd
arg are
BOLD,ITALIC,BOLD_ITALIC,NORMAL
General Properties Of TextView
4. The textColor Property
To change font color in TextView we use its
android:textColor attribute in XML and the
values are passed in hexadecimal format which is
#rgb. For eg: #000000,#FF0000
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some sample text here
android:textColor=#00FF00" />
General Properties Of TextView
To change the font color programmatically we call the
setTextColor() method on the TextView object.
Eg:
tv.setTextColor(Color.BLUE);
This method accepts the integer representation of
the Color constant . These color constants are
static members of Color class.
Some possible values are
BLUE,RED,GREEN,BLACK,WHITE etc
Creating Contextual Links in
TextView
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 .
The autoLink attribute has four values that we can use in
combination with each other.
When enabled, these autoLink attribute values create
standard web-style links to the application that can act on
that data type.
For instance, setting the attribute to web automatically
finds and links any URLs to web pages.
Creating Contextual Links in
TextView
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=@string/sample_text
android:autoLink=web|email|phone" />

Creating Contextual Links in
TextView


Creating Contextual Links in
TextView
Following are the values for the autoLink attribute:
none: Disables all linking.
web: Enables linking of URLs to web pages.
email: Enables linking of email addresses to the mail
client with the recipient filled.
phone: Enables linking of phone numbers to the dialer
application with the phone number filled out, ready to be
dialed.
map: Enables linking of street addresses to the map
application to show the location.
all: Enables all types of linking.

Creating Contextual Links in
TextView
To set the autoLink property programmatically we call
the setAutoLinkMask() method on the TextView
object.
Eg:
tv.setAutoLinkMask(Linkify.PHONE_NUMBERS|Lin
kify.WEB_URLS);
This method accepts the integer argument which
are the static members of the class Linkify
Its possible values are
PHONE_NUMBERS,WEB_URLS,MAP_ADDRESSES,
EMAIL_ADDRESSES,ALL
Retrieving Data from Users with
EditText
The Android SDK provides a number of
controls for retrieving data from users.
The Android SDK provides a convenient
control called EditText to handle text input
from a user.
The EditText class is derived from TextView.
In fact, most of its functionality is contained
within TextView but is enabled when created
as an EditText.
Creating An EditText Control
To define an EditText control in an XML layout
file we can use the following code:
<EditText
android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:hint="type here"
android:layout_width="match_parent" />

Creating An EditText Control
In the previous code a property worth noting
is:
android:hint
This property puts some text in the edit box that
goes away when the user starts entering text .
Allowing User to Type Multiple Lines
To let the user type multiple lines of input we
need to set the inputType attribute of EditText
<EditText
android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:hint="type here
android:inputType=textMultiLine
android:layout_width="match_parent" />

Restricting Number Of Display Lines
To restrict the displayable number of lines we can
use the android:lines property.
If this is not set, the entry field grows as the user
enters text. However, setting a size allows the
user to scroll within a fixed sized to edit the text
<EditText
android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:hint="type here
android:lines=4
android:layout_width="match_parent" />

Restricting User Input To Digits
To restrict the user input to digits only we can
use the android:digits property.
If this is not set, the entry field allows the user to
enter text as well. However, setting a size allows
the user to input numbers only
<EditText
android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:digits=0123456789
android:layout_width="match_parent" />

Accepting Passwords In EditText
To provide a password interface to EditText
we can set its android:inputType property.
The value given will be textPassword
<EditText
android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:inputType=textPassword
android:layout_width="match_parent" />

Accepting Passwords In EditText
To set it via java code we need to call the
setInputType( ) method on EditText object

ed.setInputType(InputType.TYPE_CLASS_TEXT|
InputType.TYPE_TEXT_VARIATION_PASSWORD)
;
This method receives an integer argument which
is framed by btiwise ORing members of
InputType class
Capitalizing User Input
In order to capitalize what user types in an
EditText we can use its capitalize property
<EditText
android:id="@+id/EditText01"
android:layout_height="wrap_content"
android:capitalize=characters
android:layout_width="match_parent" />
Other possible values are
none,words,sentences

Two Useful Methods Of EditText
The EditText control provides us 2 very useful
methods using which we can get/set text in
EditText.
These are :
public Editable getText()
(for obtaining contents of EditText)
public void setText(CharSequence)
(for setting contents of EditText)

The Toast Class
A toast is a view containing a quick little message for
the user. The toast class helps us create and show
those.
When the view is shown to the user,it appears as a
floating view over the application. It will never receive
focus. The user will probably be in the middle of typing
something else.
The idea is to be as unobtrusive as possible, while still
showing the user the information you want them to
see. Two examples are the volume control, and the
brief message saying that your settings have been
saved.


How To Create Toast ?
To create a new Toast, we call a static method
on the Toast class named makeText. There
are two different makeText methods. One of
them takes in a String that can be from any
source you want. The second expects a String
resource ID.

public static Toast makeText (Context ,
CharSequence , int )
To make a standard toast that just contains a
text view with the text as a constant String.
Parameter Desc:
1.Reference to current Context
2.Message To Show
3.Duration



1.First argument can be this or
getApplicationContext( )
2.Second argument can be any message
3. Third argument are any of following 2 constants:
public static final int LENGTH_LONG : Show the
view or text notification for a long period of time.
public static final int LENGTH_SHORT :Show the
view or text notification for a short period of time.



The Code

Toast t;
T=Toast.makeText(this,Hello,Toast.LENGTH_LONG);
How To Show Toast ?
To show Toast Messages we need to call its method
show()
Its prototype is
public void show ()
The Code:
Toast t;
t=Toast.makeText(this,Hello,Toast.LENGTH_LONG);
t.show( );

Other Methods
public void setText (CharSequence)
To update the text in a Toast that was
previously created using one of the
makeText() methods.
public void setDuration (int duration)
To set how long to show the message
for.

Displaying Images
When it comes to displaying images, Android
supports three common image formats:
PNG
JPG
GIF.
The images for the Android application are
stored in the directory res/drawable
Displaying Images
While creating a new application, ADT creates
several folders:
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi and
drawable-xxhdpi
Each directory is meant for storing images of
different screen resolutions.
Displaying Images
To support devices of different densities, we can
store the images of low, medium, high, and extra
high resolutions in these folders.

The images with resolutions of 480dpi,320dpi,
240dpi, 160dpi, and 120dpi and sizes 144*144px,
96x96px, 72x72px, 48x48px, and 36x36px are
usually stored in the res/drawable-xxhdpi,
res/drawable-xhdpi res/drawable-hdpi,
res/drawable-mdpi, and res/drawable-ldpi
folders, respectively

Adding Images
Copy images to any of resource folders.
All image filenames should be lowercase and contain only
letters, numbers, and underscores.
After we add images to the res/drawable folders, the gen
folder is regenerated where the R.java file resides. The
R.java file includes a reference to the newly added image
and hence can be used in the layout file or other Java code.
The syntax for referencing the image in the layout file is
@drawable/image_filename
In Java code, the image can be referenced using the
following syntax:
R.drawable.image_filename

The ImageView Control
The class used to display images is
android.widget.ImageView
The ImageView class can load images
from various sources (such as resources
or content providers), takes care of
computing its measurement from the
image so that it can be used in any layout
manager
Steps Reqd To display Image

1. Store the image in respective folder.
2. Add the ImageView widget to XML file
3.Set the src attribute of ImageView to the
image to be displayed. Its syntax is:
android:src=@drawable/<img nam>
For eg:
android:src=@drawable/smiley
Steps Reqd To display Image
4. To programmatically set the image we call
the setImageResource() method of
ImageView whose protoype is:

public void setImageResource (int resId)

You might also like