title: Moving an image using Accelerometer of android
body: <p>I have read articles/tutorial about accessing the phone's accelerometer (acceleration and orientation) values. I am trying to build a simple app where I can move a ball image using the these values. Here is my code:</p>
  
  <pre><code>import android.app.Activity;
  import android.content.Context;
  import android.graphics.Canvas;
  import android.graphics.drawable.ShapeDrawable;
  import android.graphics.drawable.shapes.OvalShape;
  import android.hardware.Sensor;
  import android.hardware.SensorEvent;
  import android.hardware.SensorEventListener;
  import android.hardware.SensorManager;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.TextView;
  
  public class Accelerometer extends Activity implements SensorEventListener {
      /** Called when the activity is first created. */
       CustomDrawableView mCustomDrawableView = null; 
       ShapeDrawable mDrawable = new ShapeDrawable(); 
        int x ; 
         int y ;
  
      private SensorManager sensorManager = null;
  
         /** Called when the activity is first created. */
         @Override
         public void onCreate(Bundle savedInstanceState) {
  
             super.onCreate(savedInstanceState);
             // Get a reference to a SensorManager
             sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
             mCustomDrawableView = new CustomDrawableView(this); 
             setContentView(mCustomDrawableView); 
           //  setContentView(R.layout.main);
  
         }
  
         // This method will update the UI on new sensor events
         public void onSensorChanged(SensorEvent sensorEvent) {
           {
           if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
  
           int someNumber = 100;
           float xChange = someNumber * sensorEvent.values[1];
           //values[2] can be -90 to 90
           float yChange = someNumber * 2 * sensorEvent.values[2];       
               x = x + (int)xChange;
               y = y + (int)yChange;
  
           }
  
  
           if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
  
           }
          }
         }
  
         // I've chosen to not implement this method
         public void onAccuracyChanged(Sensor arg0, int arg1) {
       // TODO Auto-generated method stub
  
      }
  
         @Override
         protected void onResume() {
          super.onResume();
          // Register this class as a listener for the accelerometer sensor
          sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
          // ...and the orientation sensor
          sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
         }
  
         @Override
         protected void onStop() {
          // Unregister the listener
          sensorManager.unregisterListener(this);
          super.onStop();
         } 
         public  class CustomDrawableView extends View { 
  
             public CustomDrawableView(Context context) { 
                 super(context); 
  
                 int width = 50; 
                 int height = 50; 
                 mDrawable = new ShapeDrawable(new OvalShape()); 
                 mDrawable.getPaint().setColor(0xff74AC23); 
                 mDrawable.setBounds(x, y, x + width, y + height); 
             } 
             protected void onDraw(Canvas canvas) { 
                 mDrawable.draw(canvas); 
                 invalidate(); 
             } 
         }
  }
  </code></pre>
  
  <p>I am getting an oval shape displayed on the screen but nothing happens after that.</p>
  
  <p>thanks</p>