Henrik R
Henrik R

Reputation: 4972

How to toggle focus between buttons without losing focus on clicking button

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();
        }
    }

Upvotes: 0

Views: 578

Answers (2)

Henrik R
Henrik R

Reputation: 4972

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

Upvotes: 1

lokifacio
lokifacio

Reputation: 71

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.

Upvotes: 1

Related Questions