Android - DataBinding with Preference activity
Asked Answered
S

2

22

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
Scotopia answered 16/1, 2016 at 2:49 Comment(4)
Hadn't you solved this issue yet?Luht
i could not get databinding to work with sharedpreferences so i re-worked my code.Scotopia
Can you please answer your own question with work-around solution, because I'm facing the same issue. Thanks!Luht
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.Scotopia
T
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.

Trichomoniasis answered 22/8, 2018 at 7:55 Comment(0)
P
6

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.

Padgett answered 17/4, 2021 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.