1

In my layout, I have TextViews that are initially set to be invisible. When the user presses the start button, I want one of these TextViews to be visible for .8 seconds. Then I want it to become invisible again and show another TextView for .8 seconds and so on. So basicly, it's like(textView1 pops up for a bit then disappears, then textView2 pops up for a bit then disappears...). There needs to be a good amount of time between each textView. My problem is finding a way to make my code wait a bit in between the:

findViewById(R.id.textView1).setVisibility(00000000); //makes it visible

and

findViewById(R.id.textView1).setVisibility(00000004); //makes it invisible

I have tried many different methods, but most either makes the textView stay invisible, because it gets to the .setVisibility(00000004) to quickly, or it pauses the code, but never seems to become visible as if .setVisibility(00000000) was ignored. This is what I am trying currently, but the textViews stay invisible.

int counter=0;
public void GamePlay(View view)
{
    turns[counter]=(int)(4*Math.random()+1);
    counter=0;
    while(turns[counter]!=0)
    {

        if(turns[counter]==1)
        {
            findViewById(R.id.textView1).setVisibility(00000000);
            HoldGame();
            findViewById(R.id.textView1).setVisibility(00000004);

        }
        else if(turns[counter]==2)
        {
            findViewById(R.id.textView2).setVisibility(00000000);
            HoldGame();
            findViewById(R.id.textView2).setVisibility(00000004);
        }
        else if(turns[counter]==3)
        {
            findViewById(R.id.textView3).setVisibility(00000000);
            HoldGame();
            findViewById(R.id.textView3).setVisibility(00000004);
        }
        else if(turns[counter]==4)
        {
            findViewById(R.id.textView4).setVisibility(00000000);
            HoldGame();
            findViewById(R.id.textView4).setVisibility(00000004);
        }
        counter++;
    }
}

public void HoldGame()
{
    Timer timer=new Timer();
    timer.schedule(new TimerTask() {
          @Override
          public void run() {

          }
        }, 1*1000);

}
Max Meijer
  • 1,530
  • 1
  • 14
  • 23
JPro
  • 79
  • 8
  • 3
    Use a `Handler` with `Handler.postDelayed(myRunnable, myDelay)`. See: http://developer.android.com/training/multiple-threads/communicate-ui.html – Darwind Jun 11 '14 at 18:19
  • 3
    FYI: It's a lot easier to use the `View` constants (i.e `View.INVISIBLE`, `View.VISIBLE`, `View.GONE`), IMHO – codeMagic Jun 11 '14 at 18:21
  • @codeMagic if OP wants to do this for a while and then remove the view again, he will need to use either a `Timer` or the `Handler`. So just setting the visibility of the views isn't enough... that's at least what I read from the question ;-) – Darwind Jun 11 '14 at 18:25
  • @Darwind you are correct. Actually, there are plenty of possibilities. I was simply making an off-point observation. I wasn't implying that it would solve the problem. – codeMagic Jun 11 '14 at 18:28
  • Is it just me or doesn't this seem like an ideal AsyncTask implementation; where you make the view invisible, execute the task, doInBackground does a simple Thread.sleep(8000); and onPostExecute makes the view visible again? – zgc7009 Jun 11 '14 at 18:31
  • @codeMagic yes I see what you mean :-) – Darwind Jun 11 '14 at 18:56

1 Answers1

0

You can try handler class -

Handler handlerTimer = new Handler();
int count = 0;
Runnable runnable = new Runnable(){
    public void run() {
      if(count==0)
          findViewById(R.id.textView1).setVisibility(00000004);
      else if(count==1)
          findViewById(R.id.textView2).setVisibility(00000004);
      else if(count==2)
          findViewById(R.id.textView3).setVisibility(00000004);
      else if(count==3)
          findViewById(R.id.textView4).setVisibility(00000004);   
      count++;
      if(count<4)
      handler.postDealyed(runnable, 8000)   
  }};

Or you can you use ScheduledThreadPoolExecutor

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
int count=0;
scheduler.scheduleAtFixedRate
  (new Runnable() {
     public void run() {
        if(count==0)
          findViewById(R.id.textView1).setVisibility(00000004);
      else if(count==1)
          findViewById(R.id.textView2).setVisibility(00000004);
      else if(count==2)
          findViewById(R.id.textView3).setVisibility(00000004);
      else if(count==3)
          findViewById(R.id.textView4).setVisibility(00000004);   
      count++;
     }
  }, 0, 8, TimeUnit.SECONDS);

For more information

Community
  • 1
  • 1
Pr38y
  • 1,565
  • 13
  • 21
  • This kind of works. Now the problem is that the order of the textViews pop up so fast, that is seems as though they appear and disappear at the same time, so I need a line or block of code that will give time in between each textView. – JPro Jun 11 '14 at 18:54
  • you can convert it to a block of code and pass value as and when required. for eg. public void setVisibilityAfterDelay(int viewId, long delayInMilisec){ handlerTimer.postDelayed((new Runnable(){ public void run() { findViewById(viewId).setVisibility(00000004); }}, delayinMillisec); } – Pr38y Jun 11 '14 at 18:57
  • This helps, but it does not space out the times in which the TextViews appear and disappear. Example(TextView1 appears, then TextView2 appears 0.1 seconds later, then TextView3 appears 0.1 seconds after TextView 2,etc. Thus, they came in the right order, but it's so fast that it seems they appear and disappear at the same time.) – JPro Jun 11 '14 at 20:02
  • I have updated the code. check now. – Pr38y Jun 12 '14 at 05:06