I\'m making an app that extends the PreferenceActivity and I want to add an icon to each Preference.
I read a similar question, and this is the answer with more repu
Thanks to:
The following worked for me:
AndroidManifest.xml
res/values/strings.xml:
YOUR_APP_NAME
About
res/values/attrs.xml:
res/layout/main.xml:
res/layout/preference_icon.xml:
Appropriately sized icons under:
res/drawable-hdpi/YOUR_ICON.png
res/drawable-mdpi/YOUR_ICON.png
res/drawable-ldpi/YOUR_ICON.png
AndroidMain.scala:
class AndroidMain extends PreferenceActivity {
override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
addPreferencesFromResource(R.layout.main)
}
}
IconPreference.scala:
class IconPreference(context: Context, attrs: AttributeSet, defStyle: Int) extends Preference(context, attrs, defStyle) {
def this(context: Context, attrs: AttributeSet) = this(context, attrs, 0)
setLayoutResource(R.layout.preference_icon);
val a: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.IconPreference, defStyle, 0);
val _icon: Drawable = a.getDrawable(R.styleable.IconPreference_icon);
override def onBindView(view: View) {
super.onBindView(view)
val imageView: ImageView = view.findViewById(R.id.icon).asInstanceOf[ImageView]
if (imageView != null && _icon != null) {
imageView.setImageDrawable(_icon);
}
}
}