I am currently trying to find out how to change the text of my textview in my MainActivity(the UI is written in Java) from a Python module. I know I have to pass my mainactivity somehow to my python module, but I still can't figure out how to do it properly. I'm using the Chaquopy SDK for python interaction.
My main activity looks like this right now
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //set the layout
final TextView simpleTextView = (TextView) findViewById(R.id.simpleTextView); //get the id for TextView
Button changeText = (Button) findViewById(R.id.btnChangeText); //get the id for button
changeText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
simpleTextView.setText("After Clicking"); //set the text after clicking button
}
});
}
}
Now I would need a function in python to be able to manipulate the textview.
Thanks in advance.
In summary, what you'll need to do is:
getModule
, and call the Python function using callAttr
(see the Java API documentation). Specifically, you could either:
setText
. For example, setText(mod.callAttr(...).toString())
, where mod
is the return value of getModule
.Or:
callAttr
, then have the Python code call setText
. For example, if your Python function is called f
, then you would write mod.callAttr("f", simpleTextView)
, and the function would receive the TextView
as its argument.