How do you forward UI values from Views/Menus to OF?

Intro
I’m currently developing an OF app and want to use the menu/dialog system of Android to adjust some settings of the application. These settings should be forwarded to OF (C++).

Implementation
For now I have implemented a Menu and View for the popup Dialog in XML. When the users presses the menu button, it will show the menubar:

After selecting one of the example options, it will open up a new dialog (on top of the current OF Activity):

In the Java part (OFActivity.java) I have the following method that is called when the dialog is created:

  
private AlertDialog getDialog() {  
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);   
        LayoutInflater inflater = getLayoutInflater();  
          
        final View myView = inflater.inflate(R.layout.settings, null);  
                  
        final SeekBar ExampleSeekBar = (SeekBar)myView.findViewById(R.id.ExampleSeekBar);          
        ExampleSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){  
        	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {  
    		   // TODO Store/set values  
    		   Log.d("OFUI", "ExampleSeekBar: " + seekBar.getProgress());  
    	   }  
  
    	   public void onStartTrackingTouch(SeekBar ExampleSeekBar) { }  
    	   public void onStopTrackingTouch(SeekBar seekBar) { }  
   	    });        	     
          
        final CheckBox ExampleCheckbox = (CheckBox)myView.findViewById(R.id.ExampleCheckbox);    
          
        builder.setTitle("Settings")  
    		   .setView(myView)  
    		   .setPositiveButton("OK", new DialogInterface.OnClickListener() {  
				    public void onClick(DialogInterface dialog, int id) {				    	  
				    	Log.d("OFUI", "getDialog - OK");  
				    }  
				})  
				.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {  
					public void onClick(DialogInterface dialog, int id) {  
						Log.d("OFUI", "getDialog - Cancel");  
						dialog.cancel();  
					}  
				});  
          
        AlertDialog alert = builder.create();          
        return alert;          
    }  

This code uses the LayoutInflater to load the view made in XML. It also binds a SeekBarChangeListener and outputs the changes in realtime to logcat.

The question
Instead of getting the values in logcat, I would like to use these values directly in the OF app. How would I do this? I have checked the methods ofxAndroidApp but none of them seem to be useful for passing values from the UI to the C++ app. menuItemSelected for example only tells me which menu item is selected (which is not useful in my case)

The only thing I can think of would be adding additional/custom JNI methods. But before doing that, I would like to know if there is another method I can use instead.

Full sourcecode menu demo
http://falcon4ever.pcsx2.net/androidEmptyExampleMenus.7z

Yes, anything more complex than the menu or a checkbox needs custom jni

Any pointers on which files I need to look at to add those functions?

to call from java to c++ in the java class (for example in OFActivity) create a method like:

  
public static native void sliderChanged(float v);  

then call it whenever the slider changes

int the c++ side create a function like:

  
  
#include <jni.h>  
extern "C"{  
void Java_cc_openframeworks_OFActivity_sliderChanged(JNIEnv*  env, jclass  thiz, jfloat value){  
...  
}  
  

whenever you call the native method in java you are actually calling the function in c

Thanks that worked!

just a slight fix for people who are trying to implement it, change:

  
Java_cc_openframeworks_OFActivity_sliderChanged  

To (You need to include your project name in the method name):

  
Java_cc_openframeworks_androidEmptyExample_OFActivity_sliderChanged  

Hello ,
sorry to extract a so old topic, but i don’t understand how to use that in oF, it’s two day i try to understand.
I understood JNI is used for use native code ( so c++ in our case) into java.
But how can we use it for send data from java to c++ ?
I tried different solution, and none works.
I think i miss an obvious point but i can’t find which one.
I want to use jni for call different part of my app since the android menu, or for transfert value like an ip which will be set by the user.
Is it possible to see an example ?

1 Like