@@ -4,47 +4,85 @@ import android.graphics.BitmapFactory
44import androidx.appcompat.app.AppCompatActivity
55import android.os.Bundle
66import android.util.Log
7+ import android.widget.Button
78import android.widget.ImageView
9+ import android.widget.TextView
10+ import android.graphics.Color
11+ import androidx.constraintlayout.widget.ConstraintLayout
812import com.chaquo.python.PyException
913import com.chaquo.python.Python
1014import com.chaquo.python.android.AndroidPlatform
1115import kotlinx.coroutines.CoroutineScope
1216import kotlinx.coroutines.Dispatchers
1317import kotlinx.coroutines.launch
1418import kotlinx.coroutines.withContext
19+ import org.json.JSONObject
1520
1621class MainActivity : AppCompatActivity () {
1722 override fun onCreate (savedInstanceState : Bundle ? ) {
1823 super .onCreate(savedInstanceState)
1924 setContentView(R .layout.activity_main)
2025
21- val imageView = findViewById<ImageView >(R .id.image_home)
22-
2326 // Initialize Chaquopy
2427 if (! Python .isStarted()) {
2528 Python .start(AndroidPlatform (this ))
2629 }
30+
2731 val py = Python .getInstance()
28- val plotModule = py.getModule(" plot" )
29- // val createButtonModule = py.getModule("create_button")
30- val xInput = " 1 2 3 4 5"
31- val yInput = " 1 4 9 16 25"
32- CoroutineScope (Dispatchers .Main ).launch {
33- try {
34- val bytes = plotModule.callAttr(
35- " plot" ,
36- xInput,
37- yInput
38- ).toJava(ByteArray ::class .java)
39- withContext(Dispatchers .IO ) {
40- val bitmap = BitmapFactory .decodeByteArray(bytes, 0 , bytes.size)
41- withContext(Dispatchers .Main ) {
42- imageView.setImageBitmap(bitmap)
32+
33+ // Generate UI from Python
34+ val uiLayoutModule = py.getModule(" ui_layout" )
35+ val layoutJson = uiLayoutModule.callAttr(" generate_layout" ).toString()
36+ val layout = JSONObject (layoutJson)
37+ val widgets = layout.getJSONArray(" widgets" )
38+
39+ for (i in 0 until widgets.length()) {
40+ val widget = widgets.getJSONObject(i)
41+ when (widget.getString(" type" )) {
42+ " Button" -> {
43+ val button = Button (this )
44+ button.text = widget.getJSONObject(" properties" ).getString(" text" )
45+ button.setTextColor(Color .parseColor(widget.getJSONObject(" properties" ).getString(" textColor" )))
46+ button.setBackgroundColor(Color .parseColor(widget.getJSONObject(" properties" ).getString(" backgroundColor" )))
47+
48+ if (widget.has(" eventHandlers" ) && widget.getJSONObject(" eventHandlers" ).has(" onClick" )) {
49+ val onClickFunctionName = widget.getJSONObject(" eventHandlers" ).getString(" onClick" )
50+ val onClickFunction = py.getModule(" ui_layout" ).get(onClickFunctionName)
51+
52+ button.setOnClickListener {
53+ onClickFunction?.call()
54+ }
4355 }
56+
57+ // Add button to your layout here
58+ val layoutMain = findViewById<ConstraintLayout >(R .id.layout_main)
59+ layoutMain.addView(button)
4460 }
45- } catch (e: PyException ) {
46- Log .e(" Python Error" , " Error executing Python code" , e)
61+ // Handle other widget types...
4762 }
4863 }
64+
65+ // Existing code for displaying plot
66+ // val imageView = findViewById<ImageView>(R.id.image_home)
67+ // val plotModule = py.getModule("plot")
68+ // val xInput = "1 2 3 4 5"
69+ // val yInput = "1 4 9 16 25"
70+ // CoroutineScope(Dispatchers.Main).launch {
71+ // try {
72+ // val bytes = plotModule.callAttr(
73+ // "plot",
74+ // xInput,
75+ // yInput
76+ // ).toJava(ByteArray::class.java)
77+ // withContext(Dispatchers.IO) {
78+ // val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
79+ // withContext(Dispatchers.Main) {
80+ // imageView.setImageBitmap(bitmap)
81+ // }
82+ // }
83+ // } catch (e: PyException) {
84+ // Log.e("Python Error", "Error executing Python code", e)
85+ // }
86+ // }
4987 }
50- }
88+ }
0 commit comments