EntryEditFieldView in manual configuration to avoid view id bugs

This commit is contained in:
J-Jamet
2021-05-15 14:55:24 +02:00
parent 9c2fd26579
commit d107beadf2
2 changed files with 66 additions and 81 deletions

View File

@@ -2,33 +2,80 @@ package com.kunzisoft.keepass.view
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.util.TypedValue
import android.view.ContextThemeWrapper
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.RelativeLayout
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.AppCompatImageButton
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.kunzisoft.keepass.R
class EntryEditFieldView @JvmOverloads constructor(context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0)
: LinearLayout(context, attrs, defStyle) {
: RelativeLayout(context, attrs, defStyle) {
private val labelView: TextInputLayout
private val valueView: TextView
private var actionImageButton: ImageButton? = null
private val labelViewId = ViewCompat.generateViewId()
private val valueViewId = ViewCompat.generateViewId()
private val actionImageButtonId = ViewCompat.generateViewId()
private val labelView = TextInputLayout(context).apply {
id = labelViewId
layoutParams = LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT).also {
it.addRule(LEFT_OF, actionImageButtonId)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
it.addRule(START_OF, actionImageButtonId)
}
}
}
private val valueView = TextInputEditText(context).apply {
id = valueViewId
layoutParams = LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT)
inputType = EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
imeOptions = EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING
importantForAutofill = IMPORTANT_FOR_AUTOFILL_NO
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO
}
maxLines = 1
}
private var actionImageButton = AppCompatImageButton(
ContextThemeWrapper(context, R.style.KeepassDXStyle_ImageButton_Simple), null, 0).apply {
id = actionImageButtonId
layoutParams = LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT).also {
it.topMargin = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
8f,
resources.displayMetrics
).toInt()
it.addRule(ALIGN_PARENT_RIGHT)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
it.addRule(ALIGN_PARENT_END)
}
}
visibility = View.GONE
contentDescription = context.getString(R.string.menu_edit)
}
init {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater?
inflater?.inflate(R.layout.view_entry_edit_field, this)
labelView = findViewById(R.id.edit_field_text_layout)
valueView = findViewById(R.id.edit_field_text)
actionImageButton = findViewById(R.id.edit_field_action_button)
// Manually write view to avoid view id bugs
labelView.addView(valueView)
addView(labelView)
addView(actionImageButton)
}
fun applyFontVisibility(fontInVisibility: Boolean) {
@@ -36,7 +83,7 @@ class EntryEditFieldView @JvmOverloads constructor(context: Context,
valueView.applyFontVisibility()
}
fun getActionImageView(): View? {
fun getActionImageView(): View {
return actionImageButton
}
@@ -53,7 +100,7 @@ class EntryEditFieldView @JvmOverloads constructor(context: Context,
return valueView.text?.toString() ?: ""
}
set(value) {
valueView.text = value
valueView.setText(value)
}
fun setValue(value: String?, valueType: TextType) {
@@ -68,7 +115,7 @@ class EntryEditFieldView @JvmOverloads constructor(context: Context,
valueView.maxLines = 40
}
}
valueView.text = value ?: ""
valueView.setText(value ?: "")
}
fun setProtection(protection: Boolean) {
@@ -81,10 +128,10 @@ class EntryEditFieldView @JvmOverloads constructor(context: Context,
fun setOnActionClickListener(onActionClickListener: OnClickListener? = null,
@DrawableRes actionImageId: Int? = null) {
actionImageId?.let {
actionImageButton?.setImageDrawable(ContextCompat.getDrawable(context, it))
actionImageButton.setImageDrawable(ContextCompat.getDrawable(context, it))
}
actionImageButton?.setOnClickListener(onActionClickListener)
actionImageButton?.visibility = if (onActionClickListener == null) View.GONE else View.VISIBLE
actionImageButton.setOnClickListener(onActionClickListener)
actionImageButton.visibility = if (onActionClickListener == null) View.GONE else View.VISIBLE
}
enum class TextType {

View File

@@ -1,62 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2019 Jeremy Jamet / Kunzisoft.
This file is part of KeePassDX.
KeePassDX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KeePassDX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:targetApi="o">
<!-- slowdown with constraint layout -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/edit_field_text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/edit_field_action_button"
android:layout_toStartOf="@+id/edit_field_action_button">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edit_field_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions"
android:imeOptions="flagNoPersonalizedLearning"
android:maxLines="1"
android:focusable="true"
android:focusableInTouchMode="true"
android:importantForAccessibility="no"
android:importantForAutofill="no"
tools:hint="@string/entry_title"
tools:targetApi="jelly_bean" />
</com.google.android.material.textfield.TextInputLayout>
<androidx.appcompat.widget.AppCompatImageButton
android:id="@+id/edit_field_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:visibility="gone"
android:src="@drawable/ic_more_white_24dp"
android:contentDescription="@string/menu_edit"
style="@style/KeepassDXStyle.ImageButton.Simple"/>
</RelativeLayout>