Harnessing the Power of Java Interfaces in Android Development — Part 0.0001

Bill Inkoom
3 min readJun 23, 2017

So lets imagine you have a long math operation and you want to do something when you have finished the calculation. What do you do? Well in android its not advisable to do it on the main thread since that will block the ui and cause bad user experience. So being as smart as you are you define a function which does the computation on another thread and then when complete updates a certain textField or edittext with the answer.

I know you have heard of Java Interface, everyone has. You even use them in your App development all the time. This is a famous one

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//do something
}
});

We all do this at some point in our code. But interfaces are meant for more. They could be used in little ways to help us write better and cleaner code. So today we will try and do same.

So lets imagine you have a long math operation and you want to do something when you have finished the calculation. What do you do? Well in android its not advisable to do it on the main thread since that will block the ui and cause bad user experience. So being as smart as you are you define a function which does the computation on another thread and then when complete updates a certain textField or edittext with the answer.

int t =0;
public void bigCalc(final int a, final int b, final TextView textView){

new Thread(new Runnable() {
@Override
public void run() {
t=a*b;
textView.setText("ans: "+t);
}
}).start();
}

Voila that looks beautiful. But what if you want to use bigCalc function at a number of places. Will you overload the function by adding more arguments all the time? What if next time you don’t want to set the answer to a textview but you want to toast it.

Toast.makeText(context,"ans"+t,Toast.LENGTH_SHORT).show();

will you make another function and name it bigCalcWithToast()? Well I hope not.

We can re-write this function in a way that will allow it to be more useful. Lets begin.

First we create a simple java interface like this

public interface BigCalcInterface{
void onFinishedCalculating(int ans);
}

This simply tells java that we are defining a function without a body in the BigCalcInterface and that we promise to give onFinishedCalculating(int ans) a proper body when we create an instance of BigCalcInterface class. Java trusts us since it will not let us compile our code if we don’t honor the contract.

So after doing this lets change the definition of bigCalc.

public void bigCalc(final int a, final int b, BigCalcInterface bigCalcInterface)

now lets see how the body of bigCalc looks like

new Thread(new Runnable() {
@Override
public void run() {
bigCalcInterface.onFinishedCalculating(a*b);
}
}).start();

So what we are saying is after the calculation we call the onFinishedCalculating(int ans) function with a*b as our argument. At this point you are wondering where is the body of onFinishedCalculating?

well onFinishedCalculating’s body will be defined when we call the bigCalc function which currently looks like this

public void bigCalc(final int a, final int b, final BigCalcInterface bigCalcInterface){


new Thread(new Runnable() {
@Override
public void run() {
bigCalcInterface.onFinishedCalculating(a*b);
}
}).start();
}

Now lets say we want to multiply 10000000000000000 by 37878378237283728377823. We definitely need bigCalc right so lets do this!

bigCalc(10000000000000000, 37878378237283728377823, new BigCalcInterface() {
@Override
public void onFinishedCalculating(int ans) {
textview.setText("ans: "+ans);
}
});

or if we wanted to show a toast of the ans we could just do this

bigCalc(10000000000000000, 37878378237283728377823, new BigCalcInterface() {
@Override
public void onFinishedCalculating(int ans) {
Toast.makeText(context,"ans: "+ans,Toast.LENGTH_SHORT).show();
}
});

This can be applied to all sought of things in android. You might be more creative than I am so I know you will find more uses for it.

Until next time,

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Bill.out();
}
},Toast.LENGTH_LONG);
Toast.makeText(context,"Please let me know what you think in the comments. All suggestions are welcome.",Toast.LENGTH_SHORT).show();

--

--

Bill Inkoom

Software Engineer with great love for Mathematics especially Abstract Mathematics.