xml - How to improve interaction between Android Studio class and Processing sketch -


i have created app in there processing sketch being displayed on mainactivity :

package app.yeah.app;          import android.app.fragment;         import android.app.fragmentmanager;         import android.support.v7.app.actionbaractivity;         import android.os.bundle;         import android.view.window;         import android.widget.textview;          public class mainactivity extends actionbaractivity {              textview t1 = (textview)findviewbyid(r.id.t1);             //             fragment fragment;             fragmentmanager fragmentmanager;              //             @override             protected void oncreate(bundle savedinstancestate) {                 super.oncreate(savedinstancestate);                 this.supportrequestwindowfeature(window.feature_no_title);                 setcontentview(r.layout.activity_main);                  fragmentmanager = getfragmentmanager();                 fragment = new procmenu();                 fragmentmanager.begintransaction()                         .replace(r.id.frag, fragment).commit();             }         } 

by using fragment, display processing sketch:

package app.yeah.app;  import android.media.mediaplayer; import processing.core.papplet; import processing.core.pvector;  public class procmenu extends papplet {     pvector cur;     float r;     public void setup()     {         //size(240, 400);         smooth();         r = width / 20;     }     public void draw()     {         background(0);         cur = new pvector(mousex, mousey);          fill(255, 0, 200, 200);         nostroke();         ellipse(cur.x, cur.y, r, r);          fill(255, 200, 0);         text("x: " + cur.x + ", y: " + cur.y, r, r);     } } 

the sketch displays coordinates, that's great but, want able access such variables in mainactivity displaying on textview t1. in variables aren't updating, don't know how it. textview on layout:

<relativelayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".mainactivity">      <framelayout android:id="@+id/frag"         android:layout_width="match_parent"         android:layout_height="match_parent"         >     </framelayout>      <linearlayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical"         >         <textview             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:id="@+id/t1"             android:layout_gravity="center_horizontal" />     </linearlayout> </relativelayout> 

thanks lot.

you need pass variables procmenu class mainactivity class. there 2 ways this:

approach 1: create function in procmenu class returns coordinates, , call function mainactivity class whenever want update.

public class mainactivity extends actionbaractivity {     textview t1 = (textview)findviewbyid(r.id.t1);    procmenu fragment;     public void updatetext(){       t1.settext(fragment.getcur().x + ", " + framgent.getcur().y);    } }  public class procmenu extends papplet{     pvector cur;      public pvector getcur(){        return cur;     } } 

approach 2: create set function in mainactivity class, pass instance of mainactivity procmenu class. call setter whenever want update.

public class mainactivity extends actionbaractivity {     textview t1 = (textview)findviewbyid(r.id.t1);    procmenu fragment = new procmenu(this);     public void updatetext(pvecotr cur){       t1.settext(cur.x + ", " + cur.y);    } }  public class procmenu extends papplet{     pvector cur;     mainactivity mainactivity;      public procmenu(mainactivity mainactivity){        this.mainactivity = mainactivity;     }      public pvector updatecur(){        mainactivity.updatetext(cur);     } } 

which approach take entirely you.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -