You are on page 1of 30

Introduction

The Android system provides us with some standard


widgets like TextView , Button ,EditText , ImageView
etc which are commonly used in most of the Activities.

Thus knowing their details is essential to proceed
further.
TextView
Purpose: To display text to the user

Class: android.widget.TextView

Important Properties:
android:typeface To display text in typeface like
normal,sans,serif,monospace
Eg:
In XML android:typeface=serif
In java t.setTypeface(Typeface.SERIF);


*Android development*
android:textStyle To display text in normal,bold and italic
Eg:
In XML android:textStyle=italic
In java t.setTypeface(null,Typeface.ITALIC);

android:textColor To set color of the text in hex format
Eg:
In XML android:textColor=#FF0000
In Java tv.setTextColor(Color.GREEN);


ImageView
Purpose: To display Image to the user

Class: android.widget.ImageView

Steps Reqd To display Image
1. Store the image in res/drawable-ldpi,
res/drawable-mdpi or res/drawable-hdpi folders
*Android development*
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
4. To programmatically set the image we call the
setImageResource() method of ImageView whose
protoype is:
public void setImageResource (int resId)

Example 6
Write a program to display an
image and change it on button
click
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android
"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />


*Android development*
<ImageView
android:id="@+id/myimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/scenery" />

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click To Change" />

</LinearLayout>

ImageDemoActivity.java
package kapoor.sachin.androidapps;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class ImageDemoActivity extends Activity
implements OnClickListener {
private Button b1;
private ImageView imgv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.btn1);
imgv1=(ImageView)findViewById(R.id.myimg);
b1.setOnClickListener(this);
}
public void onClick(View v) {

imgv1.setImageResource(R.drawable.android);

}
}
EditText
Purpose: To display editable text to the user

Class: android.widget.EditText

Important Properties:
Along with standard TextView properties following
properties are supported
android:autoText To control spelling assistance
Eg: android:autoText=true

android:capitalize To automatically capitalize
whatever user types. Possible values are
none,sentences,words,characters
Eg: android:capitalize=characters

android:digits To configure text field to only accept
specific digits
Eg: android:digits=12345

android:singleLine To allow or disallow multiple
lines
Eg: android:singleLine=false

Android:inputType To restrict the type of input .
Some possible values are
text,textPassword,number,phone,
date,textMultiLine,time,datetime
Eg: android:inputType=number

Two useful methods of EditText are

public Editable getText()
(for obtaining contents of EditText)

public void setText(CharSequence)
(for setting contents of EditText)

The Toast Class
Purpose: To display quick little message to the user

Class: android.widget.Toast

Important 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.

Important Methods:
public static Toast makeText (Context ,
CharSequence , int )
To make a standard toast that just contains a text
view with the text from a resource.
public static Toast makeText (Context , int , int
)
To make a standard toast that just contains a text
view with the text from a resource id


public void show ()
To display the message
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.


Example:
Toast.makeText(this, Welcome To Android",
Toast.LENGTH_SHORT).show()
CheckBox
Purpose: To display a specific type of two-states button
that can be either checked or unchecked.
Class: android.widget.CheckBox

Important Methods:
public boolean isChecked() To determine the state of
CheckBox
public void setChecked(boolean )
To change the checked state of this button.
RadioButton
Purpose : To display a two-states button that can be
either checked or unchecked. When the radio button is
unchecked, the user can press or click it to check it.
However, contrary to a CheckBox, a radio button cannot
be unchecked by the user once checked.

Class : android.widget.RadioButton
Creating Mutually Exclusive RadioButtons
RadioButtons need to be grouped together in order to
be mutually exclusive and for this we use RadioGroup
objects. When several radio buttons live inside a radio
group, checking one radio button unchecks all the
others.

Example 7
Write an application to contain three radio buttons
representing 3 std colors and clicking on them should
change the background color accordingly.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/l1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose A Color"
android:textSize="30px"/>
*Android development*
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<RadioButton
android:id="@+id/rdred"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Red Color" />


<RadioButton
android:id="@+id/rdblue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue Color" />

<RadioButton
android:id="@+id/rdgreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green Color" />

</RadioGroup>

RadioDemoActivity.java
package kapoor.sachin.androidapps;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class RadioDemoActivity extends Activity
implements OnClickListener{
private RadioButton r1,r2,r3;
private LinearLayout lyt;

*Android development*
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
r1=(RadioButton)findViewById(R.id.rdred);
r2=(RadioButton)findViewById(R.id.rdblue);
r3=(RadioButton)findViewById(R.id.rdgreen);
r1.setOnClickListener(this);
r2.setOnClickListener(this);
r3.setOnClickListener(this);
lyt=(LinearLayout)findViewById(R.id.l1);



}
*Android development*
public void onClick(View v) {
switch(v.getId())
{
case R.id.rdred:
lyt.setBackgroundColor(Color.RED);
break;
case R.id.rdgreen:
lyt.setBackgroundColor(Color.GREEN);
break;
case R.id.rdblue:
lyt.setBackgroundColor(Color.BLUE);
break;
}

}
}

You might also like