0

This seems like a very simple problem, but for some reason I find myself unable to find any suitable answers that work. What I have is 2 buttons with one stacked on the other in a Frame Layout, and when Button1 is clicked, it becomes invisible and Button2 appears. What I'm trying to get to happen is after a few seconds Button2 automatically becomes invisible and Button1 is visible again. Here is the little bit of code I have. Any help would be greatly appreciated!

button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);


        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub 

                button1.setVisibility(Button.GONE);
                button2.setVisibility(Button.VISIBLE);

            }
        });
BossWalrus
  • 770
  • 1
  • 12
  • 29

3 Answers3

4

A simpler solution than many that are being proposed here would be as follows:

button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);


button1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        button1.setVisibility(Button.GONE);
        button2.setVisibility(Button.VISIBLE);
        button1.postDelayed(new Runnable() {
            @Override
            public void run() {
                button1.setVisibility(View.VISIBLE);
                button2.setVisibility(View.GONE);
            }
        }, 2000);
    }
});
Scott W
  • 9,742
  • 2
  • 38
  • 53
1

This can be an inner class to your Activity.

public class SleepTask extends AsyncTask<Void, Void, Void>
{

    final Button mOne, mTwo;

    public OnCreateTask(final Button one, final Button two) {  
          mOne = one;
          mTwo = two;
    }

    protected Void doInBackground(Void... params)
    {
         //Surround this with a try catch, I don't feel like typing it....
         Thread.sleep(2000);
    }

    protected void onPostExecute(Void result) {
         //This keeps us from updating a no longer relevant UI thread.
         //Such as if your acitivity has been paused or destroyed.
         if(!isCancelled())
         {
              //This executes on the UI thread.
              mOne.setVisible(Button.VISIBLE);
              mTwo.setVisible(Button.GONE);
          }
     }
}

In your Activity

   SleepTask mCurTask;

   onPause()
   {
       super.onPause();
       if(mCurTask != null)
           mCurTask.cancel();
   }

In your onClick

   if(mCurTask == null)
   {
       button1.setVisibility(Button.GONE);
       button2.setVisibility(Button.VISIBLE);
       mCurTask = new SleepTask;
       mCurTask.execute();
   }

I did all this from the top of my head so it may have to be pushed through eclipse to make it happy. Keep in mind, all lifecycle calls (onCreate, onDestroy) are done on the UI thread, you should only access the mCurTask on the UI thread if you want to make it safe.

AsyncTasks are very nice to use, this might be over kill for you particular scenario, but it's a pattern that is common in Android.

accordionfolder
  • 573
  • 4
  • 17
  • Thank you so much for your help and lending me your knowledge! Very very much appreciated!! – BossWalrus Jan 18 '13 at 19:11
  • 1
    Lol, it's overkill for the situation, the only thing I'd add to the accepted answer is check for the relevancy of the buttons in the situation, aka null them out in onPause and check for null in the delayed runnable. – accordionfolder Jan 18 '13 at 19:13
  • 1
    It'll come in handy for me someday though! No doubt about it! – BossWalrus Jan 18 '13 at 21:06
0

There are many way to do that.

You should implement a handler in your activity (link to the UI thread) and post a sendMessageDelayed from a new thread

Edit : Scott W. has right : In the same logic you can use the command

PostDelayed(Your_runnable, time_to_wait)

Goo
  • 1,318
  • 1
  • 13
  • 31