Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - DataBinding with Preference activity

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
like image 572
j2emanue Avatar asked Jan 16 '16 02:01

j2emanue


People also ask

What is 2 way binding in Android?

Stay organized with collections Save and categorize content based on your preferences. The @={} notation, which importantly includes the "=" sign, receives data changes to the property and listen to user updates at the same time. // Avoids infinite loops.

Which method is called for binding the user interface with the activity code file?

The bind method is the most complex code in the generated binding object, with a call to findViewById for each view to bind.


2 Answers

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.

like image 59
Andrei Vinogradov Avatar answered Oct 24 '22 18:10

Andrei Vinogradov


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.

like image 32
user13239021 Avatar answered Oct 24 '22 19:10

user13239021