changed qfinish button design

This commit is contained in:
oxidiert
2025-09-19 17:16:53 +02:00
parent ca8f6ca8e4
commit 0a04568a7c
3 changed files with 66 additions and 43 deletions

View File

@ -7,6 +7,7 @@ import kotlinx.coroutines.*
import android.util.TypedValue
import android.widget.TextView
import androidx.core.widget.TextViewCompat
import com.google.android.material.button.MaterialButton
class HandlerLastPage(
private val answers: Map<String, Any>,
@ -26,6 +27,8 @@ class HandlerLastPage(
val titleTv = layout.findViewById<TextView>(R.id.textView)
val questionTv = layout.findViewById<TextView>(R.id.question)
val prevBtn = layout.findViewById<MaterialButton>(R.id.Qprev)
val finishBtn = layout.findViewById<MaterialButton>(R.id.Qfinish)
// Texte setzen
titleTv.text = LanguageManager.getText(languageID, currentQuestion.textKey)
@ -34,43 +37,40 @@ class HandlerLastPage(
Html.FROM_HTML_MODE_LEGACY
)
// ==== Schriftgrößen prozentual zur Bildschirmhöhe ====
// Passe die Faktoren bei Bedarf an (z. B. 0.032f für etwas größer)
// Finish-Button: Text + responsive Schrift
finishBtn.text = LanguageManager.getText(languageID, "save")
finishBtn.isAllCaps = false
applyResponsiveTextSizing(finishBtn)
// Überschriften responsiv skalieren (wie zuvor)
setTextSizePercentOfScreenHeight(titleTv, 0.03f)
setTextSizePercentOfScreenHeight(questionTv, 0.03f)
// =====================================================
// Buttons
layout.findViewById<Button>(R.id.Qprev).setOnClickListener {
goToPreviousQuestion()
}
prevBtn.setOnClickListener { goToPreviousQuestion() }
layout.findViewById<Button>(R.id.Qfinish).setOnClickListener {
showLoading(true) // Show loading indicator
finishBtn.setOnClickListener {
showLoading(true)
// Save answers on a background thread
CoroutineScope(Dispatchers.IO).launch {
val startTime = System.currentTimeMillis()
// Save answers to database (suspend function)
// Antworten speichern
saveAnswersToDatabase(answers)
// Calculate total points and update global value
// Punkte summieren
GlobalValues.INTEGRATION_INDEX = sumPoints()
// Save last client code globally if available
// Client-Code merken
val clientCode = answers["client_code"] as? String
if (clientCode != null) GlobalValues.LAST_CLIENT_CODE = clientCode
// Ensure loading animation runs at least 2 seconds
// min. Ladezeit einhalten
val elapsedTime = System.currentTimeMillis() - startTime
if (elapsedTime < minLoadingTimeMs) {
delay(minLoadingTimeMs - elapsedTime)
}
if (elapsedTime < minLoadingTimeMs) delay(minLoadingTimeMs - elapsedTime)
// Switch back to main thread to update UI
withContext(Dispatchers.Main) {
showLoading(false) // Hide loading indicator
showLoading(false)
val activity = layout.context as? MainActivity
activity?.finishQuestionnaire() ?: goToNextQuestion()
}
@ -78,25 +78,50 @@ class HandlerLastPage(
}
}
override fun validate(): Boolean = true // No validation needed on last page
override fun saveAnswer() {} // No answers to save here
override fun validate(): Boolean = true
override fun saveAnswer() {}
// ---------- Responsive Textgröße für den Finish-Button ----------
private fun applyResponsiveTextSizing(btn: MaterialButton) {
// Max-/Min-Sp anhand der Bildschirmhöhe (in sp) berechnen
val dm = btn.resources.displayMetrics
val maxSp = (dm.heightPixels * 0.028f) / dm.scaledDensity // ~2.8% der Höhe
val minSp = (dm.heightPixels * 0.018f) / dm.scaledDensity // ~1.8% der Höhe
// AutoSize aktivieren (schrumpft/expandiert den Text innerhalb des Buttons)
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(
btn,
minSp.toInt(),
maxSp.toInt(),
1,
TypedValue.COMPLEX_UNIT_SP
)
btn.setSingleLine(true)
btn.maxLines = 1
btn.isAllCaps = false
// Padding nach Layout proportional zur Button-Höhe setzen (wirkt auf Lesbarkeit)
btn.post {
val padH = (btn.height * 0.18f).toInt()
val padV = (btn.height * 0.12f).toInt()
btn.setPadding(padH, padV, padH, padV)
}
}
// ----------------------------------------------------------------
// Helper: Textgröße prozentual zur Bildschirmhöhe setzen (in sp)
private fun setTextSizePercentOfScreenHeight(view: TextView, percentOfHeight: Float) {
val dm = (view.context ?: layout.context).resources.displayMetrics
val sp = (dm.heightPixels * percentOfHeight) / dm.scaledDensity
// AutoSize ausschalten, damit unsere Größe nicht überschrieben wird
TextViewCompat.setAutoSizeTextTypeWithDefaults(view, TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE)
view.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp)
}
// Calculate the sum of all keys ending with "_points"
private fun sumPoints(): Int =
answers.filterKeys { it.endsWith("_points") }
.values.mapNotNull { it as? Int }
.sum()
// Show or hide a ProgressBar (loading spinner)
private fun showLoading(show: Boolean) {
val progressBar = layout.findViewById<ProgressBar>(R.id.progressBar)
val finishButton = layout.findViewById<Button>(R.id.Qfinish)

View File

@ -5,6 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Zurück (links unten) -->
<com.google.android.material.button.MaterialButton
android:id="@+id/Qprev"
android:layout_width="@dimen/nav_btn_size"
@ -23,27 +24,24 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
<!-- Fertig/Speichern (rechts unten) gleiche Farbe wie Qprev -->
<com.google.android.material.button.MaterialButton
android:id="@+id/Qfinish"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="12dp"
android:layout_marginStart="70dp"
android:autoSizeMaxTextSize="48sp"
android:autoSizeMinTextSize="12sp"
android:autoSizeStepGranularity="2sp"
android:autoSizeTextType="uniform"
android:gravity="center"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:tag="save"
android:layout_width="@dimen/finish_btn_width"
android:layout_height="@dimen/nav_btn_size"
android:layout_marginEnd="20dp"
android:layout_marginBottom="16dp"
android:textAllCaps="false"
android:minWidth="0dp"
android:insetLeft="0dp"
android:insetRight="0dp"
android:paddingStart="24dp"
android:paddingEnd="24dp"
app:cornerRadius="999dp"
app:backgroundTint="@color/btn_nav_right_tint"
app:rippleColor="@color/btn_nav_right_ripple"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.07"
app:layout_constraintStart_toEndOf="@id/Qprev"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:layout_constraintWidth_percent="0.30" />
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/textView"
@ -78,7 +76,6 @@
app:layout_constraintVertical_bias="0.0"
app:layout_constraintWidth_percent="0.9" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"

View File

@ -2,4 +2,5 @@
<resources>
<dimen name="nav_btn_size">64dp</dimen>
<dimen name="nav_icon_size">28dp</dimen>
<dimen name="finish_btn_width">160dp</dimen>
</resources>