[Android] Add custom EditText dialog for TV

Robert Stone git at videolan.org
Wed Apr 6 13:51:02 UTC 2022


vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Sun Apr  3 08:02:24 2022 -0700| [2eb4b5a2c31070671b4ab49783d959b3f8cc81a9] | committer: Nicolas Pomepuy

Add custom EditText dialog for TV

> https://code.videolan.org/videolan/vlc-android/commit/2eb4b5a2c31070671b4ab49783d959b3f8cc81a9
---

 .../ui/preferences/BasePreferenceFragment.kt       | 16 ++++++
 .../CustomEditTextPreferenceDialogFragment.kt      | 60 ++++++++++++++++++++++
 2 files changed, 76 insertions(+)

diff --git a/application/television/src/main/java/org/videolan/television/ui/preferences/BasePreferenceFragment.kt b/application/television/src/main/java/org/videolan/television/ui/preferences/BasePreferenceFragment.kt
index 3c7b17496..f70fa49bc 100644
--- a/application/television/src/main/java/org/videolan/television/ui/preferences/BasePreferenceFragment.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/preferences/BasePreferenceFragment.kt
@@ -26,6 +26,9 @@ package org.videolan.television.ui.preferences
 import android.app.Fragment
 import android.os.Bundle
 import androidx.leanback.preference.LeanbackPreferenceFragment
+import androidx.preference.EditTextPreference
+import androidx.preference.Preference
+import androidx.preference.PreferenceDialogFragment
 
 import org.videolan.vlc.R
 
@@ -43,4 +46,17 @@ abstract class BasePreferenceFragment : LeanbackPreferenceFragment() {
                 .addToBackStack("main")
                 .commit()
     }
+
+    protected fun buildPreferenceDialogFragment(preference: Preference): PreferenceDialogFragment? {
+        return if (preference is EditTextPreference) {
+            CustomEditTextPreferenceDialogFragment.newInstance(preference.getKey()).apply {
+                setTargetFragment(this at BasePreferenceFragment, 0)
+                show(this at BasePreferenceFragment.fragmentManager, DIALOG_FRAGMENT_TAG)
+            }
+        } else null
+    }
+
+    companion object {
+        private const val DIALOG_FRAGMENT_TAG = "androidx.preference.PreferenceFragment.DIALOG"
+    }
 }
diff --git a/application/television/src/main/java/org/videolan/television/ui/preferences/CustomEditTextPreferenceDialogFragment.kt b/application/television/src/main/java/org/videolan/television/ui/preferences/CustomEditTextPreferenceDialogFragment.kt
new file mode 100644
index 000000000..d4897dede
--- /dev/null
+++ b/application/television/src/main/java/org/videolan/television/ui/preferences/CustomEditTextPreferenceDialogFragment.kt
@@ -0,0 +1,60 @@
+/*
+ * *************************************************************************
+ *  CustomEditTextPreferenceDialogFragment.kt
+ * **************************************************************************
+ *  Copyright © 2022 VLC authors and VideoLAN
+ *
+ *  This program 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 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *  ***************************************************************************
+ */
+
+package org.videolan.television.ui.preferences
+
+import android.R
+import android.text.InputFilter
+import android.text.InputType
+import android.view.View
+import android.widget.EditText
+import androidx.core.os.bundleOf
+import androidx.preference.EditTextPreferenceDialogFragment
+
+class CustomEditTextPreferenceDialogFragment : EditTextPreferenceDialogFragment() {
+
+    private var customFilters = emptyArray<InputFilter>()
+    private var customInputType = InputType.TYPE_NULL
+
+    companion object {
+        fun newInstance(key: String?) =
+            CustomEditTextPreferenceDialogFragment().apply {
+                arguments = bundleOf(ARG_KEY to key)
+            }
+    }
+
+    override fun onBindDialogView(view: View) {
+        view.findViewById<EditText>(R.id.edit).apply {
+            if (customFilters.isNotEmpty()) filters = customFilters
+            if (customInputType != InputType.TYPE_NULL) inputType = customInputType
+        }
+        super.onBindDialogView(view)
+    }
+
+    fun setFilters(filters: Array<InputFilter>) {
+        this.customFilters = filters
+    }
+
+    fun setInputType(inputType: Int) {
+        this.customInputType = inputType
+    }
+}
\ No newline at end of file



More information about the Android mailing list