1

I have a Preference screen and also two EditTextPreference.

I want to change its background color and font and also to add an image in its background.

How can I do that?

Here it the code as shown below for the layout in XML:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory 
        android:title="Login Information"
        android:key="first_category">
        <EditTextPreference
            android:key="@string/txtusername"
            android:title="@string/username" 
            android:summary="@string/userNameSummary" />
    </PreferenceCategory>

    <PreferenceCategory
        android:title="Device Information"
        android:key="Device">
        <EditTextPreference
            android:key="welcome_message"
            android:title="DeviceId"
            android:summary="This is your mobile device ID"
            android:shouldDisableView="true" />
    </PreferenceCategory>
</PreferenceScreen>
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
saqibabbasi
  • 423
  • 1
  • 5
  • 9

1 Answers1

2

Actually it looks like you may be stuck using styles and themes in the XML for preferences.

You can set it differently in code though. After you create the preference, you will need to access its view and set the desired changes.

  • To change the background image use the following, where ... is a drawable.

getView().setBackgroundDrawable(...);

  • To change background color use the following, where ... is a color.

getView().setBackgroundColor(...);

  • To change the font use the following, where ... is a font type.

getView().setTypeFace(...);

  • To change font color use the following, where ... is a color.

getView().setTextColor(...);

prolink007
  • 33,872
  • 24
  • 117
  • 185
  • 1
    Did not know you can customize preference screens - TIL! +1 from me on that! But then again, its best to stick to familiar preference screen layouts a lá Menu > Settings to keep consistency. – t0mm13b Jul 18 '12 at 22:24
  • i want to preference screen same as my other app screen.want to add background image and change font – saqibabbasi Jul 18 '12 at 22:36
  • in my java class which is extended from Preference Activity,do not get getview.setbackground – saqibabbasi Jul 18 '12 at 22:43
  • What i have in my answer should help you out. – prolink007 Jul 18 '12 at 22:44
  • `getView()` above is arbitrary. It will change depending on what type of `View` you are getting. If you are in a `PreferenceActivity` you may need to use `findViewById(...).setBackgroundDrawable(...)`. Where the `...` in `findViewById(...)` is the resource id of the `View` you are trying to change. – prolink007 Jul 18 '12 at 22:46