Reputation: 2159
I have an accelerometer app I am developing for Android, and I want to start and stop the activity with a "start" and "stop" button. I am having trouble implementing the buttons in the code itself and I am looking for some help. Here is my main java file.
package com.*****.accelo;
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.TextView;
import android.widget.Button;
import android.view.View.OnClickListener;
public abstract class AcceloActivity extends Activity implements SensorEventListener,
OnClickListener {
TextView textViewX, textViewY, textViewZ, textViewError;
String blank;
Button buttonStop, buttonStart;
StringBuilder builderx = new StringBuilder();
StringBuilder buildery = new StringBuilder();
StringBuilder builderz = new StringBuilder();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textViewError = (TextView) findViewById(R.id.textViewError);
textViewX = (TextView) findViewById(R.id.textViewX);
textViewY = (TextView) findViewById(R.id.textViewY);
textViewZ = (TextView) findViewById(R.id.textViewZ);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
//Button listener
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener( this);
SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor accelerometer = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if(!manager.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_UI)){
}
textViewX.setText(blank);
textViewY.setText(blank);
textViewZ.setText(blank);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
builderx.setLength(0);
buildery.setLength(0);
builderz.setLength(0);
builderx.append("X: " + event.values[0]);
buildery.append("Y: "+ event.values[1] );
builderz.append("Z: " + event.values[2]);
textViewX.setText(builderx.toString());
textViewY.setText(buildery.toString());
textViewZ.setText(builderz.toString());
textViewError.setText(blank);
}
Thanks in advance!
Upvotes: 0
Views: 1207
Reputation: 2343
You get a reference to the buttons from the resources and then never do anything with them. What you do you mean by "having some trouble implementing"? If you're having a specific problem or are encountering an error please add that information to the question so we can help you. If you just don't know how to use buttons in Android there are a ton of good sites online but you should probably start here http://developer.android.com/reference/android/widget/Button.html.
[Edits]
Since this is your first language and first attempt to get something like this working let's take a look at a few simple steps to begin with and then you can expand from there.
I assume Googling for an answer is nearly reflexive at this point but here are a few good places to do some reading:
In general what you'll need to do is override the onTouchEvent for your buttons http://developer.android.com/guide/topics/ui/ui-events.html so you can handle the user input and then record/compare the values you're interested in. From what you've mentioned you want to do I'm not sure that extending the SensorEventListener is really what you want. If you're just trying to capture the change from start to stop of your button clicks you can just handle the button events and record/analyze/display the data without worrying about the individual sensor updates. If, on the other hand, you actually want to display "live" sensor data between Start and Stop then you likely will need the SensorEventListener.
Go through the Google tutorials and API guides, check out as many examples as you can find, and you should be on your way. Good luck with your project.
Upvotes: 1