I'm trying to write an Android program in eclipse. The idea of the program is to display a message in TextView
saying "The device has been flipped" if the device is rotated exactly 180 degrees (in the xy plane, I think?). I'm using the rotation sensor and trying to write my code in the onSensorChanged
event. Currently this code is supposed to change a TextView when any rotation is detected, however it does not.
So my questions in simple are:
- How do I get the textview to change given any rotation?
- How do I then apply this to a rotation of 180 degrees?
package com.example.rotation2;
import android.hardware.*;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener {
TextView message;
private SensorManager mSensorManager;
private Sensor rotation;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView message = ((TextView) findViewById(R.id.message_view));
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
rotation = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
if (rotation != null){
// Success! There's a rotation sensor.
message.setText(R.string.compatible);
}
else {
// Failure! No rotation sensor.
message.setText(R.string.incompatible);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
@Override
public void onSensorChanged(SensorEvent event) {
message.setText(R.string.rotated);
}
}