Tapan parmar Tapan parmar - 4 months ago 60
Android Question

Why android data binding library is not working as expected?

I am new to concept of Android Data Binding, i have read it from this blog and it is working properly with simple examples they had provided , I have taken a blank activity so there will be two files

activity_main.xml
and
content_main.xml



This is
activity_main.xml
code




<android.support.design.widget.CoordinatorLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activity.MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/stat_notify_sync_noanim" />

</android.support.design.widget.CoordinatorLayout>
</layout>


As suggested i have taken parent as layout, and from java side i am accessing it as follows


public class MainActivity extends AppCompatActivity {

ActivityMainBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);


setSupportActionBar(activityMainBinding.toolbar);
//Floating Action Button
activityMainBinding.fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

}

}


This is ok and i can access all my views under
activity_main.xml
but for accessing contents inside
content_main.xml
i have done this in
MainActivity.java



ContentMainBinding contentMainBinding = DataBindingUtil.inflate(inflater,R.layout.content_main,clActivityMain,true);
contentMainBinding.tvHello.setText("yippee"); <br/>


Problem is that it can not change the default text value provided from xml side in
content_main.xml
EX :
<android:Text = "Hello World!">
and shows me both values(Provided in xml layout + Which i have set from java side) I have already read it in Developer Documentation but its so much different then this blog i have read. Is there any proper way for this? As developer documentation or mentioned as in this Blog ?

Answer

You might check out:

https://medium.com/google-developers/android-data-binding-that-include-thing-1c8791dd6038#.p7nhkxhdt

You don't need to inflate that included layout, but you should give the include an ID if you want to access the included layout's binding:

<include android:id="@+id/contentMain" layout="@layout/content_main" />

Now, you can access the content like this:

binding.contentMain.tvHello.setText("yippee");
Comments