Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtquick-for-android-fragments.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2025 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\title Android Fragments with Qt Quick for Android
6
\brief Tutorial on using Qt Quick Views in Android Fragments
7
\page qtquick-for-android-fragments.html
8
\ingroup qq4a-extra-topics
9
10
You can have a \l QtQuickView in an Android UI layout by using a ViewGroup-based
11
object. Here we'll use a \l{Android: FrameLayout}{FrameLayout}.
12
13
If you're not familiar with the \l QtQuickView API, read its documentation
14
before continuing with this tutorial.
15
16
Before proceeding, it's worthwhile to explore the Qt Academy course,
17
\l{Qt Academy: Embed 3D in an Android}{Embedding Qt Quick 3D Content in an Android App}.
18
19
To start, create a new project in Android Studio using the
20
\uicontrol{Bottom Navigation Views Activity} template.
21
22
\list 1
23
\li Locate the Fragment or Activity under which you want your QtQuickView
24
to be visible. Here we use \c HomeFragment and \c fragment_home.xml.
25
26
\li In \c fragment_home.xml create a FrameLayout and set its \c id as
27
shown below.
28
\code
29
<FrameLayout
30
android:id="@+id/homeQmlFrame"
31
android:layout_width="0dp"
32
android:layout_height="0dp"
33
app:layout_constraintTop_toTopOf="parent"
34
app:layout_constraintStart_toStartOf="parent"
35
app:layout_constraintEnd_toEndOf="parent"
36
app:layout_constraintHeight_percent="0.8"/>
37
\endcode
38
Note this id, as it needed to be referred to in \c HomeFragment when
39
binding the layout.
40
41
\li Inside HomeFragment.kt, add an import statement for FrameLayout:
42
\code
43
import android.widget.FrameLayout
44
\endcode
45
46
\li Add your imports for your \l QtQuickView and Screen01 QML type
47
and declare them inside the class:
48
49
\code
50
import org.qtproject.qt.android.QtQuickView
51
import org.qtproject.example.RoboApp.RoboContent.Screen01
52
53
class HomeFragment : Fragment() {
54
55
private var binding: FragmentHomeBinding? = null
56
private lateinit var homeQmlContent: Screen01
57
private lateinit var homeQtQuickView: QtQuickView
58
\endcode
59
60
\li Assign your QtQuickView, giving it the Activity instance
61
using \c requireActivity()
62
\code
63
homeQtQuickView = QtQuickView(requireActivity())
64
homeQmlContent = Screen01()
65
\endcode
66
67
\li Initialize the layout parameters, if you create views
68
programmatically, add views dynamically or change them on runtime.
69
Otherwise you may skip this section and you do not need to use
70
\c params with \c addView().
71
\code
72
val params = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
73
ViewGroup.LayoutParams.MATCH_PARENT)
74
\endcode
75
76
\li Add your view to the layout, either with:
77
\list 1
78
\li Using \l{Android: View binding}{View binding} inside \c onCreateView():
79
First check that view binding is enabled by adding \c buildFeature
80
section into build.gradle.kts android section of your app:
81
\code
82
buildFeatures {
83
viewBinding = true
84
}
85
\endcode
86
Then add the following in \c onCreateView():
87
\code
88
binding = FragmentHomeBinding.inflate( inflater, container, false)
89
homeQtQuickView.loadContent(homeQmlContent)
90
91
val root: View = binding.root
92
binding.homeQmlFrame.addView(homeQtQuickView, params)
93
...
94
return root
95
\endcode
96
\li Using \c {findViewById()} inside \c onCreate():
97
\code
98
val qtFrame = findViewById(R.id.qtFrame)
99
qmlFrame.addView(m_quickView, params)
100
m_quickView.loadContent(homeQmlContent)
101
\endcode
102
See usage from other
103
\l {Qt Quick for Android Studio Projects}{Qt Quick for Android examples}.
104
\endlist
105
\endlist
106
Your Qt Quick content will now appear in your home fragment.
107
*/
qtdeclarative
src
quick
doc
src
android
qtquick-for-android-fragments.qdoc
Generated on
for Qt by
1.14.0