Changing the Title and EditText color in androidx.preference.EditTextPreference

596 views Asked by At

I have found some similar questions on this topic but it works for support version and not on androidx. I wasn't able to make this change on androidx. But on the previous support version there are solutions for it already. Can anyone give me some directions on how I can change the title and editetext field text color in EditTextPreference? Currently the color is white and with the background being white too, the texts are not visible.

enter image description here

And here are the solutions I found on stackoverflow already but I wasn't able to use.I would like to change the white text color to black. Thank you.

2

There are 2 answers

0
AndroidNewbieDarmstadt On BEST ANSWER

In the thread of Murphybro2's solution we got also the solution for androidx https://stackoverflow.com/a/58836953

You can change the AlertDialogStyle used by your app by providing a custom Theme style

<style name="AppTheme.Settings">
     <!-- This will change the opening dialogs for EditTextPreference -->
     <item name="alertDialogStyle">@style/Theme.example.AlertTheme</item>
</style>

<style name="Theme.example.AlertTheme" parent="">
    <item name="android:textViewStyle">@style/Theme.AlertTextStyle</item>
    <item name="android:textColor">@color/pink</item>
    <item name="android:editTextBackground">@color/pink</item>
    <item name="android:windowMinWidthMajor">@dimen/abc_dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@dimen/abc_dialog_min_width_minor</item>
    <item name="android:windowIsFloating">true</item>
</style>

<style name="Theme.AlertTextStyle" parent="Theme.example">
        <item name="android:textColorPrimary">@color/gray</item>
</style>

The most important one for me was "windowIsFloating">true because else the window is stuck to the top of your screen For me somehow only adding the extra textViewStyle helped to change the top text inside the dialog, but I think its because of my example theme.

0
lukasz.nowak On

I was able to change the color of the title of the dialog box created by the androidx.preference.EditTextPreference in the way presented in that answer. But I could not change the color of the text in the EditText widget placed in that dialog in that way and I needed to use the following method. AndroidX version of EditTextPreference allows you to set a listener on the event of binding the EditText. You can use it for example in onCreatePreferences of the androidx.preference.PreferenceFragmentCompat class to change the attributes of the EditText widget.

override fun onCreatePreferences() {
    super.onCreatePreferences()
        findPreference<EditTextPreference>("my_preference_key")?.let {
            it.setOnBindEditTextListener { editText ->
                editText.setTextColor(Color.BLACK)
            }
        }
    }
}