Friday, 15 February 2013

Android: Constantly updating TextView with a new value -



Android: Constantly updating TextView with a new value -

i'm new java , android , need suggestions how can update textview located in xml value generated through code.

the programme draws straight line through canvas, want textview reflect line's tip x-value.

appreciate help!

my code follow:

package com.example.threadexperiment1; import android.app.activity; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.os.bundle; import android.view.view; import android.widget.relativelayout; import android.widget.textview; public class mainactivity extends activity { linedrawing infiniteline; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); infiniteline = new linedrawing (this); setcontentview(r.layout.activity_main); thread thread = new thread(infiniteline); thread.start(); relativelayout rl1 = (relativelayout)findviewbyid(r.id.relativelayout1); rl1.addview(infiniteline); } public class linedrawing extends view implements runnable { float x=100, y=100; paint linecolour = new paint (); textview tv1 = (textview)findviewbyid(r.id.textview1); //constructor public linedrawing(context context) { super(context); linecolour.setcolor(color.black); } //ondraw codes @override public void ondraw (canvas canvas){ canvas.drawline(0,0,x,y,linecolour); if (x==500){ x=0; y=0; } invalidate(); } @override public void run() { while (!thread.currentthread().isinterrupted()) { seek { thread.sleep(250); } grab (exception e) {} x+=10; y+=10; } } } }

try textview.settext(string) within thread.

textview

No comments:

Post a Comment