title: How to pass C++ callback as a click handler of a Button in android?
body: <p>I am building an android app and I need to create UI elements dynamically from JNI part and assign C++ functions as click handler.</p>
  
  <p>I have defined a java function to create a button and return it.
  I have a C++ function that calls the Java function and has jobject of button. Now I want to assign another C++ function as click handler to this object.</p>
  
  <p>Java:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  ...
      public View control(String text) {
          Button bt = new Button(this);
          bt.setText(text);
          ((ConstraintLayout)findViewById(R.id.layout_main)).addView(bt);
          return bt;
      }
  }
  </code></pre>
  
  <p>C++:</p>
  
  <pre><code>extern "C" JNIEXPORT void JNICALL Java_com_shaidin_ebellum_MainActivity_start(JNIEnv *env, jobject me)
  {
      jmethodID jControl = env-&gt;GetMethodID(env-&gt;GetObjectClass(me), "control", "(Ljava/lang/String;)Landroid/view/View;");
      jstring jText = env-&gt;NewStringUTF("test");
      jobject jView = env-&gt;CallObjectMethod(me, jControl, jText);
  
      // How to add listener here?
  
      env-&gt;DeleteLocalRef(jView_);
      env-&gt;DeleteLocalRef(jText);
  }
  </code></pre>
  
  <p>In this question
  <a href="https://stackoverflow.com/questions/6227086/android-ui-in-c/6305497">Android UI in C++</a>
  JNIpp library
  <a href="https://github.com/DmitrySkiba/itoa-jnipp" rel="nofollow noreferrer">https://github.com/DmitrySkiba/itoa-jnipp</a>
  has introduced and it does exactly what I want, but I want to implement it myself because I don't want to add that much code to my project. Also that library has a complicated implementation using nested macros that makes it difficult to learn how it works.</p>