style: convert number picker to material design

This commit is contained in:
Andries Smit 2022-04-08 13:33:12 +02:00
parent fa9a470526
commit 1b093af941
8 changed files with 103 additions and 81 deletions

View file

@ -1,34 +1,57 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="60dp" android:layout_width="60dp"
android:layout_height="100dp" android:layout_height="100dp">
android:background="@drawable/background_total"
android:orientation="vertical"> <com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:errorEnabled="true"
app:hintEnabled="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:imeOptions="actionDone"
android:inputType="number"
tools:text="1" />
</com.google.android.material.textfield.TextInputLayout>
<ImageButton <ImageButton
android:id="@+id/increment" android:id="@+id/increment"
android:layout_width="match_parent" android:layout_width="0dp"
android:layout_height="30dp"
android:background="@color/transparent"
android:src="@drawable/ic_plus" />
<EditText
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="40dp" android:layout_height="40dp"
android:background="@drawable/border_gray" android:layout_marginStart="-2dp"
android:gravity="center" android:layout_marginTop="-4dp"
android:imeOptions="actionDone" android:layout_marginEnd="-2dp"
android:inputType="number" android:contentDescription="@string/a11y_min_button_description"
android:textColor="?attr/numPickerText" android:src="@drawable/ic_plus"
tools:text="1" /> app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton <ImageButton
android:id="@+id/decrement" android:id="@+id/decrement"
android:layout_width="match_parent" android:layout_width="0dp"
android:layout_height="30dp" android:layout_height="40dp"
android:background="@color/transparent" android:layout_marginStart="-2dp"
android:src="@drawable/ic_minus" /> android:layout_marginEnd="-2dp"
android:layout_marginBottom="-3.8dp"
android:contentDescription="@string/a11y_plus_button_description"
android:src="@drawable/ic_minus"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -19,10 +19,12 @@ import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityManager import android.view.accessibility.AccessibilityManager
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.Button import android.widget.Button
import android.widget.EditText
import android.widget.ImageButton import android.widget.ImageButton
import android.widget.LinearLayout import android.widget.LinearLayout
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import info.nightscout.androidaps.core.R import info.nightscout.androidaps.core.R
import info.nightscout.androidaps.extensions.toVisibility
import info.nightscout.androidaps.utils.ToastUtils import info.nightscout.androidaps.utils.ToastUtils
import info.nightscout.shared.SafeParse import info.nightscout.shared.SafeParse
import java.text.NumberFormat import java.text.NumberFormat
@ -39,9 +41,10 @@ open class NumberPicker(context: Context, attrs: AttributeSet? = null) : LinearL
fun onValueChanged(value: Double) fun onValueChanged(value: Double)
} }
var editText: EditText? = null var editText: TextInputEditText? = null
private var minusButton: ImageButton? = null private var minusButton: ImageButton? = null
private var plusButton: ImageButton? = null private var plusButton: ImageButton? = null
var textInputLayout: TextInputLayout? = null
var currentValue = 0.0 var currentValue = 0.0
var minValue = 0.0 var minValue = 0.0
var maxValue = 1.0 var maxValue = 1.0
@ -110,6 +113,7 @@ open class NumberPicker(context: Context, attrs: AttributeSet? = null) : LinearL
minusButton?.id = generateViewId() minusButton?.id = generateViewId()
plusButton = findViewById(R.id.increment) plusButton = findViewById(R.id.increment)
plusButton?.id = generateViewId() plusButton?.id = generateViewId()
textInputLayout = findViewById(R.id.textInputLayout)
editText = findViewById(R.id.display) editText = findViewById(R.id.display)
editText?.id = generateViewId() editText?.id = generateViewId()
minusButton?.setOnTouchListener(this) minusButton?.setOnTouchListener(this)
@ -124,7 +128,9 @@ open class NumberPicker(context: Context, attrs: AttributeSet? = null) : LinearL
override fun afterTextChanged(s: Editable) { override fun afterTextChanged(s: Editable) {
if (focused) currentValue = SafeParse.stringToDouble(editText?.text.toString()) if (focused) currentValue = SafeParse.stringToDouble(editText?.text.toString())
callValueChangedListener() callValueChangedListener()
okButton?.visibility = if (currentValue > maxValue || currentValue < minValue) INVISIBLE else VISIBLE val inValid = currentValue > maxValue || currentValue < minValue
okButton?.visibility = inValid.not().toVisibility()
textInputLayout?.error = if (inValid) "invalid" else null
} }
}) })
editText?.setOnFocusChangeListener { _: View?, hasFocus: Boolean -> editText?.setOnFocusChangeListener { _: View?, hasFocus: Boolean ->

View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="?attr/totalBackground"/>
<stroke android:width="1dp" android:color="?attr/colorControlNormal"/>
<corners android:radius="4dp" />
</shape>

View file

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="0dp">
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="?attr/colorControlNormal"/>
<solid android:color="@android:color/white" />
<corners android:radius="0dp" />
</shape>
</item>
</layer-list>

View file

@ -1,34 +1,58 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="130dp" android:layout_width="130dp"
android:layout_height="40dp" android:layout_height="40dp">
android:background="@drawable/background_total"
android:orientation="horizontal"> <com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true"
app:hintEnabled="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:imeOptions="actionDone"
android:inputType="number"
android:padding="9dp"
tools:text="1" />
</com.google.android.material.textfield.TextInputLayout>
<ImageButton <ImageButton
android:id="@+id/decrement" android:id="@+id/decrement"
android:layout_width="30dp" android:layout_width="40dp"
android:layout_height="match_parent" android:layout_height="0dp"
android:background="@color/transparent" android:layout_marginStart="-2dp"
android:src="@drawable/ic_minus" /> android:layout_marginTop="-4dp"
android:layout_marginBottom="-3.7dp"
<EditText android:contentDescription="@string/a11y_min_button_description"
android:id="@+id/display" android:src="@drawable/ic_minus"
android:layout_width="70dp" app:layout_constraintBottom_toBottomOf="parent"
android:layout_height="match_parent" app:layout_constraintStart_toStartOf="parent"
android:background="@drawable/border_gray" app:layout_constraintTop_toTopOf="parent" />
android:gravity="center"
android:imeOptions="actionDone"
android:inputType="number"
android:textColor="?attr/numPickerText"
tools:text="1" />
<ImageButton <ImageButton
android:id="@+id/increment" android:id="@+id/increment"
android:layout_width="30dp" android:layout_width="40dp"
android:layout_height="match_parent" android:layout_height="0dp"
android:background="@color/transparent" android:layout_marginTop="-4dp"
android:src="@drawable/ic_plus" /> android:layout_marginEnd="-2dp"
android:layout_marginBottom="-3.7dp"
android:contentDescription="@string/a11y_plus_button_description"
android:src="@drawable/ic_plus"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -32,16 +32,12 @@
<item name="actionModeCloseDrawable">@drawable/ic_close</item> <item name="actionModeCloseDrawable">@drawable/ic_close</item>
<!---bolus color --> <!---bolus color -->
<item name="bolusColor">@color/bolus</item> <item name="bolusColor">@color/bolus</item>
<!---Number Picker text color -->
<item name="numPickerText">@color/black</item>
<!---NS Client action text color --> <!---NS Client action text color -->
<item name="actionButton">@color/action</item> <item name="actionButton">@color/action</item>
<!---Text color for Quickwizard and more --> <!---Text color for Quickwizard and more -->
<item name="cardObjectiveText">@color/cardObjectiveText</item> <item name="cardObjectiveText">@color/cardObjectiveText</item>
<!---Text color for misc buttons and texts --> <!---Text color for misc buttons and texts -->
<item name="alarmColor">@color/alarm</item> <item name="alarmColor">@color/alarm</item>
<!-- Number picker and more -->
<item name="totalBackground">@color/sphere_plastic_grey</item>
<!-- BG source temp button --> <!-- BG source temp button -->
<item name="setTempButton">@color/colorSetTempButton</item> <item name="setTempButton">@color/colorSetTempButton</item>
<!-- Card Item--> <!-- Card Item-->

View file

@ -1,16 +1,12 @@
<resources> <resources>
<!---bolus color --> <!---bolus color -->
<attr name="bolusColor" format="reference|color" /> <attr name="bolusColor" format="reference|color" />
<!---Number Picker text color -->
<attr name="numPickerText" format="reference|color" />
<!---NS Client action text color --> <!---NS Client action text color -->
<attr name="actionButton" format="reference|color" /> <attr name="actionButton" format="reference|color" />
<!---Text color for Quickwizard and more --> <!---Text color for Quickwizard and more -->
<attr name="cardObjectiveText" format="reference|color" /> <attr name="cardObjectiveText" format="reference|color" />
<!---Text color for misc buttons and texts --> <!---Text color for misc buttons and texts -->
<attr name="alarmColor" format="reference|color" /> <attr name="alarmColor" format="reference|color" />
<!-- Number picker and more -->
<attr name="totalBackground" format="reference|color" />
<!-- BG source temp button --> <!-- BG source temp button -->
<attr name="setTempButton" format="reference|color" /> <attr name="setTempButton" format="reference|color" />
<!-- Card Item--> <!-- Card Item-->

View file

@ -40,16 +40,12 @@
<item name="actionModeCloseDrawable">@drawable/ic_close</item> <item name="actionModeCloseDrawable">@drawable/ic_close</item>
<!---bolus color --> <!---bolus color -->
<item name="bolusColor">@color/bolus</item> <item name="bolusColor">@color/bolus</item>
<!---Number Picker text color -->
<item name="numPickerText">@color/black</item>
<!---NS Client action text color --> <!---NS Client action text color -->
<item name="actionButton">@android:color/holo_orange_light</item> <item name="actionButton">@android:color/holo_orange_light</item>
<!---Text color for Quickwizard and more --> <!---Text color for Quickwizard and more -->
<item name="cardObjectiveText">@color/cardObjectiveText</item> <item name="cardObjectiveText">@color/cardObjectiveText</item>
<!---Text color for misc buttons and texts --> <!---Text color for misc buttons and texts -->
<item name="alarmColor">@color/alarm</item> <item name="alarmColor">@color/alarm</item>
<!-- Number picker and more -->
<item name="totalBackground">@color/gray</item>
<!-- BG source temp button --> <!-- BG source temp button -->
<item name="setTempButton">@color/colorSetTempButton</item> <item name="setTempButton">@color/colorSetTempButton</item>
<!-- Card Item--> <!-- Card Item-->