0

Im doing speed game where Thread changes focus on 4 different buttons. I want only the thread to change focuses on the buttons. The problem I am having is that when the thread for example chooses blue button to focus and if I press red button then the blue button loses focus. I can see this clearly because my buttons have different pictures when default, pressed and focused. So is there anyway to force keep focus on the button I have chosen even tho I press other button. Here is my code:

Thread:

public void run() {
        while (aika > 0 && isKaynnissa()) {
            try {
                Thread.sleep(aika);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            debug.setText(Integer.toString(aika));
                            if (isKaynnissa()) {
                            arvoNappi();
                            }
                        }
                    });

            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }
            if (aika < 550) {
                uusiaika = 3;
            } else if (aika < 450) {
                uusiaika = 2;
            } else if (aika < 350) {
                uusiaika = 1;
            }
            aika = aika - uusiaika;
        }
        aika = aloitusaika;
        unfocusButtons();
        arvotutLuvut.clear();

    }

Random focus chooser:

public void arvoNappi() {
        Random random = new Random();
        int uusirandom = (int) (4 * random.nextDouble() + 1);
        while (uusirandom == painettavaNappi) {
            uusirandom = (int) (4 * random.nextDouble() + 1);
        }
        painettavaNappi = uusirandom;
        tvrandom.setText(Integer.toString(painettavaNappi));
        if (painettavaNappi == 1) {
            arvotutLuvut.add(1);
            btnPun.setFocusable(true);
            btnPun.requestFocusFromTouch();
        } else if (painettavaNappi == 2) {
            arvotutLuvut.add(2);
            btnVio.setFocusable(true);
            btnVio.requestFocusFromTouch();
        } else if (painettavaNappi == 3) {
            arvotutLuvut.add(3);
            btnVih.setFocusable(true);
            btnVih.requestFocusFromTouch();
        } else {
            arvotutLuvut.add(4);
            btnSin.setFocusable(true);
            btnSin.requestFocusFromTouch();
        }
    }
Henrik R
  • 4,742
  • 1
  • 24
  • 23

2 Answers2

1

Every time you focus a button inside your thread, store a reference to that button in a variable accesible by your class.

Then, on your button's onClickListener in addition to your program logic, request the focus to the last focused button.

Not sure if it will prevent your on click from changing the image of the button and causing some glitches. If that would be the case, you can try to perform your click detection using a TouchListener instead.

lokifacio
  • 71
  • 1
  • 8
  • I tried and it works some what. Only problem is that the button I press gets focus for split second and then it goes to the last choosed random button. This answer is pretty good but not perfect since buttons flash very fast when thread gets random number very fast. Any other suggestions? :) – Henrik R Nov 18 '14 at 12:54
  • Have you tried handling click event on TouchListener instead? By doing so, you don't perform any actual click and thus the state of the button shouldn't change (only if you want to do that explicitly) – lokifacio Nov 18 '14 at 15:32
  • Try this other answer: http://stackoverflow.com/questions/17831395/how-can-i-detect-a-click-in-an-ontouch-listener – lokifacio Nov 18 '14 at 17:44
1

I finally did it with imageviews so I can toggle pictures how I want

Henrik R
  • 4,742
  • 1
  • 24
  • 23