Skip to content

Commit 5075b3a

Browse files
committed
Add Android Context to pythonnative
1 parent 6331007 commit 5075b3a

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

apps/android_pythonnative_3/app/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ android {
2121
}
2222
python {
2323
pip {
24+
// https://chaquo.com/chaquopy/doc/current/android.html#android-requirements
2425
// install "matplotlib"
25-
install "pythonnative"
26+
// install "pythonnative"
27+
28+
// A directory containing a setup.py, relative to the project
29+
// directory (must contain at least one slash):
30+
install "/Users/owenthcarey/Documents/pythonnative-workspace/libs/pythonnative"
2631
}
2732
}
2833
}

apps/android_pythonnative_3/app/src/main/java/com/pythonnative/pythonnative/MainActivity.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ class MainActivity : AppCompatActivity() {
3636
// val pyButton = createButtonModule.callAttr("create_button", this).toJava(Button::class.java)
3737
// layoutMain.addView(pyButton)
3838

39-
val createWidgetsModule = py.getModule("create_widgets")
40-
val pyLayout = createWidgetsModule.callAttr("create_widgets", this).toJava(LinearLayout::class.java)
41-
layoutMain.addView(pyLayout)
42-
43-
// val createLayoutModule = py.getModule("create_pn_layout")
44-
// val pyLayout = createLayoutModule.callAttr("create_pn_layout").toJava(LinearLayout::class.java)
39+
// val createWidgetsModule = py.getModule("create_widgets")
40+
// val pyLayout = createWidgetsModule.callAttr("create_widgets", this).toJava(LinearLayout::class.java)
4541
// layoutMain.addView(pyLayout)
4642

43+
val createLayoutModule = py.getModule("create_pn_layout")
44+
val pyLayout = createLayoutModule.callAttr("create_pn_layout", this).toJava(LinearLayout::class.java)
45+
layoutMain.addView(pyLayout)
46+
4747
// val createConstraintLayoutModule = py.getModule("create_constraint_layout")
4848
// val pyLayout = createConstraintLayoutModule.callAttr("create_constraint_layout", this).toJava(ConstraintLayout::class.java)
4949
// layoutMain.addView(pyLayout)

apps/android_pythonnative_3/app/src/main/python/create_pn_layout.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
import pythonnative as pn
44

55

6-
def create_pn_layout():
7-
layout = pn.LinearLayout()
6+
def create_pn_layout(context):
7+
layout = pn.LinearLayout(context)
88

9-
label = pn.Label("This is a PythonNative label")
9+
label = pn.Label(context, "This is a PythonNative label")
1010
layout.add_view(label)
1111

12-
button = pn.Button("Click me")
12+
button = pn.Button(context, "Click me")
1313
layout.add_view(button)
1414

1515
return layout.native_instance
16+
17+
# from java import jclass
18+
# LinearLayout = jclass("android.widget.LinearLayout")
19+
# layout = LinearLayout(context)
20+
# return layout

libs/pythonnative/pythonnative/button.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
class Button(View):
88
native_class = jclass("android.widget.Button")
99

10-
def __init__(self, title: str = "") -> None:
10+
def __init__(self, context, title: str = "") -> None:
1111
super().__init__()
12-
self.native_instance = self.native_class()
12+
self.native_instance = self.native_class(context)
1313
self.set_title(title)
1414

1515
def set_title(self, title: str) -> None:

libs/pythonnative/pythonnative/label.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
class Label(View):
88
native_class = jclass("android.widget.TextView")
99

10-
def __init__(self, text: str = "") -> None:
10+
def __init__(self, context, text: str = "") -> None:
1111
super().__init__()
12-
self.native_instance = self.native_class()
12+
self.native_instance = self.native_class(context)
1313
self.set_text(text)
1414

1515
def set_text(self, text: str) -> None:

libs/pythonnative/pythonnative/linear_layout.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
class LinearLayout(View):
88
native_class = jclass("android.widget.LinearLayout")
99

10-
def __init__(self) -> None:
10+
def __init__(self, context) -> None:
1111
super().__init__()
12-
self.native_instance = self.native_class()
13-
self.native_instance.setOrientation(1) # Set orientation to vertical
12+
self.native_instance = self.native_class(context)
13+
self.native_instance.setOrientation(self.native_class.VERTICAL) # Set orientation to vertical
1414
self.views = []
1515

1616
def add_view(self, view):

0 commit comments

Comments
 (0)