I have a main activity with a side navigation drawer in the action bar, specified as follows (note that a lot of code has been omitted for brevity) in default_screen.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="190dp" android:background="@drawable/honeycomb" android:orientation="vertical" > <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_gravity="start" app:headerLayout="@layout/header" app:menu="@menu/drawer" />
where layout/header is as follows (again, a bunch of lines omitted for brevity):
<data> <variable name="user" type="oose2017.place2b.ClientUser"/> </data> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{user.displayName}" android:textSize="14sp" android:textColor="#FFF" android:textStyle="bold" android:gravity="left" android:paddingBottom="4dp" android:id="@+id/username" android:layout_above="@+id/email" android:layout_alignLeft="@+id/profile_image" android:layout_alignStart="@+id/profile_image" /> </RelativeLayout>
Where I instantiate my default_screen in the main activity as follows:
setContentView(R.layout.default_screen);
How do I data bind to the header? I have tried a few things unsuccessfully, mainly:
DefaultScreenBinding binding = DataBindingUtil.setContentView(R.layout.default_screen);
Which does not work. How can I do this?
Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change. For example, if the user edits the value in a TextBox element, the underlying data value is automatically updated to reflect that change.
Data Binding in <include> Layouts xml layout to enable data binding. The <data> and <variable> tags are used to bind the student object. To pass the user to included content_student_main layout, bind:student=”@{student}” is used. Without this, the user object won't be accessible in content_student_main layout.
Valid solution will be following, add header view in main activity OnCreate:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_main); NavHeaderMainBinding _bind = DataBindingUtil.inflate(getLayoutInflater(), R.layout.nav_header_main, binding .navView, false); binding.navView.addHeaderView(_bind.getRoot()); _bind.setUser(Session.getUserProfile()); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With