Project description: I will change image drawable in the
ImageView
and update TextView
when user tilt device.First, set layout for our
Activity
:
activity_sensor.xml
In programmatically code, have now come to the point, that we should get the accelerometer events, and use them. We are going to get the x,y events, by using the <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/img"
android:gravity="center"
android:layout_marginTop="10dp"
android:textColor="@android:color/holo_green_dark"
android:textSize="20sp" />
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="@string/action_settings"
android:src="@drawable/center" />
</RelativeLayout>
event.values[0]
, event.values[1]
accordingly. When we tilt device, these variables value will be changed, and processing this action in onSensorChanged()
method:
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
if (Math.abs(x) > Math.abs(y)) {
if (x < 0) {
image.setImageResource(R.drawable.right);
textView.setText("You tilt the device right");
}
if (x > 0) {
image.setImageResource(R.drawable.left);
textView.setText("You tilt the device left");
}
} else {
if (y < 0) {
image.setImageResource(R.drawable.up);
textView.setText("You tilt the device up");
}
if (y > 0) {
image.setImageResource(R.drawable.down);
textView.setText("You tilt the device down");
}
}
if (x > (-2) && x < (2) && y > (-2) && y < (2)) {
image.setImageResource(R.drawable.center);
textView.setText("Not tilt device");
}
}
Generally, we do not want these kind of listeners to run to our device continually, because most of the times they appear to be energy and cpu consuming, so we are going to unregister our listener in onPause()
of our Activity
, and then register them back in onResume()
. So, we have to add this code:
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
//unregister Sensor listener
sensorManager.unregisterListener(this);
}
And full SensorActivity.java code:
SensorActivity.java
Some application screenshots (click for full size):package com.blogspot.hongthaiit.sensor;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private Sensor sensor;
private TextView textView;
private ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor);
//declaring Sensor Manager and sensor type
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//locate views
textView = (TextView) findViewById(R.id.txt);
image = (ImageView) findViewById(R.id.img);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
if (Math.abs(x) > Math.abs(y)) {
if (x < 0) {
image.setImageResource(R.drawable.right);
textView.setText("You tilt the device right");
}
if (x > 0) {
image.setImageResource(R.drawable.left);
textView.setText("You tilt the device left");
}
} else {
if (y < 0) {
image.setImageResource(R.drawable.up);
textView.setText("You tilt the device up");
}
if (y > 0) {
image.setImageResource(R.drawable.down);
textView.setText("You tilt the device down");
}
}
if (x > (-2) && x < (2) && y > (-2) && y < (2)) {
image.setImageResource(R.drawable.center);
textView.setText("Not tilt device");
}
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
//unregister Sensor listener
sensorManager.unregisterListener(this);
}
}
(sorry for ads at download link)