Day 2 android view tutorial - 13092012

13
Mr. Pritesh N. Patel Assistant Professor MCA ISTAR, V. V. Nagar Mr. Pritesh N. Patel

Transcript of Day 2 android view tutorial - 13092012

Page 1: Day   2 android view tutorial - 13092012

Mr. Pritesh N. PatelAssistant Professor

MCAISTAR, V. V. Nagar

Mr. Pritesh N. Patel

Page 2: Day   2 android view tutorial - 13092012

View are user interface control that are display on screen.

All views are in android.view pacakage.

All of the views in a window are arranged in asingle tree. You can add views either from codeor by specifying a tree of views in one or moreXML layout files

Mr. Pritesh N. Patel

Page 3: Day   2 android view tutorial - 13092012

Button – Use to perform user actionTextView – Use to

perform display textEditText – Use to input

data from user

CheckBox – Use to select multiple option

ImageView – Use to display Image

Spinner – Use to select from given list (ComboBox)

RadioButton – Use to select single option

ProgressBar– Use to show work in progress

ToggleButton – Use to selecte on/off state

Mr. Pritesh N. Patel

Page 4: Day   2 android view tutorial - 13092012

XML code<Button

android:id="@+id/sampleBtn"android:text="@string/press_me"android:layout_width="fill_parent"android:layout_height="wrap_content" />

Java code

myBtn=(Button)findViewById(R.id.sampleBtn);myTxt=(TextView)findViewById(R.id.msgTxt);

myBtn.setOnClickListener(new View.OnClickListener() {

@Overridepublic void onClick(View v) {

myTxt.setText("Button has been Pressed for "+(++i)+" Times.");}

});

Mr. Pritesh N. Patel

Page 5: Day   2 android view tutorial - 13092012

XML code

<EditText

android:id="@+id/edittext"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

Java code

myBtn=(Button)findViewById(R.id.sampleBtn);

et = (EditText) findViewById(R.id.edittext);

myBtn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String text=et.getText().toString();Toast msg = Toast.makeText(getBaseContext(),

text, Toast.LENGTH_LONG).show();

}

});Mr. Pritesh N. Patel

Page 6: Day   2 android view tutorial - 13092012

XML code

Code - 1

<Spinner android:layout_height="wrap_content"

android:id="@+id/spinner1"

android:layout_width="fill_parent"

android:prompt=“Select Value"/>

Code – 2

<Spinner android:id="@+id/searchCriteria"

android:entries="@array/countrylist“ />

Entry in string.xml<string-array name=“countrylist">

<item>India</item>

<item>USA</item>

<item>Canada</item>

</string-array>Mr. Pritesh N. Patel

Page 7: Day   2 android view tutorial - 13092012

Java code

ArrayAdapter<String> adapter;String numbers[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE“ };sp = (Spinner) findViewById(R.id.spinner1);

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, numbers)

sp.setAdapter(adapter);

sp.setOnItemSelectedListener(new OnItemSelectedListener(){

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){

Toast.makeText(getBaseContext(), sp.getSelectedItem().toString(),Toast.LENGTH_LONG).show();

}

});Mr. Pritesh N. Patel

Page 8: Day   2 android view tutorial - 13092012

XML code<CheckBox android:id="@+id/check1"

android:layout_width="100px"android:layout_height="wrap_content"android:text="Android" />

Java code

c1 = (CheckBox) findViewById(R.id.check1);if (c1.isChecked()==true && c2.isChecked()==true){

Toast.makeText(this, "Android ", Toast.LENGTH_SHORT).show();

}

Mr. Pritesh N. Patel

Page 9: Day   2 android view tutorial - 13092012

XML code<RatingBar android:id="@+id/ratingbar"android:layout_width="wrap_content"android:layout_height="wrap_content" />

Java coderatingbar = (RatingBar) findViewById(R.id.ratingbar);

ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){

public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser)

{ratings.setText("The Rating is " + rating);

}});

Mr. Pritesh N. Patel

Page 10: Day   2 android view tutorial - 13092012

XML code<SeekBar android:id="@+id/seekbar"

android:layout_width="wrap_content"android:layout_height="wrap_content"android:max="100"android:minWidth="250px" />

Java codeseekbar = (SeekBar) findViewById(R.id.seekbar);seekbar.setOnSeekBarChangeListener( new OnSeekBarChangeListener(){

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){

value.setText("SeekBar value is "+progress);}

public void onStartTrackingTouch(SeekBar seekBar){

// TODO Auto-generated method stub}

public void onStopTrackingTouch(SeekBar seekBar){

// TODO Auto-generated method stub}

});

Page 11: Day   2 android view tutorial - 13092012

XML code<ToggleButton android:id="@+id/togglebutton"

android:layout_width="150px"android:layout_height="50px"android:textOn="ON"android:textOff="OFF" />

Java codetb = (ToggleButton) findViewById(R.id.togglebutton);

tb.setOnClickListener(new OnClickListener(){

public void onClick(View v){

Toast.makeText(getBaseContext(),"Button is "+tb.getText().toString(),

Toast.LENGTH_LONG).show();}

});

Page 12: Day   2 android view tutorial - 13092012

To open new activity following startActivity() method will be used.

Intent i = new Intent(getApplicationContext(), SecondScreen.class);

StartActivity(i); Sending parameters to new Activity

i.putExtra("key", "value"); //Syntaxi.putExtra("email", "[email protected]");

Receiving parameters on new ActivityIntent i = getIntent();i.getStringExtra("key");

String myemail = i.getStringExtra("email");

Mr. Pritesh N. Patel

Page 13: Day   2 android view tutorial - 13092012

Mr. Pritesh N. Patel