Friday, June 21, 2013

Android 1: Lesson 12

Android 1: Lesson 12: Image Swiper Part 1


INTRODUCTION
In this lesson you will be Importing a few more pictures into your app, and you’ll be creating an Activity that allows you to swipe in between them
LESSON OBJECTIVES
By the end of this lesson, you will be able to:
1. Swipe in between a few images
2. Add another Activity to the menu
3. Completely understand the manifest
LEARNING SEQUENCE



Required Reading
Read the following:
Introduction To Development Environment
·        Eclipse
·        Android
·        Considerations for beginning programming.
Resources
View the following:
1.      Eclipse
2.     Android Development
Assignments
XML
  1. Before we get into the xml, import a few pictures into your app, put them the same place you put the logo and the icons. Can be any size. Name them pic1, pic2, and so on
  2. Create a new XML called anything you’d like. I chose image. Keep it in RelativeLayout
  3. make the width and height both ‘match_parent’
  4. Under that, Type ‘
  5. Make the id for it ‘switcher’ and the width and height both ‘match_parent’
  6. Type ‘android:layout_alignParentTop=”true”’
  7. Below that, type ‘
  8. Make the id for it ‘gallery’
  9. make the background for it any color, I picked ‘#55000000’ which is black.
  10. Type out the align piece of code, but chose bottom. Make it true. You can actually switch this and the Imageswitcher, depending on where you want the pictures.
  11. Make the gravity ‘center_vertical’
  12. Type ‘android:spacing=”16dp’ or you can pick any number, 16dp looks good though
Java
  1. Start a new class, I called it ‘ImageSwitcher’
  2. Import ‘android.widget.ImageSwitcher;’
  3. extend the Activity
  4. ‘implements AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory’
  5. Import the Activity, leave the other two for now.
  6. Import via override onCreate(Bundle)
  7. Under the ‘super’ type ‘requestWindowFeature(Window.FEATURE_NO_TITLE);
  8. Enter ‘setContentView(R.layout.image);’
  9. Ignore the errors. Enter ‘mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);’
  10. Enter ‘mSwitcher.SetFactor(this);’
  11. Enter ‘mSwitcher.SetInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in);’ import it.
  12. Enter and copy and paste the code from above, but change ‘in’ to ‘out’
  13. Go to the top, and import ‘android.widget.AdapterView;’, now move back down, below the override. and import the ‘onItemSelected’ and ‘onNothingSelected’
  14. For ‘ItemSelected’ erase what’s after ‘AdapterView’ and type ‘parent, View v, int position, long id) {‘
  15. Under that type ‘mSwitcher.setImageResource(mImageIds[position]);’
  16. Go to the top, and import ‘android.widget.ViewSwitcher;’ don’t touch any of the errors. Go back down to where you were typing, and under ‘NothingSelected’ import the ‘View makeView’

No comments:

Post a Comment