Zutru Zutru - 4 days ago 4x
Android Question

Generate equation after a certain amount of time

I'm working on a school project in Android Studio and so far I've written a code which generates a random equation and then display this equation in a textview. Here is the code:

String[] operationSet = new String[]{"+", "-", "/", "*"};

Random random = new Random();
int numOfOperations = random.nextInt(2) + 1;

List<String> operations = new ArrayList<>();

for (int i = 0; i < numOfOperations; i++) {
String operation = operationSet[random.nextInt(4)];

int numOfNumbers = numOfOperations + 1;
List<Integer> numbers = new ArrayList<>();

for (int i = 0; i < numOfNumbers; i++) {
int number = random.nextInt(10)+1;

String equation = "";
for (int i = 0; i < numOfOperations; i++) {
equation += numbers.get(i);
equation += operations.get(i);
equation += numbers.get(numbers.size() -1);

TextView TextEquation = (TextView)findViewById(R.id.textView3);

String stringResultOfEquation = String.valueOf(equation);

// Resultat der Rechung berechnen

double doubleAnswer = eval(stringResultOfEquation);

String stringAnswer = Double.toString(doubleAnswer);

TextView textAnswer = (TextView)findViewById(R.id.textView4);

So far I've tried to use the TimerTask command:

TimerTask timerTaskWaiting = new TimerTask() {
public void run() {


Timer timerwaiting = new Timer();
timerwaiting.schedule(timerTaskWaiting, 5000);

I've put the "equation generater code" and put it into "public void run(){...}" but the app crashed when I tried it out.

My question now is, if there is a simple way which will generate the equation after a certain amount of time (for example 5 seconds) I mean, I want that the equation will be generated 5 seconds after the app is launched.

If there is anything unclear in my question, feel free to ask and I will try to clarify the problem :)

Thank you already in advance for your help!


This bit of code should suffice for you to understand what you need to do. obviously there are other ways you can achieve this.

protected void onCreate(Bundle savedInstanceState) {
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
     }, 5000);