0% found this document useful (0 votes)
123 views4 pages

Android Firebase GraphView App

This document contains code for an Android application that displays real-time sensor data on a graph. It imports libraries for graphing, Firebase database access, and other functions. It defines classes and methods for connecting to the Firebase database, updating the graph with new sensor values over time, and allowing the user to enter a value to store in the database. The layout file defines UI elements including the graph, a text view, a button, and an edit text field.

Uploaded by

Adams uni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views4 pages

Android Firebase GraphView App

This document contains code for an Android application that displays real-time sensor data on a graph. It imports libraries for graphing, Firebase database access, and other functions. It defines classes and methods for connecting to the Firebase database, updating the graph with new sensor values over time, and allowing the user to enter a value to store in the database. The layout file defines UI elements including the graph, a text view, a button, and an edit text field.

Uploaded by

Adams uni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import android.app.

ProgressDialog;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.jjoe64.graphview.DefaultLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
GraphView graph;
LineGraphSeries<DataPoint> series;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("x");
DatabaseReference myRef2 = database.getReference("y");
//
Double inicio=0.0;
private final Handler mHandler = new Handler();
private Runnable mTimer;
//
double[] vector;
SimpleDateFormat sdf=new SimpleDateFormat("ss.SS", Locale.CANADA);
private double graphLastXValue =10;
EditText hello;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hello=(EditText)findViewById(R.id.escribe);
graph = (GraphView) findViewById(R.id.graph);
series = new LineGraphSeries<>();
graph.addSeries(series);
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMaxX(4);
graph.getViewport().setMinX(0);
graph.getViewport().setYAxisBoundsManual(true);
graph.getViewport().setMaxY(10);
graph.getViewport().setMinY(-10);
graph.setTitle("Aceleracion en X");
//series.setTitle("X");
//graph.getLegendRenderer().setVisible(true);
// graph.getLegendRenderer().setTextSize(30);
//graph.getLegendRenderer().setTextColor(Color.rgb(0,0,200));
//graph.getLegendRenderer().setFixedPosition(350,10);
series.setDrawDataPoints(true);
graph.addSeries(series);
//graph.getGridLabelRenderer().setLabelVerticalWidth(100);
/*graph.getGridLabelRenderer().setNumHorizontalLabels(2);
graph.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter(){

@Override
public String formatLabel(double value, boolean isValueX) {
if(isValueX) {
return sdf.format(new Date((long) value));
}else {
return super.formatLabel(value, isValueX);
}
}
});*/

// myRef.setValue(hola);
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// DataPoint[] datos=new DataPoint[(int)
dataSnapshot.getChildrenCount()];
vector=new double[(int) dataSnapshot.getChildrenCount()];
int index=0;
for(DataSnapshot mydataSnapshot:dataSnapshot.getChildren())
{
Double pointValue=mydataSnapshot.getValue(Double.class);
vector[index]=pointValue;
index++;
}
//
DataPoint[] datos=new DataPoint[vector.length];
for(int j=0;j<vector.length;j++) {
datos[j]= new DataPoint(j,vector[j]);
}
//series.resetData(datos);
//
inicio=vector[1];

}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});

public void onResume() {


super.onResume();

mTimer = new Runnable() {

@Override
public void run() {
graphLastXValue += 0.25d;
String segun=sdf.format(new Date());
Float segund=Float.parseFloat(segun);
series.appendData(new DataPoint(graphLastXValue,inicio), true, 22);
mHandler.postDelayed(this, 250);
}
};
mHandler.postDelayed(mTimer, 1500);
}

public void onPause() {


mHandler.removeCallbacks(mTimer);
super.onPause();
}

double mLastRandom = 2;
Random mRand = new Random();
private double getRandom() {
return mLastRandom += mRand.nextDouble()*0.5 - 0.25;
}

public void enviar(View view){


String hello2=hello.getText().toString().trim();
DatabaseReference usersRef = myRef.child("1");
usersRef.setValue(Double.parseDouble(hello2));
TextView texto=(TextView)findViewById(R.id.textView);
texto.setText(hello2);
}
}

/////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.jjoe64.graphview.GraphView
android:id="@+id/graph"
android:layout_width="365dp"
android:layout_height="254dp"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:layout_marginTop="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView"
android:layout_width="161dp"
android:layout_height="53dp"
android:layout_marginLeft="112dp"
android:layout_marginStart="112dp"
android:layout_marginTop="52dp"
android:textSize="30sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/graph" />

<Button
android:id="@+id/boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="230dp"
android:layout_marginStart="230dp"
android:layout_marginTop="39dp"
android:onClick="enviar"
android:text="enviar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<EditText
android:id="@+id/escribe"
android:layout_width="164dp"
android:layout_height="41dp"
android:layout_marginBottom="17dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:ems="10"
android:hint="numero"
android:inputType="textPersonName"
android:text="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

You might also like