aboutsummaryrefslogtreecommitdiffstats
path: root/examples/platforms/android/qml_in_java_based_android_project/app/src/main/java/com/example/qml_in_java_based_android_project/MainActivity.java
blob: e7aee43c55c8c46ead9c5e6e1cda46dbc342d433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
package com.example.qml_in_java_based_android_project;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.qtproject.qt.android.QtQuickView;

import java.util.HashMap;
import java.util.Map;


// Implement QtQuickView StatusChangeListener interface to get status updates
// from the underlying QQuickView
public class MainActivity extends AppCompatActivity implements QtQuickView.StatusChangeListener {

    private static final String TAG = "myTag";
    private final Colors m_colors = new Colors();
    private final Map<Integer, String> m_statusNames = new HashMap<Integer, String>()  {{
        put(QtQuickView.STATUS_READY, " READY");
        put(QtQuickView.STATUS_LOADING, " LOADING");
        put(QtQuickView.STATUS_ERROR, " ERROR");
        put(QtQuickView.STATUS_NULL, " NULL");
    }};
    private int m_qmlButtonSignalListenerId;
    private LinearLayout m_mainLinear;
    private FrameLayout m_qmlFrameLayout;
    private QtQuickView m_qmlView;
    private LinearLayout m_androidControlsLayout;
    private TextView m_getPropertyValueText;
    private TextView m_qmlStatus;
    private SwitchCompat m_switch;
    private View m_box;

    //! [onCreate]
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        m_mainLinear = findViewById(R.id.mainLinear);
        m_getPropertyValueText = findViewById(R.id.getPropertyValueText);
        m_qmlStatus = findViewById(R.id.qmlStatus);
        m_androidControlsLayout = findViewById(R.id.javaLinear);
        m_box = findViewById(R.id.box);

        m_switch = findViewById(R.id.switch1);
        m_switch.setOnClickListener(view -> switchListener());
        //! [m_qmlView]
        m_qmlView = new QtQuickView(this, "qrc:/qt/qml/qml_in_android_view/main.qml",
                "qml_in_android_view");
        //! [m_qmlView]

        // Set status change listener for m_qmlView
        // listener implemented below in OnStatusChanged
        //! [setStatusChangeListener]
        m_qmlView.setStatusChangeListener(this);
        //! [setStatusChangeListener]
        //! [layoutParams]
        ViewGroup.LayoutParams params = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        m_qmlFrameLayout = findViewById(R.id.qmlFrame);
        m_qmlFrameLayout.addView(m_qmlView, params);
        //! [layoutParams]
        Button button = findViewById(R.id.button);
        button.setOnClickListener(view -> onClickListener());

        // Check target device orientation on launch
        handleOrientationChanges();
    }
    //! [onCreate]
    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        handleOrientationChanges();
    }

    private void handleOrientationChanges() {
        // When specific target device display configurations (listed in AndroidManifest.xml
        // android:configChanges) change, get display metrics and make needed changes to UI
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        ViewGroup.LayoutParams qmlFrameLayoutParams = m_qmlFrameLayout.getLayoutParams();
        ViewGroup.LayoutParams linearLayoutParams = m_androidControlsLayout.getLayoutParams();

        if (displayMetrics.heightPixels > displayMetrics.widthPixels) {
            m_mainLinear.setOrientation(LinearLayout.VERTICAL);
            qmlFrameLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            qmlFrameLayoutParams.height = 0;
            linearLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            linearLayoutParams.height = 0;
        } else {
            m_mainLinear.setOrientation(LinearLayout.HORIZONTAL);
            qmlFrameLayoutParams.width = 0;
            qmlFrameLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
            linearLayoutParams.width = 0;
            linearLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
        }
        m_qmlFrameLayout.setLayoutParams(qmlFrameLayoutParams);
        m_androidControlsLayout.setLayoutParams(linearLayoutParams);
    }

    //! [onStatusChanged]
    @Override
    public void onStatusChanged(int status) {
        Log.i(TAG, "Status of QtQuickView: " + status);

        final String qmlStatus = getResources().getString(R.string.qml_view_status)
                + m_statusNames.get(status);

        // Show current QML View status in a textview
        m_qmlStatus.setText(qmlStatus);

        // Connect signal listener to "onClicked" signal from main.qml
        // addSignalListener returns int which can be used later to identify the listener
        //! [qml signal listener]
        if (status == QtQuickView.STATUS_READY && !m_switch.isChecked()) {
            m_qmlButtonSignalListenerId = m_qmlView.connectSignalListener("onClicked", Object.class,
                    (String signal, Object o) -> {
                Log.i(TAG, "QML button clicked");
                m_androidControlsLayout.setBackgroundColor(Color.parseColor(m_colors.getColor()));
            });

        }
        //! [qml signal listener]
    }
    //! [onStatusChanged]
    //! [onClickListener]
    public void onClickListener() {
        // Set the QML view root object property "colorStringFormat" value to
        // color from Colors.getColor()
        m_qmlView.setProperty("colorStringFormat", m_colors.getColor());

        String qmlBackgroundColor = m_qmlView.getProperty("colorStringFormat");

        // Display the QML View background color code
        m_getPropertyValueText.setText(qmlBackgroundColor);

        // Display the QML View background color in a view
        m_box.setBackgroundColor(Color.parseColor(qmlBackgroundColor));
    }
    //! [onClickListener]

    public void switchListener() {
        TextView text = findViewById(R.id.switchText);
        // Disconnect QML button signal listener if switch is On using the saved signal listener Id
        // and connect it again if switch is turned off
        if (m_switch.isChecked()) {
            Log.i(TAG, "QML button onClicked signal listener disconnected");
            text.setText(R.string.connect_qml_button_signal_listener);
            //! [disconnect qml signal listener]
            m_qmlView.disconnectSignalListener(m_qmlButtonSignalListenerId);
            //! [disconnect qml signal listener]
        } else {
            Log.i(TAG, "QML button onClicked signal listener connected");
            text.setText(R.string.disconnect_qml_button_signal_listener);
            m_qmlButtonSignalListenerId = m_qmlView.connectSignalListener("onClicked",
                    Object.class, (String t, Object value) -> {
                Log.i(TAG, "QML button clicked");
                m_androidControlsLayout.setBackgroundColor(Color.parseColor(m_colors.getColor()));
            });
        }
    }
}