Day 2 android view tutorial - 13092012

439
-1

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
439
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
62
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Day 2 android view tutorial - 13092012

  1. 1. Mr. Pritesh N. Patel Assistant Professor MCA ISTAR, V. V. Nagar Mr. Pritesh N. Patel
  2. 2.  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 a single tree. You can add views either from code or by specifying a tree of views in one or more XML layout files Mr. Pritesh N. Patel
  3. 3. Button – Use to perform user actionTextView – Use to perform display text EditText – 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
  4. 4.  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() { @Override public void onClick(View v) { myTxt.setText("Button has been Pressed for "+(++i)+" Times."); } }); Mr. Pritesh N. Patel
  5. 5.  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
  6. 6.  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
  7. 7.  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
  8. 8.  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
  9. 9.  XML code <RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content" android:layout_height="wrap_content" />  Java code ratingbar = (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
  10. 10.  XML code <SeekBar android:id="@+id/seekbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:max="100" android:minWidth="250px" />  Java code seekbar = (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 } });
  11. 11.  XML code <ToggleButton android:id="@+id/togglebutton" android:layout_width="150px" android:layout_height="50px" android:textOn="ON" android:textOff="OFF" />  Java code tb = (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(); } });
  12. 12.  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"); //Syntax i.putExtra("email", "myemail@gmail.com");  Receiving parameters on new Activity Intent i = getIntent(); i.getStringExtra("key"); String myemail = i.getStringExtra("email"); Mr. Pritesh N. Patel
  13. 13. Mr. Pritesh N. Patel

×