Most visited

Recently visited

DataBindingUtil

public class DataBindingUtil
extends Object

java.lang.Object
   ↳ android.databinding.DataBindingUtil


Utility class to create ViewDataBinding from layouts.

Summary

Public methods

static <T extends ViewDataBinding> T bind(View root)

Returns the binding for the given layout root or creates a binding if one does not exist.

static <T extends ViewDataBinding> T bind(View root, DataBindingComponent bindingComponent)

Returns the binding for the given layout root or creates a binding if one does not exist.

static String convertBrIdToString(int id)

Converts the given BR id to its string representation which might be useful for logging purposes.

static <T extends ViewDataBinding> T findBinding(View view)

Retrieves the binding responsible for the given View.

static <T extends ViewDataBinding> T getBinding(View view)

Retrieves the binding responsible for the given View layout root.

static DataBindingComponent getDefaultComponent()

Returns the default DataBindingComponent used in data binding.

static <T extends ViewDataBinding> T inflate(LayoutInflater inflater, int layoutId, ViewGroup parent, boolean attachToParent, DataBindingComponent bindingComponent)

Inflates a binding layout and returns the newly-created binding for that layout.

static <T extends ViewDataBinding> T inflate(LayoutInflater inflater, int layoutId, ViewGroup parent, boolean attachToParent)

Inflates a binding layout and returns the newly-created binding for that layout.

static <T extends ViewDataBinding> T setContentView(Activity activity, int layoutId)

Set the Activity's content view to the given layout and return the associated binding.

static <T extends ViewDataBinding> T setContentView(Activity activity, int layoutId, DataBindingComponent bindingComponent)

Set the Activity's content view to the given layout and return the associated binding.

static void setDefaultComponent(DataBindingComponent bindingComponent)

Set the default DataBindingComponent to use for data binding.

Inherited methods

From class java.lang.Object

Public methods

bind

T bind (View root)

Returns the binding for the given layout root or creates a binding if one does not exist. This uses the DataBindingComponent set in setDefaultComponent(DataBindingComponent).

Prefer using the generated Binding's bind method to ensure type-safe inflation when it is known that root has not yet been bound.

Parameters
root View: The root View of the inflated binding layout.
Returns
T A ViewDataBinding for the given root View. If one already exists, the existing one will be returned.
Throws
IllegalArgumentException when root is not from an inflated binding layout.

See also:

bind

T bind (View root, 
                DataBindingComponent bindingComponent)

Returns the binding for the given layout root or creates a binding if one does not exist.

Prefer using the generated Binding's bind method to ensure type-safe inflation when it is known that root has not yet been bound.

Parameters
root View: The root View of the inflated binding layout.
bindingComponent DataBindingComponent: The DataBindingComponent to use in data binding.
Returns
T A ViewDataBinding for the given root View. If one already exists, the existing one will be returned.
Throws
IllegalArgumentException when root is not from an inflated binding layout.

See also:

convertBrIdToString

String convertBrIdToString (int id)

Converts the given BR id to its string representation which might be useful for logging purposes.

Parameters
id int: The integer id, which should be a field from BR class.
Returns
String The name if the BR id or null if id is out of bounds.

findBinding

T findBinding (View view)

Retrieves the binding responsible for the given View. If view is not a binding layout root, its parents will be searched for the binding. If there is no binding, null will be returned.

This differs from getBinding(View) in that findBinding takes any view in the layout and searches for the binding associated with the root. getBinding takes only the root view.

Parameters
view View: A View in the bound layout.
Returns
T The ViewDataBinding associated with the given view or null if view is not part of a bound layout.

getBinding

T getBinding (View view)

Retrieves the binding responsible for the given View layout root. If there is no binding, null will be returned. This uses the DataBindingComponent set in setDefaultComponent(DataBindingComponent).

Parameters
view View: The root View in the layout with binding.
Returns
T The ViewDataBinding associated with the given view or null if either the view is not a root View for a layout or view hasn't been bound.

getDefaultComponent

DataBindingComponent getDefaultComponent ()

Returns the default DataBindingComponent used in data binding. This can be null if no default was set in setDefaultComponent(DataBindingComponent).

Returns
DataBindingComponent the default DataBindingComponent used in data binding. This can be null if no default was set in setDefaultComponent(DataBindingComponent).

inflate

T inflate (LayoutInflater inflater, 
                int layoutId, 
                ViewGroup parent, 
                boolean attachToParent, 
                DataBindingComponent bindingComponent)

Inflates a binding layout and returns the newly-created binding for that layout.

Use this version only if layoutId is unknown in advance. Otherwise, use the generated Binding's inflate method to ensure type-safe inflation.

Parameters
inflater LayoutInflater: The LayoutInflater used to inflate the binding layout.
layoutId int: The layout resource ID of the layout to inflate.
parent ViewGroup: Optional view to be the parent of the generated hierarchy (if attachToParent is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToParent is false.)
attachToParent boolean: Whether the inflated hierarchy should be attached to the parent parameter. If false, parent is only used to create the correct subclass of LayoutParams for the root view in the XML.
bindingComponent DataBindingComponent: The DataBindingComponent to use in the binding.
Returns
T The newly-created binding for the inflated layout or null if the layoutId wasn't for a binding layout.
Throws
InflateException When a merge layout was used and attachToParent was false.

inflate

T inflate (LayoutInflater inflater, 
                int layoutId, 
                ViewGroup parent, 
                boolean attachToParent)

Inflates a binding layout and returns the newly-created binding for that layout. This uses the DataBindingComponent set in setDefaultComponent(DataBindingComponent).

Use this version only if layoutId is unknown in advance. Otherwise, use the generated Binding's inflate method to ensure type-safe inflation.

Parameters
inflater LayoutInflater: The LayoutInflater used to inflate the binding layout.
layoutId int: The layout resource ID of the layout to inflate.
parent ViewGroup: Optional view to be the parent of the generated hierarchy (if attachToParent is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToParent is false.)
attachToParent boolean: Whether the inflated hierarchy should be attached to the parent parameter. If false, parent is only used to create the correct subclass of LayoutParams for the root view in the XML.
Returns
T The newly-created binding for the inflated layout or null if the layoutId wasn't for a binding layout.
Throws
InflateException When a merge layout was used and attachToParent was false.

See also:

setContentView

T setContentView (Activity activity, 
                int layoutId)

Set the Activity's content view to the given layout and return the associated binding. The given layout resource must not be a merge layout.

Parameters
activity Activity: The Activity whose content View should change.
layoutId int: The resource ID of the layout to be inflated, bound, and set as the Activity's content.
Returns
T The binding associated with the inflated content view.

setContentView

T setContentView (Activity activity, 
                int layoutId, 
                DataBindingComponent bindingComponent)

Set the Activity's content view to the given layout and return the associated binding. The given layout resource must not be a merge layout.

Parameters
activity Activity: The Activity whose content View should change.
layoutId int: The resource ID of the layout to be inflated, bound, and set as the Activity's content.
bindingComponent DataBindingComponent: The DataBindingComponent to use in data binding.
Returns
T The binding associated with the inflated content view.

setDefaultComponent

void setDefaultComponent (DataBindingComponent bindingComponent)

Set the default DataBindingComponent to use for data binding.

bindingComponent may be passed as the first parameter of binding adapters.

When instance method BindingAdapters are used, the class instance for the binding adapter is retrieved from the DataBindingComponent.

Parameters
bindingComponent DataBindingComponent

Hooray!