21

I set up the data binding in xml but when i go to the preference activity to actually set up the binding i cant. The file where the data binding xml is in is called preference.xml so i assume there should be a generated class called PreferencesBinding but instead there is not. Should it not be auto generated ? Also my code in preferenceActivity.java wont compile the following:

    Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         PreferencesBinding binding = DataBindingUtil..... //what do put here ??? this is preferenceactivity not Activity

         Admin user = new Admin(true);
         binding.setUser(user);//this what i want to do

         addPreferencesFromResource(R.xml.preferences);//this would not be necessary if i can get dataBinding to work
//..rest of code not important
       }

i am starting to think data binding does not work for preference screens, can anyone help ?

the preferences.xml file if neseccary looks like this:

 <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:customNS="http://schemas.android.com/apk/res/mypackage"
    >
    <data>
        <variable name="admin" type="mypackage.Administration"/>
    </data>

<PreferenceScreen>

<!-- ....bunch of preferences ... -->
</PreferenceScreen>
</layout
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Hadn't you solved this issue yet? – Yurii Tsap Nov 08 '16 at 14:24
  • i could not get databinding to work with sharedpreferences so i re-worked my code. – j2emanue Nov 08 '16 at 14:29
  • 1
    Can you please answer your own question with work-around solution, because I'm facing the same issue. Thanks! – Yurii Tsap Nov 08 '16 at 14:30
  • 2
    i went back to that project to check what i did. i simply did not use databinding for sharedPreferences. i had no way to make the xml preference file talk to the preferenceactivity so i just update the view in code. sorry cant help more, wish i could. – j2emanue Nov 08 '16 at 16:34

2 Answers2

10

DataBinding for PreferenceScreen is not supported at the moment. It's cause of this xml is not layout xml. Also preference elements are not extending View or ViewGroup at all. Maybe this feature will be introduced at future, but not now. Only layout files can use DataBinding.

Andrei Vinogradov
  • 1,865
  • 15
  • 32
4

DataBinding for PreferenceScreen is still not supported. Android development is really inconsistent. Google tells us to use these new better technologies, but then you hit a brick-wall and have to do it the old way. You might as well just stuck with the old way all along.