Creating an instance of an inner class (Preference) in an XML file

If you want to access a custom view in some layout.xml file, you have two options:

  • The view has its own class. Then you do <package.name.MyView android:layout_width= ... />
  • A view is an inner class: <view class="package.name.OuterClass$MyView" android:layout_width= ... />

Now I want to do the same inside the <PreferenceScreen> . The first method works well, but I would like to put all the custom Preference classes in my PreferenceActivity. I tried <Preference class="package.name.OuterClass$MyPreference" ... /> (also with '.' Instead of '$') as well as <package.name.OuterClass.MyPreference ... /> , but both failed.

Does anyone have any ideas?

+9
android inner-classes android-xml preferencescreen
Aug 30 '11 at 19:56
source share
1 answer

When working with blowing, Views LayoutInflater searches for the case “view” → “class”:

 View createViewFromTag(View parent, String name, AttributeSet attrs) { if (name.equals("view")) { name = attrs.getAttributeValue(null, "class"); } ... 

Preference PreferenceInflater does not mean that this applies to the case of "class".

It uses reflection in its createItem() method and probably why the first case works for you.

+2
Jul 08 '13 at 22:06
source share



All Articles