Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data binding not working on Tab Item text attribute

I have recently started learning data binding and so far it's working really well. But I've encountered a problem binding data to a tab item as follows (see the text attribute in the first tab item):

<com.google.android.material.tabs.TabLayout
                    android:id="@+id/tabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginTop="20dp"
                    style="@style/tab_bar"
                    android:background="@drawable/comments_divider_background"
                    app:tabPaddingBottom="0dp"
                    app:tabPaddingTop="0dp"
                    android:clipToPadding="true"
                    app:tabIndicatorHeight="3dp">

                    <com.google.android.material.tabs.TabItem
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        **android:text="@{viewmodel.signedInUser.username}"**
                        android:id="@+id/tabItem1" />

                    <com.google.android.material.tabs.TabItem
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Members"
                        android:id="@+id/tabItem2"/>

                </com.google.android.material.tabs.TabLayout>

Upon building I get the following error:

Cannot find a setter for <com.google.android.material.tabs.TabItem android:text> that accepts parameter type 'java.lang.String'

Data binding seems to be working well for my other views but not for tab items. What could be the problem?

Edit:

Here's part of my onCreateView:

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    binding = FragmentViewIncidentsBinding.inflate(inflater,container,false);
    binding.setLifecycleOwner(getActivity());

//   

    View view = binding.getRoot();
    return view;

In onActivityCreated:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    viewModel = ViewModelProviders.of(getActivity()).get(MyViewModel.class);
    binding.setViewmodel(viewModel);
like image 979
ericgithinji Avatar asked Mar 01 '26 04:03

ericgithinji


1 Answers

I found a workaround by using a binding adapter in my handler class. I pass the entire tablayout as a parameter (not an individual tabitem), together with whatever string I want to set as the text for the tabitem.

In the handler class:

@BindingAdapter("tabText1")
public static void setTabText1(TabLayout tabLayout, String text){
    tabLayout.getTabAt(0).setText("Title is "+text+")");
}

//can add more adapters for more tabitems here

And in the binding expression:

<com.google.android.material.tabs.TabLayout
                    android:id="@+id/tabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginTop="20dp"
                    style="@style/tab_bar"
                    android:background="@drawable/divider_background"
                    app:tabPaddingBottom="0dp"
                    app:tabPaddingTop="0dp"
                    android:clipToPadding="true"
                    app:tabIndicatorHeight="3dp"
                    app:tabText1="@{someobject.somevalue}">

                    <com.google.android.material.tabs.TabItem
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=""
                        android:id="@+id/tabItem1" />

                    <!-- can add more tabitems here -->

                </com.google.android.material.tabs.TabLayout>
like image 193
ericgithinji Avatar answered Mar 03 '26 19:03

ericgithinji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!