I'm new on Java and Android programming language and this is the first platform that I studied.
I want to ask, how can I access the event.values[1] in this method?
public void onSensorChanged(SensorEvent event) {synchronized (this){
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
mAccVals.x = (float) (event.values[1] * FILTERING_FACTOR + mAccVals.x * (1.0 - FILTERING_FACTOR));
//mAccVals.y = (float) ((-event.values[1] * FILTERING_FACTOR + mAccVals.y * (1.0 - FILTERING_FACTOR)));
mAccVals.z = (float) (event.values[2] * FILTERING_FACTOR + mAccVals.z * (1.0 - FILTERING_FACTOR));
scene.camera().position.x = mAccVals.x * .2f;
scene.camera().position.z = mAccVals.z * .8f;
scene.camera().target.x = -scene.camera().position.x;
scene.camera().target.z = -scene.camera().position.z;
}
I want to get event.values[1] and then display it on a textview
protected void onCreateSetContentView()
{
setContentView(R.layout.custom_layout_example);
LinearLayout ll = (LinearLayout) this.findViewById(R.id.scene1Holder);
ll.addView(_glSurfaceView);
TextView myTextView = (TextView) findViewById(R.id.splashTitle);
myTextView.setText("Test " + event.values[1] );
return;
}
There's any suggestion how can I solve this problem?
Thanks in advance
Create a field in your Activity and assign it to your R.id.splashTitle:
TextView mTextView;
...
protected void onCreateSetContentView() {
mTextView = (TextView) findViewById(R.id.splashTitle);
...
}
Then set it's text in onSensorChanged() as follows:
public void onSensorChanged(SensorEvent event) {
mTextView.setText("Test " + event.values[1]);
...
}
Or you may store most recent sensor's values in a field:
// Class field
float[] mSensorValues;
// In onSensorChanged()
mSensorValues = event.values;
// In onCreateSetContentView()
myTextView.setText("Test " + mSensorValues[1]);
To preserve right sequense of onCreate and onSensorChanged you may use an approach described in documentation:
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
Related
I successfully made an app to measure the ambient temperature in Android studio. But the next step doesn't want to work. here's the point:
when I start the app the temperature should be shown in 1 textView (works so far). Then it should stay with this temperature but it changes. I only want to check the temperature once.
when I click on submit it shall do the same with the second textView.
I did this successfully with the time but with the temperature, it won't work. And my second question is, I did a lot of research and it looks like the temperature will always be shown in °c. Can I change that so the people who want it in °F can have it this way?
Code for the temperature without change the value and with the button for getting the temperature on textView2:
MainActivity:
public class MainActivity extends AppCompatActivity implements SensorEventListener {
private TextView textView, textView2;
private Button button;
private SensorManager sensorManager;
private Sensor tempSensor;
private Boolean isTemperatureSensorAvailable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
textView2 = findViewById(R.id.textView2);
button = findViewById(R.id.button);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE) !=null) {
tempSensor = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
isTemperatureSensorAvailable = true;
}else{
textView.setText("Temperature Sensor is not available");
isTemperatureSensorAvailable = false;
}
}
#Override
public void onSensorChanged(SensorEvent event) {
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
#Override
protected void onResume(){
super.onResume();
if(isTemperatureSensorAvailable){
sensorManager.registerListener(this,tempSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
#Override
protected void onPause(){
super.onPause();
if(isTemperatureSensorAvailable){
sensorManager.unregisterListener(this);
}
}
private void update_text_unit()//Run from button
{
boolean toogle = false;
float temperature = 0;
float C_temp = 0;
float F_temp =0;
//Get sensor data
Sensor temp = sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
F_temp = (C_temp * (9/5) +32);//Convert to °F
if(toogle = true) {
textView2.setText("Temp is:" + C_temp + "°C");
toogle = false;
}
else {
textView2.setText("Temp is:" + F_temp + "°F");
toogle = true;
}
}
}
I hope someone can help me going into a better direction.
Thanks for your help.
In the first code you have a section which is on sensor changed so this will run each time causing your issue. In you oncreate all the API to get the temperature once and chuck it to the text view as a string. For °C to °F you will need to convert. I would just do this manually:
(X°C × 9/5) + 32 = result_°F
Where X is the input temperature and result is the output in °F.
Here is an example of the button temp change feature not exactly sure how you read the temp so the line of Sensor.gettemp will probably need to change. Run this from a button push, it will update your text view
private void update_text_unit()//Run from button
{
boolean toogle = false;
float temperature = 0;
float C_temp = 0;
float F_temp =0;
//Get sensor data
C_temp = Sensor.gettemp();
F_temp = (C_temp * (9/5) +32);//Convert to °F
if(toogle = true) {
yourtextview.setText("Temp is:" + C_temp + "°C");
toogle = false;
}
else {
yourtextview.setText("Temp is:" + F_temp + "°F");
toogle = true;
}
}
I want the app to vibrate accordingly to the light value detected. For example, when the value is higher then the vibration is stronger. But this code seems not working, anyone can help to see where is my mistake? It can detect the light value correctly and the vibration is working but the vibration is not vibrating according to the light value.
public class LightDetection extends AppCompatActivity {
TextView textLight, textOn, textOff;
Button btnStart, btnStop;
float x;
long ambientValue = 16;
long floor_delay = 500;
long ceiling_delay = 80;
long vibrate = 100;
long vibrate_delay;
Vibrator sensorVibrator;
SensorManager sensorManager;
Sensor sensor;
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_light_detection);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
sensorVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
textLight = (TextView) findViewById(R.id.textView);
textOn = (TextView) findViewById(R.id.textView2);
textOff = (TextView) findViewById(R.id.textView3);
btnStart = (Button) findViewById(R.id.button3);
btnStop = (Button) findViewById(R.id.button4);
final SensorEventListener lightListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int acc) {
}
#SuppressLint("SetTextI18n")
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
Log.i("IE LightDetect.", "Light value: " + x);
textLight.setText((int) x + " lux");
}
};
btnStart.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
public void onClick(View view) {
sensorManager.registerListener(lightListener, sensor,
SensorManager.SENSOR_DELAY_FASTEST);
handler.postDelayed(runnable, vibrate_delay);
textOn.setText("LIGHT DETECTION MODE: ON");
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
public void onClick(View view) {
textOn.setText("LIGHT DETECTION MODE: OFF");
sensorManager.unregisterListener(lightListener);
sensorVibrator.cancel();
handler.removeCallbacks(runnable);
}
});
}
private Runnable runnable = new Runnable() {
#Override
public void run() {
/* do what you need to do */
long luxValue = (long) x;
//Correction Ratio - Ceiling Delay/Most Likely Max Lux
long ratio = 950 / 300;
if (luxValue < ambientValue) {
vibrate = 100;
vibrate_delay = floor_delay + (ratio * luxValue);
} else {
vibrate = 100;
vibrate_delay = floor_delay - (ratio * luxValue);
}
if (vibrate_delay < ceiling_delay) {
long[] pattern = {0, vibrate, ceiling_delay};
sensorVibrator.vibrate(pattern, 1);
} else {
long[] pattern = {0, vibrate, vibrate_delay};
sensorVibrator.vibrate(pattern, 1);
}
Log.i("IE LightDetect.", "Lux: " + luxValue);
handler.postDelayed(this, vibrate_delay);
}
};
}`
After getting the light value in onSensorChanged() you are not saving the value globally, there u r using float x = event.values[0] which is just creating a local variable inside the function, and not the global one..try ditching the float from there, use x = event.values[0] instead of float x = event.values[0]..the it will save the value in the global variable and that you will use where you calculating vibration..
I made an app about android water drinking monitor. But it does the wrong calculation as a percentage. What do you think I might have done wrong.
(When the progress bar is 100 at 100, the daily water calculation is wrong)
waterml = sharedPreferences.getInt(WATER_ML, 0);
waterdata = sharedPreferences.getInt(WATER_DATA, 0);
int dailywater = 2838; // example
ml100.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//scale 100 ml
int percent = 100 * 100 / dailywater;
ProgressBarAnimation anim = new ProgressBarAnimation(circlebar, waterdata, (waterdata + percent));
anim.setDuration(300);
circlebar.startAnimation(anim);
dailywater.setText((waterml + 100) + " / " + dailywater + " ml");
editor = sharedPreferences.edit();
editor.putInt(WATER_DATA, (waterdata + percent));
editor.putInt(WATER_ML, (waterml + 100));
editor.apply();
}
});
////
public static class ProgressBarAnimation extends Animation {
private CircleProgressBar progressBar;
private float from;
private float to;
ProgressBarAnimation(CircleProgressBar progressBar, float from, float to) {
super();
this.progressBar = progressBar;
this.from = from;
this.to = to;
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
float value = from + (to - from) * interpolatedTime;
progressBar.setProgress((int) value);
}
}
It seems to me like your code, namely editor.apply(); is working as follows:
you are trying to save the progress and it occurs that sometimes it is called multiple times in a row. According to documentation if you are sending several apply() or commit() commands only the last one will be executed. Therefore, some clicks might not be processed.
I suggest you simply changing your variable for progress and saving it later:
waterml = sharedPreferences.getInt(WATER_ML, 0);
waterdata = sharedPreferences.getInt(WATER_DATA, 0);
int dailywater = 2838; // example
ml100.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//scale 100 ml
int percent = 100 * 100 / dailywater;
ProgressBarAnimation anim = new ProgressBarAnimation(circlebar, waterdata, (waterdata + percent));
anim.setDuration(300);
circlebar.startAnimation(anim);
dailywater.setText((waterml + 100) + " / " + dailywater + " ml");
waterdata += percent;
waterml += 100;
}
});
and then in your onPause() function you simply save them:
public void onPause() {
editor = sharedPreferences.edit();
editor.putInt(WATER_DATA, waterdata);
editor.putInt(WATER_ML, waterml);
editor.apply();
}
If you want your percent not to be rounded, you could change putInt to putFloat and getInt to getFloat
I'm trying to get data from my phone's gyros.
So far,the angles keep changing depends on the angle that i move my phone.
However, the azimuth angle for example, i want it to start counting from the position i started and not from the north direction --> i want it to start from 0.
my question is: how can i save the first data i get from the gyro so i could do "minus" from the rest of the data when i move the phone?
The code looks like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
rotationvector=sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
sm.registerListener(this,rotationvector,SensorManager.SENSOR_DELAY_NORMAL);
rotationx=(TextView)findViewById(R.id.rotationx);
rotationy=(TextView)findViewById(R.id.rotationy);
rotationz=(TextView)findViewById(R.id.rotationz);
}
#Override
public void onSensorChanged(SensorEvent event) {
rotationx.setText(" " +event.values[0]);
rotationy.setText(" " +event.values[1]);
rotationz.setText(" " +event.values[2]);
}
The thing i want to do is like:
rotationx.setText(" " +event.values[0]-FirstAzimuthValue);
Thanks!
new code:
private boolean isFirstMeasure = true;
private float FirstAzimuthValue = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
rotationvector=sm.getDefaultSensor(Sensor.TYPE_ORIENTATION);
sm.registerListener(this,rotationvector,SensorManager.SENSOR_DELAY_NORMAL);
rotationx=(TextView)findViewById(R.id.rotationx);
rotationy=(TextView)findViewById(R.id.rotationy);
rotationz=(TextView)findViewById(R.id.rotationz);
}
#Override
public void onSensorChanged(SensorEvent event) {
if (isFirstMeasure){
FirstAzimuthValue - event.values[0]; //error this line
isFirstMeasure = false;
}
rotationx.setText(" " +(event.values[0] - FirstAzimuthValue));
rotationy.setText(" " +event.values[1]);
rotationz.setText(" " +event.values[2]);
}
Why not just
private boolean isFirstMeasure = true;
private float firstAzimuthValue = 0;
// ...
#Override
public void onSensorChanged(SensorEvent event) {
if (isFirstMeasure) {
firstAzimuthValue = event.values[0];
isFirstMeasure = false;
}
rotationx.setText(" " + (event.values[0] - firstAzimuthValue));
rotationy.setText(" " +event.values[1]);
rotationz.setText(" " +event.values[2]);
}
The code works fine but the seekbar value is not fetched in my code. The error shown is "Cannot resolve symbol progress" even after making it public. Someone throw light on this possibly with short explanation. I have stored the SeekBar value in progress and tried to use it in calculation further.
sb = (SeekBar)findViewById(R.id.set_years);
yrs = (TextView)findViewById(R.id.years);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public int progress;
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
progress = i;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
yrs.setText(progress+" year(s)");
}
});
addButtonClickListener();
}
private void addButtonClickListener() {
submit = (Button)findViewById(R.id.submit);
submit.setOnClickListener(this);
}
#Override
public void onClick(View view) {
float loanAmtValue = 0;
float roiValue = 0;
double answer;
loanAmt = (EditText)findViewById(R.id.amt);
loanAmtValue = Float.parseFloat(loanAmt.getText().toString());
roi = (EditText)findViewById(R.id.roi);
roiValue = Float.parseFloat(roi.getText().toString());
if (loanAmtValue != 0 || roiValue != 0){
answer = (loanAmtValue * roiValue * progress) / 100;
siResult = (TextView) findViewById(R.id.result);
siResult.setText("Calculated Simple Interest for Amount Rs" + loanAmtValue + " and ROI " + roiValue + "% is = " + Double.toString(answer));
loanAmt.setText("0");
roi.setText("0");
}
else
{
siResult.setText("Please provide valid details");
}
First of all - your progress variable is member of SeekBar.OnSeekBarChangeListener anonymous inner class. You cannot access it directly without having reference to instance of this listener.
Second of all - you do not need to store the progress by yourself. Use sb.getProgress();.
Check your imports for the error "Cannot resolve symbol progress".
and check too, if you define the max for seekbar.