Friday, June 21, 2013

Android 1: Lesson 13

Android 1: Lesson 13: Image Swiper Part 2


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
  1. Above the ‘return null’ type ‘ImageView i = new ImageView(this);’ import it
  2. Enter ‘i.setBackgroundColor(0xff000000);’ this should match up with what you had down in your xml
  3. Enter ‘i.setScaleType(ImageView.ScaleType.FIT_CENTER);’ this is how the picture fits on screen.
  4. Enter ‘i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));’ go to the top and import ‘android.widget.Gallery.LayoutParams’
  5. Change ‘null’ to ‘i’
  6. Under the View ending brackets type ‘private ImageSwitcher mSwitcher;’
  7. Enter ‘public class ImageAdapter extends BaseAdapter’ You are extending another activity within this activity. Import the BaseAdapter, and add unimplemented methods of ImageAdapter
  8. Enter ‘public ImageAdapter(Context c) {
  9. Enter ‘mContext = c;’
  10. When you added the implemented methods of ImageAdapter, it generated some code. For the ‘getCount’ change 0 to ‘mSpaceIds.length’
  11. For ‘getItem’ change null to ‘position’
  12. For ‘getItemId’ change 0 to ‘position’
  13. Move down to the ‘getView; Above the ‘return’ type ‘ImageView i = new ImageView(mContext);’
  14. Enter ‘i.setImageResource(mSpaceIds[position]);’
  15. Enter ‘i.setAdjustViewBounds(true);’
  16. Enter ‘i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));’
  17. Enter ‘i.setBackgroundResource(R.drawable.testbackground);’ ‘testbackground’ should be whatever you named that picture that is in your drawable folder from the Logo lesson.
  18. Change null to i
  19. Under the ‘getView’ ending brackets, type ‘private Context mContext;’ import the context
  20. Under the ending brackets of that type ‘private Integer[] mSpaceIds = {R.drawable.pic1, R.drawable.pic2’
  21. Enter ‘private Integer[] mImageIds = {R.drawable.pic1, R.drawable.pic2’ for pic1 and 2, type out the names of the pictures you put in your app folders.
  22. Export and see how it works!

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’

Android 1: Lesson 11

Android 1: Lesson 11: Accelerometer Part 2



INTRODUCTION
In this lesson you will be getting access to your accelerometer. And being able to read the x, y, and z values that it gives it.
LESSON OBJECTIVES
By the end of this lesson, you will be able to:
1. Access your accelerometer
2. Be able to read what the accelerometer is reading
3. Be able to set up a Sensor and SensorManager Activity
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
  1. Continuing onwards, hit Enter ‘layout = (RelativeLayout)findViewById(R.id.relative);’
  2. Enter ’title=(TextView)findViewById(R.id.name); ’
  3. Enter ’tv=(TextView)findViewById(R.id.xval);’
  4. Enter ’tv1=(TextView)findViewById(R.id.yval);’
  5. Enter ’tv2=(TextView)findViewById(R.id.zval);’
  6. The ‘onAccuracyChanged’ should be untouched, and you won’t need to add any code here.
  7. The ‘onSensorchanged’ however will need a bit of coding. After the brackets add ‘float x = event.values[0];’
  8. Do the same for y and z, but setting y values to 1, and z to 2.
  9. Enter ’title.setText(R.string.app_name); ’ you’ll actually want to type ‘app_name” here, because it is referring to the ‘string.xml’ document, which is under the ‘values’ folder.
  10. Enter ’tv.setText(“X axis” + “\t\t” + x); ‘
  11. You’ll want to do the same for the y and for the x.
  12. Just changing all xs to y and z, and ‘tv’ to ‘tv1’ for y and ‘tv2’ for z.
  13. Import ‘onResume’ and ‘onPause’ like you did for the ‘onCreate(Bundle)’
  14. Under ‘Resume’ , before the ending bracket, you’ll want to type ‘mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);’
  15. Now for ‘Pause’, again before the ending bracket, you’ll want to type ‘mSensorManager.unregisterListener(this);’
  16. Now open up your Manifest.You’ll need to do something slightly different this time. Copy and paste the MainActivity activity below it. And cut out the ‘action’ and the ‘category’ They are unneeded.
  17. Change ‘MainActivity’ to the ‘Accelerometer’
  18. Also open up the Menu.java and change one of the examples to the same as the Java for your accelerometer.
  19. Export the app and see what your phone reads! If laying on a table, x and y should both read about 0, not absolutely zero because it will read the tiniest vibration. z will read the pull on the phone, aka, gravity, so it should read around 9.8. All of the readings will be in meters per second.

Android 1: Lesson 10

Android 1: Lesson 10: Accelerometer Part 1



INTRODUCTION
In this lesson you will be getting access to your accelerometer. And being able to read the x, y, and z values that it gives it.
LESSON OBJECTIVES
By the end of this lesson, you will be able to:
1. Access your accelerometer
2. Be able to read what the accelerometer is reading
3. Be able to set up a Sensor and SensorManager Activity
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
  1. We’ll briefly hit the points for the XML layout in this one, you should be good with TextViews.
  2. Create a new XML document. We called it ‘accel’
  3. If it isn’t in ‘RelativeLayout’, then change the ‘LinearLayout’.
  4. Change the width and height to ‘match_parent’, and add ‘android:id=”@+id/relative” ‘ at the bottom.
  5. We’ll have 4 TextViews in this app. Make the first of them a slightly bigger text size than the others, because it will be the title. Name them like we do.
    1. ‘android:id=”@+id/name” ‘
    2. ‘android:id=”@+id/xval” ‘
    3. ‘android:id=”@+id/yval” ‘
    4. ‘android:id=”@+id/zval” ‘
  6. Make the width ‘fill_parent’ and the height ‘wrap_content’. Also, add
    2.‘android:layout_below=”@+id/name” ‘
    3.‘android:layout_below=”@+id/xval” ‘
    4.‘android:layout_below=”@+id/yval” ‘
    making them show up in that order
  7. Now for the Java. Start a new class, name it Accelerometer. Type ‘extends Activity implements SensorEventListener’ within the brackets. Import the Activity and SensorEventListener. And add the unimplemented methods of the Accelerometer.
  8. Now after the brackets, type ‘private SensorMangager mSensorManager;’
  9. Enter, ‘private Sensor mAccelerometer;
  10. Enter, ‘TextView title,tv, tv1,tv2;’
  11. Enter, ‘RelativeLayout layout;’
  12. Import ‘onCreate(Bundle)’ like you did in lesson 9.
  13. Below the ‘super.onCreate’ type, ‘setContentView(R.layout.accel);’
  14. Enter ‘mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  15. Enter ‘mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);’

Android 1: Lesson 9

Android 1: Lesson 9: List Menu


INTRODUCTION
In this lesson you will be creating a Menu that will list all of your activities in your app and allows you to pick which one to start
LESSON OBJECTIVES
By the end of this lesson, you will be able to:
1. Create a List Menu
2. To expand your app with more Activities
3. Know more about 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
  1. Create a new class called ‘Menu’
  2. After Menu, type ‘extends ListActivity’, import it
  3. Below that line of text, right click and go to ‘source’ and go ‘override’ and you’ll see ‘ListActivity’ which was imported. Select ‘onCreate(Bundle)’ and hit ok.
  4. go below the ‘}’ and do the same as above, but select ‘onListItemClick’
  5. Inbetween the public class at top, and the override, Type out ‘String classes[] = { “MainActivity”, “example1”, “example2”};’ You can put as many examples as you want. This is what the Menu will actually show. The names should be the names of the classes
  6. Below the first override, under the super type ‘setListAdapter(new ArrayAdapter(Men.this, android.R.layout.));’ after the ‘layout.’ you’ll want to pick the ‘simple_list_item_1’ this will put the code in automatically for you. After that type’, classes’
  7. Ok now move below the second override. Type ‘String xxx = classes[postion];’ xxx can be anything you want. It’s just making it easier for you in the code to follow.
  8. Enter, ‘try{‘
  9. Enter ‘Class ourClass =  Class.forName(“xxx” + easy); xxx should be your package, make sure there is a period after the package and before the final “
  10. Enter ‘Intent ourIntent = new Intent(Menu.this, ourClass);’ import the intent
  11. Enter ‘startActivity(ourIntent);’
  12. After the ‘}’ right below the code you just typed, type ‘catch (ClassNotFoundException e){‘
  13. Enter ‘e.printStackTrace();’
  14. Now open up your ‘AndroidManifest.xml’, you’ll need to add the menu to it.
  15. Copy the MainActivity activity, and paste it above
  16. Change MainActivity to the name of your Menu class.
  17. Also change ‘.STARTINGPOINT’ to ‘.MENU’ save and export
  18. Congrats! you made a menu! export the app and test it!