bigdestroyer bigdestroyer - 1 month ago 6
Android Question

Enable/disable EditText based on whether adapter has items

I'm using Data Binding Library. I have this in the XML:

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="@{!myAdapter.isEmpty}"/>


I want that EditText to be enabled only when Spinner adapter is not empty. When app starts, EditText is disabled. So far, so good.

Then, in my activity, items are inserted in the adapter. But after:

myAdapter.notifyDataSetChanged();


EditText is not enabled. Do I have to do anything more?

Answer

To have an ability to notify data binding about something you could use ObservableBoolean like this:

...
<variable name="isAdapterEmpty"
          type="android.databinding.ObservableBoolean" />
...
<EditText
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:enabled="@{!isAdapterEmpty}"/>

And then notify databinding about changes like this:

myAdapter.notifyDataSetChanged();
isAdapterEmpty.set(myAdapter.isEmpty());

Or you can make your own method inside your adapter that will return ObservableBoolean instead of simple boolean and provide ability to manage this value to adapter.

Comments