Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. How to use delay in for loop?

How to use delay in for loop?

Scheduled Pinned Locked Moved Android
androidtutorialquestion
2 Posts 2 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chandan kumar
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • C chandan kumar

      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

      A Offline
      A Offline
      agent_kruger
      wrote on last edited by
      #2

      this might not be what you wanted but for delay you can use thread.Sleep() to delay the loop.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups