How to use delay in for loop?
-
I am trying to display a multiplication table with delay. My code is working fine but I am not able to implement the delay. Here is My code:-
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;public class TestActivity extends Activity{
Button tableButton1;
TextView txtView;
Runnable r;
String s;
int value = 0;
static int count = 0;
Handler handle = new Handler();
StringBuilder sb = new StringBuilder();@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.text\_display); txtView = (TextView) findViewById(R.id.outputTXT); tableButton1 = (Button) findViewById(R.id.seven); tableButton1.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(getApplicationContext(), "display" , 222).show(); value= Integer.parseInt(tableButton1.getText().toString()); r = new Runnable() { @Override public void run() { // TODO Auto-generated method stub count+=1000; if(count==10000){ count = 0; value=0; handle.removeCallbacks(r); sb.setLength(0); Toast.makeText(getApplicationContext(), "display counter" +sb, 222).show(); }else{ sb.append(value + " x " + count/1000 + " = " + count/1000 \* value+ "\\n"); Toast.makeText(getApplicationContext(), "Hi" +sb, 222).show(); handle.postDelayed(this, 1000); } } }; } }); }
Any answer is appreciable. Thank in advance
-
I am trying to display a multiplication table with delay. My code is working fine but I am not able to implement the delay. Here is My code:-
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;public class TestActivity extends Activity{
Button tableButton1;
TextView txtView;
Runnable r;
String s;
int value = 0;
static int count = 0;
Handler handle = new Handler();
StringBuilder sb = new StringBuilder();@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.text\_display); txtView = (TextView) findViewById(R.id.outputTXT); tableButton1 = (Button) findViewById(R.id.seven); tableButton1.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(getApplicationContext(), "display" , 222).show(); value= Integer.parseInt(tableButton1.getText().toString()); r = new Runnable() { @Override public void run() { // TODO Auto-generated method stub count+=1000; if(count==10000){ count = 0; value=0; handle.removeCallbacks(r); sb.setLength(0); Toast.makeText(getApplicationContext(), "display counter" +sb, 222).show(); }else{ sb.append(value + " x " + count/1000 + " = " + count/1000 \* value+ "\\n"); Toast.makeText(getApplicationContext(), "Hi" +sb, 222).show(); handle.postDelayed(this, 1000); } } }; } }); }
Any answer is appreciable. Thank in advance
this might not be what you wanted but for delay you can use thread.Sleep() to delay the loop.