19

) I'm in a process of creating a memory game. My problem is that whenever i click for the second time, i can't even see toggled button. To be clear - first click toggles the togglebutton, so i can see the number it holds, the second click on a different togglebutton is suposed to toggle it, show me the number and then proceed to either set a score +1 if numbers are the same, or reverse them back again if they're different.

Below is the code that i use as my onClick function, i've been thinking about putting some kind of sleep or delay function somwhere in the second "if block" - (if(klikniecia ==2)).

Any help on this topic would be appreciated.

public void onClick(View view) {
for (int i = 0; i < karta.length; i++){
    if (view == karta[i]){
        karta[i].setEnabled(false);
        klikniecia++;
        if (klikniecia == 1){
            kartaID[0]=i;
            kartaWartosc[0]=listaKart.get(i);

        }
        if (klikniecia == 2){
            kartaID[1]=i;
            kartaWartosc[1]=listaKart.get(i);

            //i think, about setting a delay here, so i can see both of the cards, regardles if the're the same or not before reverting them.

            if (czyPara()){
                karta[kartaID[0]].setEnabled(false);
                karta[kartaID[1]].setEnabled(false);
                klikniecia=0;
            }
            else{

                karta[kartaID[0]].setEnabled(true);
                karta[kartaID[0]].toggle();
                karta[kartaID[1]].setEnabled(true);
                karta[kartaID[1]].toggle();
                klikniecia=0;

            }
        }

    }

}

}

Piotrgo
  • 305
  • 1
  • 2
  • 8
  • 12
    Why do people still use non-english names in their code? That makes understanding/reading it really hard for people who don't speak $language. – ThiefMaster Nov 16 '10 at 23:23
  • 2
    It's easier for me to use polish names of variables in code, and when i write it i don't think about anyone else looking at it ;-) Though in future more complex projects i might consider using english :-) – Piotrgo Nov 17 '10 at 09:46

5 Answers5

51

Instead of sleeping in the onclick, you could post a delayed message to a handler (with associated runnable) and have it update the UI. Obviously fit this into the design of your app and make it work, but the basic idea is this:

//Here's a runnable/handler combo
private Runnable mMyRunnable = new Runnable()
{
    @Override
    public void run()
    {
       //Change state here
    }
 };

Then from onClick you post a delayed message to a handler, specifying that runnable to be executed.

Handler myHandler = new Handler();
myHandler.postDelayed(mMyRunnable, 1000);//Message will be delivered in 1 second.

Depending on how complicated your game is, this might not be exactly what you want, but it should give you a start.

G M Ramesh
  • 3,420
  • 9
  • 37
  • 53
Ryan Reeves
  • 10,209
  • 3
  • 42
  • 26
  • If i got this right, the concept is that i should wrap that part of the code i want to be delayed inside the runnable, and then refer to it with a delay from onclick. This seems to be a good idea. I'll try this today, but i think it's going to work :-) – Piotrgo Nov 17 '10 at 09:43
  • how can i pass argument to runnable in this case – Yashpal Singla Nov 19 '12 at 13:42
34

Correctly work:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after 100ms
  }
}, 100);
Majid
  • 13,853
  • 15
  • 77
  • 113
Ali Mohammadi
  • 1,306
  • 1
  • 14
  • 28
2

You never should sleep in UI thread (by default all your code runs in UI thread) - it will only make UI freeze, not let something change or finish. I can't suggest more because I don't understand code logic.

Andrey Novikov
  • 5,563
  • 5
  • 30
  • 51
2

Do not sleep in the UI thread. You need another thread that will look for a "wake up and wait" message from your UI thread. That second thread could then do your hiding after a normal sleep call. You could then keep the thread around for the next time you need to hide something, or kill it and whip up a new one each time you need another delay.

I believe there are some "postFoo" functions that might be useful in this context (triggering UI events from outside the UI thread).

Mark Storer
  • 15,672
  • 3
  • 42
  • 80
1
b.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        final ProgressDialog myPd_ring=ProgressDialog.show(MainActivity.this, "confident checker", "calculating please wait..", true);
        myPd_ring.setCancelable(true);
        new Thread(new Runnable() {  
              public void run() {
                    // TODO Auto-generated method stub


                    try
                    {
                          Thread.sleep(5000);


                    }
                    catch(Exception e){}
                    b.dismiss();
              }
        }).start();


        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
          public void run() {
              Toast.makeText(getApplicationContext(), "today your confident level is 90% ",
                        Toast.LENGTH_LONG).show();

          }
        }, 5000);


    }
});
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Anand Mohan
  • 79
  • 15