android development

30
Standard Android Widgets

Upload: cshashank1992

Post on 05-May-2017

214 views

Category:

Documents


1 download

TRANSCRIPT

Standard Android Widgets

Standard Android Widgets

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

TextViewPurpose: To display text to the user

Class: android.widget.TextView

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

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

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

ImageViewPurpose: To display Image to the user

Class: android.widget.ImageView

Steps Reqd To display ImageStore the image in res/drawable-ldpi, res/drawable-mdpi or res/drawable-hdpi folders

*Android development*Add the ImageView widget to XML fileSet the src attribute of ImageView to the image to be displayed. Its syntax is:android:src=@drawable/For eg:android:src=@drawable/smileyTo programmatically set the image we call the setImageResource() method of ImageView whose protoype is:public void setImageResource (int resId)

Example 6Write a program to display an image and change it on button click

main.xml

*Android development*

ImageDemoActivity.javapackage 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);

}}

EditTextPurpose: To display editable text to the user

Class: android.widget.EditText

Important Properties: Along with standard TextView properties following properties are supportedandroid:autoText To control spelling assistanceEg: android:autoText=true

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

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

android:singleLine To allow or disallow multiple linesEg: 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 ClassPurpose: 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 messagepublic 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()

CheckBoxPurpose: 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.

RadioButtonPurpose : 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.RadioButtonCreating Mutually Exclusive RadioButtonsRadioButtons 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 7Write an application to contain three radio buttons representing 3 std colors and clicking on them should change the background color accordingly.

main.xml

*Android development*

RadioDemoActivity.javapackage 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;}

}}