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" >
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" >