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

View File

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

View File

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