I am using graphview library in my android application to plot real time data coming from a sensor. I already have the graph, but I want to display the current value plotted in the graph also in a textview.
Here is the code I used for plotting the data in the graphview. Here, as an example, I generate and simulate 100 random values. But it's the same principle. And those random values shall be displayed in the same time they are added (appendData()) in the graph.
@Override
protected void onResume() {
super.onResume();
// we're going to simulate real time with thread that append data to the graph
new Thread(new Runnable() {
@Override
public void run() {
// we add 100 new entries
for (int i = 0; i < 100; i++) {
runOnUiThread(new Runnable() {
@Override
public void run() {
addEntry();
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}).start();
}
private void addEntry() {
series.appendData(new DataPoint(lastX++, RANDOM.nextDouble() * 100d), true, 100);
}
Or is there another way than using a textview element?
Thanks in Advance!
You should create a TextView in your layout with an identifier using the xml property "id". i.e
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewID"
/>
After that you take the reference in your code using findview function.
TextView lastValue = (TextView) findViewById(R.id.textViewID);
When you have this reference you can change the value that it shows using setText function.
lastValue.setText(RANDOM.nextDouble());
You can try to do it after your appendData function. If you can call the static method directly inside addEntry function you can pass your view as parameter and call findview from the View object and it should work.