layout inflater - Updating Android UI from custom view -


i'm new android development, , still having difficulties comprehending how framework works. try write simple game experience , i'd ask below. in main activity, have few textviews show various data game, , under have custom view called animatedview has canvas draw objects game itself. based on objects on canvas, i'd update textviews show e.g. number of object, level player at, etc.

the problem is, though can textview object below code, when call textview.settext(...), nothing changes on ui.

the below simplified version of real code, demonstrate problem:

activity_main.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     ...     tools:context="com.danielsh.test.teststuff.mainactivity"     android:orientation="vertical">      <textview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/mytextview"         android:text="change this!" />      <com.danielsh.test.teststuff.animatedview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/animview" />  </linearlayout> 

mainactivity.java

public class mainactivity extends appcompatactivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);     } } 

animatedview.java

public class animatedview extends view {     public animatedview(context context, attributeset attrs) {         super(context, attrs);          final handler uihandler = new handler();         final runnable runnable = new runnable() {             public void run() {                 layoutinflater inflater = layoutinflater.from(getcontext());                 linearlayout layout = (linearlayout) inflater.inflate(r.layout.activity_main, null, false);                 textview textview = (textview) layout.findviewbyid(r.id.mytextview);                 textview.settext("" + (system.currenttimemillis() / 1000));                 textview.invalidate();             }         };          uihandler.postdelayed(runnable, 1000);     } } 

i assume i'm not working actual textview object that's visible on ui, not sure what'd correct way of doing that.

any or pointer existing post deals same problem appreciated.

it's not clear me, you're trying animatedview, won't able give real solution on how working correctly, on what's issue here. if full implementation, should maybe try implement behavior in activity itself.

in posted runnable inflate layout xml. means create new layouts , views , assign these local variable. these views not part of layout hierarchy displayed in activity. when update text of textview you'll never see changes anywhere.

as sidenote: use postdelayed(runnable, long) instead of creating new thread.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -