0

Here is the situation, Im clicking my buttons then make it invisble Onclick. Question is: How can I check if all of my buttons are invisible(no one left) in columns and rows. Then execute another command. Here is some of my screenshot buttons that current visible.

enter image description here

My Code:

 private void loadCards(){
            try{
                givingcards.start();

                int size = ROW_COUNT*COL_COUNT;

                Log.i("loadCards()","size=" + size);

                ArrayList<Integer> list = new ArrayList<Integer>();

                for(int i=0;i<size;i++){
                    list.add(new Integer(i));
                }


                Random r = new Random();

                for(int i=size-1;i>=0;i--){
                    int t=0;

                    if(i>0){
                        t = r.nextInt(i);
                    }

                    t=list.remove(t).intValue();
                    cards[i%COL_COUNT][i/COL_COUNT]=t%(size/2);

                    Log.i("loadCards()", "card["+(i%COL_COUNT)+
                            "]["+(i/COL_COUNT)+"]=" + cards[i%COL_COUNT][i/COL_COUNT]);
                }
            }
            catch (Exception e) {
                Log.e("loadCards()", e+"");
            }

        }

        private TableRow createRow(int y){
             TableRow row = new TableRow(context);
             row.setHorizontalGravity(Gravity.CENTER);

             for (int x = 0; x < COL_COUNT; x++) {
                     row.addView(createImageButton(x,y));
             }
             return row;
        }

        private View createImageButton(int x, int y){
            Button button = new Button(context);
            button.setBackgroundDrawable(backImage);
            button.setId(100*x+y);
            button.setOnClickListener(buttonListener);
            return button;
        }

2 Answers2

1

there is a simple solution you can apply, that is using a variable as counter, it will update whenever onClick() be called

   int counter = 0;

    public void onClick(View view) {
      // visible button ..
      counter++;
      checkButtonsStatus();
    }

    public checkButtonsStatus() {
    if (counter == (4*4)) {
      // all buttons is visible
    }
    }
}

hope this helps

GianhTran
  • 3,443
  • 2
  • 22
  • 42
0

Try this code :

Add method below in createImageButton(); :

button.setTag(100*x+y);

in your buttonListener put add following code :

public void checkCards(){
                if(cards[secondCard.x][secondCard.y] == cards[firstCard.x][firstCard.y]){
                    successpair.start();
                    firstCard.button.setVisibility(View.INVISIBLE);
                    secondCard.button.setVisibility(View.INVISIBLE);
                }
                else {
                    wrongpair.start();
                    secondCard.button.setBackgroundDrawable(backImage);
                    firstCard.button.setBackgroundDrawable(backImage);
                }

                firstCard=null;
                secondCard=null;


                // check here


                boolean isAnyViewVisible = false;

                for (int y = 0; y < ROW_COUNT; y++) {
                    for (int x = 0; x < COL_COUNT; x++) {
                        View view = findViewWithTag(100 * x + y);

                        if (view!=null && view.getVisibility() == VISIBLE) {
                            isAnyViewVisible = true;
                            break;
                        }
                    }
                    if (isAnyViewVisible) {
                        break;
                    }
                }

                if (!isAnyViewVisible) {
                    // write your code here, which you want to execute when all views are invisible

                }


            }

Change this method too to this one :

    // check here
    private void turnCard(Button button,int x, int y) {
        button.setBackgroundDrawable(images.get(cards[x][y]));

        if(firstCard==null){
            firstCard = new Card(button,x,y);

//setTag here firstCard.button.setTag(100*x+y); } else{

            if(firstCard.x == x && firstCard.y == y){
                return; //the user pressed the same card
            }

            secondCard = new Card(button,x,y);

//setTag here

            secondCard.button.setTag(100*x+y);

            turns++;
            ((TextView)findViewById(R.id.tv1)).setText("Tries: "+turns);


            TimerTask tt = new TimerTask() {

                @Override
                public void run() {
                    try{
                        synchronized (lock) {
                            handler.sendEmptyMessage(0);
                        }
                    }
                    catch (Exception e) {
                        Log.e("E1", e.getMessage());
                    }
                }
            };

            Timer t = new Timer(false);
            t.schedule(tt, 1300);
        }


    }
NehaK
  • 2,639
  • 1
  • 15
  • 31