Skip to content

Commit 6f3c8fa

Browse files
committed
Add create_constraint_layout.py
1 parent 678ff26 commit 6f3c8fa

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ 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)
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 createConstraintLayoutModule = py.getModule("create_constraint_layout")
44+
val pyLayout = createConstraintLayoutModule.callAttr("create_constraint_layout", this).toJava(ConstraintLayout::class.java)
4145
layoutMain.addView(pyLayout)
4246

4347
// val createRecyclerViewModule = py.getModule("create_recycler_view")
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from java import jclass
2+
3+
4+
BottomNavigationView = jclass(
5+
"com.google.android.material.bottomnavigation.BottomNavigationView"
6+
)
7+
ConstraintLayout = jclass("androidx.constraintlayout.widget.ConstraintLayout")
8+
View = jclass("android.view.View")
9+
ViewGroup = jclass("android.view.ViewGroup")
10+
11+
12+
def create_constraint_layout(context):
13+
# Create ConstraintLayout
14+
layout = ConstraintLayout(context)
15+
layout_params = ViewGroup.LayoutParams(
16+
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
17+
)
18+
layout.setLayoutParams(layout_params)
19+
20+
# Create BottomNavigationView
21+
bottom_nav = BottomNavigationView(context)
22+
bottom_nav.setId(View.generateViewId()) # Add this line to generate unique id for the view
23+
24+
# Create Menu for BottomNavigationView
25+
menu = bottom_nav.getMenu()
26+
27+
# Add items to the menu
28+
menu.add(0, 0, 0, "Home")
29+
menu.add(0, 1, 0, "Search")
30+
menu.add(0, 2, 0, "Notifications")
31+
menu.add(0, 3, 0, "Messages")
32+
menu.add(0, 4, 0, "Profile")
33+
34+
# Add BottomNavigationView to ConstraintLayout
35+
nav_layout_params = ConstraintLayout.LayoutParams(
36+
ConstraintLayout.LayoutParams.MATCH_PARENT,
37+
ConstraintLayout.LayoutParams.WRAP_CONTENT
38+
)
39+
# Set the constraints here
40+
nav_layout_params.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
41+
bottom_nav.setLayoutParams(nav_layout_params)
42+
layout.addView(bottom_nav)
43+
44+
return layout

0 commit comments

Comments
 (0)