Friday, June 21, 2013

Android 1: Lesson 10

Android 1: Lesson 10: Accelerometer Part 1

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

Required Reading
Read the following:
Introduction To Development Environment
·        Eclipse
·        Android
·        Considerations for beginning programming.
View the following:
1.      Eclipse
2.     Android Development
  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);’