Android dataBinding - unable to notify view of changes
<p>i am trying to use databinding for a purpose. I have a project i've created that starts a countdown timer. If the time is a prime number it should update a TextView in a xml layout. The timer works fine but the textview never gets updated. </p>

<p>here is my timer which works fine:</p>

<pre><code>public class MyCountDownTimer {
private CountDownTimer countDownTimer;
private boolean isExecuting =false;
private ICountDownListener listener;
private final String TAG = getClass().getSimpleName();

public MyCountDownTimer(ICountDownListener listener){
this.listener = listener;
}

public void startTimer(long timeLeftMillis) {
if (!isExecuting) {
    isExecuting = true;
    countDownTimer = new CountDownTimer(timeLeftMillis, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
           ;
           if(isPrime(millisUntilFinished/1000)){
            listener.doSomethingWithPrimeCountDown(millisUntilFinished / 1000);

           }
        }

        @Override
        public void onFinish() {
            isExecuting = false;
            listener.doSomethingWithPrimeCountDown(0L);
        }
    };
    countDownTimer.start();
} else {
    Log.i(TAG, "Timer already started");
}
</code></pre>

<p>}m</p>

<pre><code>public void cancelTimer() {
    if (isExecuting) {
        countDownTimer.cancel();
        isExecuting = false;
    }
}

public void restartTimer(Long milli) {
    cancelTimer();
    startTimer(milli);
}

//checks whether an int is prime or not.
boolean  isPrime(Long n) {
    //check if n is a multiple of 2
    if (n%2==0) return false;
    //if not, then just check the odds
    for(int i=3;i*i&lt;=n;i+=2) {
        if(n%i==0)
            return false;
    }
    return true;
}
</code></pre>

<p>}</p>

<p>I have a interface that is used to send the timer ticks out to whoever is listening:</p>

<pre><code>public interface ICountDownListener {
void doSomethingWithPrimeCountDown(Long count);
</code></pre>

<p>}</p>

<p>and the main Activity class where i actually bind to the data looks like this:</p>

<pre><code>public class MainActivity extends FragmentActivity {
CountdownBinder mCountdownBinder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

   mCountdownBinder = DataBindingUtil.setContentView(this, R.layout.activity_main);

    //Lets reference our textview just for fun
    mCountdownBinder.tvGreen.setText("initial text");


     ViewModel vModel = ViewModel.instance();
//now tell databinding about your viewModel below
    mCountdownBinder.setViewModel(viewModel);
    vModel.startCounting(200000L); //start a countdown

}
</code></pre>

<p>}</p>

<p>and here is the very important part the ViewModel i created. Its a singleton class and it extends BaseObservable and implements ICountDownListener so it can listen for ticks:</p>

<pre><code>    package com.databindingexample.mycompany.databindingexample.ViewModels;


import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.BindingAdapter;
import android.graphics.Color;
import android.util.Log;
import android.widget.TextView;

import com.databindingexample.mycompany.databindingexample.Interfaces.ICountDownListener;
import com.databindingexample.mycompany.databindingexample.MyCountDownTimer;

//notice we are subclassing BaseObservable
public class ViewModel extends BaseObservable implements ICountDownListener{

    private static ViewModel instance;
    private long countDownTime;
    private MyCountDownTimer mCountDownTimer;

    //lock the constructor as this is a singleton
    private ViewModel(){
        mCountDownTimer=new MyCountDownTimer(this);

    }

    public static ViewModel instance() {
        if (instance == null) {
            instance = new ViewModel();
        }
        return instance;
    }

    @Bindable
    public long getCountDownTime() {
        return countDownTime;
    }

    public void setCountDownTime(long countDownTime) {
        this.countDownTime = countDownTime;

        //this seems to not notify the UI, nothings changing
        notifyPropertyChanged((int) countDownTime);

        //this log prints out
        Log.d("TAG","prime tick:"+countDownTime);
    }

    @BindingAdapter({"app:primeColor"})
    public static void setTextColor(TextView view, String color) {

        if("green".equals(color))
            view.setTextColor(Color.parseColor("#63f421"));

        else  if("pink".equals(color))
            view.setTextColor(Color.parseColor("#ffc0cb"));
    }

    public void startCounting(Long milli){
        mCountDownTimer.restartTimer(milli);
    }

    @Override
    public void doSomethingWithPrimeCountDown(Long count) {
        setCountDownTime(count);
    }
}
</code></pre>

<p>note: the app:primeColor attribute bindableAdapter works fine its just the bindable getCountdownTime() thats not working. </p>

<p>here is the xml file:</p>

<pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"&gt;
    &lt;data class="CountdownBinder"&gt;
        &lt;variable name="viewModel" type="com.databindingexample.mycompany.databindingexample.ViewModels.ViewModel"/&gt;
    &lt;/data&gt;

    &lt;RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.databindingexample.mycompany.databindingexample.MainActivity"
        tools:showIn="@layout/activity_main"&gt;

        &lt;TextView
            android:id="@+id/tv_green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:primeColor='@{"pink"}'
            android:text="@{Long.toString(viewModel.getCountDownTime)}" /&gt;
    &lt;/RelativeLayout&gt;

&lt;/layout&gt;
</code></pre>

<p>update:
thanks for great answers from SO i wrote a <a href="http://j2emanue.blogspot.com/2016/01/android-databinding-big-picture.html" rel="nofollow">blog on dataBinding</a> to help others. </p>