From 2034e3ab78fefb1877bbafacecd3da105fb20f29 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sat, 28 Aug 2021 12:12:32 +0200 Subject: [PATCH] Dropdown list as outlined box --- .../keepass/view/TextSelectFieldView.kt | 79 ++++++++++++++----- .../res/layout/item_select_field_value.xml | 8 +- 2 files changed, 62 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/view/TextSelectFieldView.kt b/app/src/main/java/com/kunzisoft/keepass/view/TextSelectFieldView.kt index c5ae92f55..e420dcc9e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/TextSelectFieldView.kt +++ b/app/src/main/java/com/kunzisoft/keepass/view/TextSelectFieldView.kt @@ -2,21 +2,27 @@ package com.kunzisoft.keepass.view import android.content.Context import android.os.Build +import android.text.InputType import android.util.AttributeSet import android.util.TypedValue import android.view.ContextThemeWrapper import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.view.inputmethod.EditorInfo import android.widget.* import androidx.annotation.DrawableRes import androidx.appcompat.widget.AppCompatImageButton -import androidx.appcompat.widget.AppCompatTextView import androidx.core.content.ContextCompat +import androidx.core.graphics.BlendModeColorFilterCompat +import androidx.core.graphics.BlendModeCompat import androidx.core.view.ViewCompat import androidx.core.view.isVisible +import com.google.android.material.textfield.TextInputEditText +import com.google.android.material.textfield.TextInputLayout import com.kunzisoft.keepass.R + class TextSelectFieldView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) @@ -28,30 +34,54 @@ class TextSelectFieldView @JvmOverloads constructor(context: Context, private var actionImageButtonId = ViewCompat.generateViewId() private var mDefaultPosition = 0 - private val labelView = AppCompatTextView(context).apply { - setTextAppearance(context, R.style.KeepassDXStyle_TextAppearance_LabelTextStyle) + private val labelView = TextInputLayout(context).apply { layoutParams = LayoutParams( - LayoutParams.MATCH_PARENT, - LayoutParams.WRAP_CONTENT).also { - val leftMargin = 4f - it.leftMargin = TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, - leftMargin, - resources.displayMetrics - ).toInt() - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - it.marginStart = TypedValue.applyDimension( - TypedValue.COMPLEX_UNIT_DIP, - leftMargin, - resources.displayMetrics - ).toInt() - } + LayoutParams.MATCH_PARENT, + LayoutParams.WRAP_CONTENT) + } + private val valueView = TextInputEditText( + ContextThemeWrapper(context, + R.style.KeepassDXStyle_TextInputLayout) + ).apply { + layoutParams = LinearLayout.LayoutParams( + LayoutParams.MATCH_PARENT, + LayoutParams.WRAP_CONTENT) + inputType = EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + imeOptions = EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING + importantForAutofill = IMPORTANT_FOR_AUTOFILL_NO } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO + } + val drawable = ContextCompat.getDrawable(context, R.drawable.ic_arrow_down_white_24dp) + ?.apply { + mutate().colorFilter = BlendModeColorFilterCompat + .createBlendModeColorFilterCompat(currentTextColor, BlendModeCompat.SRC_IN) + } + setCompoundDrawablesWithIntrinsicBounds( + null, + null, + drawable, + null + ) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + setCompoundDrawablesRelativeWithIntrinsicBounds( + null, + null, + drawable, + null + ) + } + isFocusable = false + inputType = InputType.TYPE_NULL + maxLines = 1 } private val valueSpinnerView = Spinner(context).apply { layoutParams = LayoutParams( - LayoutParams.MATCH_PARENT, - LayoutParams.WRAP_CONTENT) + 0, + 0 + ) } private var actionImageButton = AppCompatImageButton( ContextThemeWrapper(context, R.style.KeepassDXStyle_ImageButton_Simple), null, 0).apply { @@ -75,15 +105,22 @@ class TextSelectFieldView @JvmOverloads constructor(context: Context, init { // Manually write view to avoid view id bugs buildViews() + labelView.addView(valueView) addView(labelView) addView(valueSpinnerView) addView(actionImageButton) + valueView.apply { + setOnClickListener { + valueSpinnerView.performClick() + } + } valueSpinnerView.apply { adapter = valueSpinnerAdapter onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { - valueSpinnerAdapter.getItem(position) + val stringValue = valueSpinnerAdapter.getItem(position) + valueView.setText(stringValue) } override fun onNothingSelected(parent: AdapterView<*>?) {} } diff --git a/app/src/main/res/layout/item_select_field_value.xml b/app/src/main/res/layout/item_select_field_value.xml index 35c624640..82d57223e 100644 --- a/app/src/main/res/layout/item_select_field_value.xml +++ b/app/src/main/res/layout/item_select_field_value.xml @@ -27,10 +27,10 @@ android:ellipsize="end" android:gravity="center_vertical" android:maxLines="2" - android:paddingStart="6dp" - android:paddingLeft="6dp" - android:paddingEnd="6dp" - android:paddingRight="6dp" + android:paddingStart="@dimen/default_margin" + android:paddingLeft="@dimen/default_margin" + android:paddingEnd="@dimen/default_margin" + android:paddingRight="@dimen/default_margin" style="@style/KeepassDXStyle.TextAppearance.Large" tools:text="Test" />