title: How to use accelerometer to press a button in android?
body: <p>I am trying to use accelerometer to press a button in android studio for my dice app. Does anyone know how to hook it up to a button, so that when I shake my phone the dice will roll? I've tried to use SensorEventListener but couldn't get it to work. Thanks for any feedback/suggestion.
  PS. I'm a noob.</p>
  
  <p>Here is my java code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      Button rollButton;
      rollButton = findViewById(R.id.rollButton);
  
      final ImageView leftDice = findViewById(R.id.diceLeft);
      final ImageView rightDice = findViewById(R.id.diceRight);
      final ImageView topDice = findViewById(R.id.diceTop);
  
      final int [] diceArray = {
              R.drawable.perspective_dice_one,
              R.drawable.perspective_dice_two,
              R.drawable.perspective_dice_three,
              R.drawable.perspective_dice_four,
              R.drawable.perspective_dice_five,
              R.drawable.perspective_dice_six,
      };
  
      rollButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              Log.d("Dice", "You pressed the button!");
  
              Random randomNumberGenerator = new Random();
  
              int number = randomNumberGenerator.nextInt(6);
              Log.d("Dice", "The random number is: " + number);
  
              leftDice.setImageResource(diceArray[number]);
  
              number = randomNumberGenerator.nextInt(6);
              rightDice.setImageResource(diceArray[number]);
  
              number = randomNumberGenerator.nextInt(6);
              topDice.setImageResource(diceArray[number]);
  
  
          }
      });
  
  
  
  }
  </code></pre>
  
  <p>}</p>