Search code examples
androidpreferences

How to change text size of dialog preference message?


I have a class that extends EditTextPreference and I use it in my own layout, such as:

    <com.app.MyEditTextPreference
        android:key="@string/key"
        android:title="@string/title"
        android:summary="@string/summary"
        android:dialogTitle="@string/title"
        android:dialogMessage="@string/message"
        android:layout="@layout/preference_edittext"/>

My app theme inherits from Theme.AppCompat.Light.DarkActionBar and I have changed the values of the text sizes:

<style name="TextAppearance.Small">
    <item name="android:textSize">18sp</item> <!-- was 14sp -->
</style>

However, when the preference dialog is displayed, the size of the message is different than the one I have defined in my style.

So how do I set the text size of the dialog message correctly?

EDIT

I copied the dialog layout from the sdk:

<LinearLayout
    android:id="@+android:id/edittext_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:orientation="vertical">

    <TextView
        android:id="@android:id/message"
        android:layout_marginBottom="48dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingStart="4dp"
        android:paddingEnd="4dp"
        android:textColor="?android:attr/textColorSecondary"
        style="@style/TextAppearance.Small" />

</LinearLayout>

However, I get a build error Resource is not public on the line android:id="@+android:id/edittext_container".

And if I change it to "@*android:id/edittext_container", the edit text is not visible anymore.

Is there a simpler way to just change the textsize of the message?


Solution

  • I finally managed to change the message text size by:

    a) Changing the id of the LinearLayout to: @+id/edittext_container, and

    b) Adding the following code in my class extending EditTextPreference:

    @Override
    protected void onAddEditTextToDialogView(View dialogView, EditText editText) {
        ViewGroup container = (ViewGroup) dialogView
                .findViewById(R.id.edittext_container);
    
        if (container != null) {
            container.addView(editText, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }
    

    This works fine and is clearly a valid answer, however a simpler solution would be to just change the message text size through themeing.