views:

514

answers:

2

I would like some of my preferences to have icons, like the Settings app.

I guess one way of doing this would be to copy all the relevant code and resources from the Settings app, but it seems like overkill for a couple of icons.

Also I don't like the idea of having to duplicate the code and resources in each project that requires settings icons.

Has anyone solved this already with a simpler or resource-free approach?

+1  A: 

The Settings application uses a private custom PreferenceScreen subclass to have the icon -- IconPreferenceScreen. It is 51 lines of code, including the comments, though it also requires some custom attributes. The simplest option is to clone all of that into your project, even though you do not like that.

CommonsWare
If only resources could be shared between projects. I kind of hoped someone solved this already and wrapped it in a nice class without any external resources.
hgpc
It should be noted that the Settings app approach extends Preference, not PreferenceScreen and that it also requires a custom layout to work.
hgpc
A: 

I have imported all of the necessary code, but on

public IconPreferenceScreen(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setLayoutResource(R.layout.preference_icon); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconPreferenceScreen, defStyle, 0); mIcon = a.getDrawable(R.styleable.IconPreferenceScreen_icon);

}

i get errors on the R.styleable.IconPreferenceScreen and R.styleable.IconPreferenceScreen_icon, but cant find any reference to those in the settings app or anywhere in the source code using grep to search. Any ideas? I would love to use this wrapper too

roger l
Check in attrs.xml of the Settings app.
hgpc