• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Run and collect the methods of buttons in android studio

Hello to everyone
I hope I wrote the title correctly.
I'm trying to design an android application, the interface I want to do is as follows:
There will be 4 buttons (forward, back, right, left) and 1 start button on the screen.
But the problem is, when I click on these 4 buttons and in order to collect their methods and click on their methods in order to press the start button, I'm trying to collect the methods of these 4 buttons in order to work in order and I couldn't find anything like this on the internet.
I've used translation to ask this question, I'm sorry if I have spelling errors.and I tried to draw the interface to better understand him in the appendix.I would be very happy if you help.Thanks in advance.
1.png
 
So your solution will involve -

1. Storing a list of Button objects
2. Programmatically clicking each Button in the list

So you'd need something like the following. Bear in mind this is just my typed in pseudo-code, not compiled, just to give you an idea:

Code:
ArrayList<Button> buttons = new ArrayList<>();
....
// In the onClick() method of each Button
public void onClick() {
  buttons.add(this);
}
...
// In the onClick() method of the start Button
public void onClick() {
  for (Button button : buttons) {
    button.performClick();
}
 
Upvote 0
So your solution will involve -

1. Storing a list of Button objects
2. Programmatically clicking each Button in the list

So you'd need something like the following. Bear in mind this is just my typed in pseudo-code, not compiled, just to give you an idea:

Code:
ArrayList<Button> buttons = new ArrayList<>();
....
// In the onClick() method of each Button
public void onClick() {
  buttons.add(this);
}
...
// In the onClick() method of the start Button
public void onClick() {
  for (Button button : buttons) {
    button.performClick();
}
Thank you so much.I will try to do that.
 
Upvote 0
So your solution will involve -

1. Storing a list of Button objects
2. Programmatically clicking each Button in the list

So you'd need something like the following. Bear in mind this is just my typed in pseudo-code, not compiled, just to give you an idea:

Code:
ArrayList<Button> buttons = new ArrayList<>();
....
// In the onClick() method of each Button
public void onClick() {
  buttons.add(this);
}
...
// In the onClick() method of the start Button
public void onClick() {
  for (Button button : buttons) {
    button.performClick();
}
Hello, I tried to implement the psedo code you gave me in my own code, but gave an error.
I add the error and the button method I wrote down.

This is my array list for the gathering the methods as you said.
Code:
ArrayList<Button>  buttons = new ArrayList<>();

This is method of the one of my 4 buttons;
Code:
forward_btn.setOnClickListener(new View.OnClickListener() {
    @Override

    public void onClick(View v) {
        komutlistesi.add(i);
        buttons.add(this);
        veriAdaptoru.notifyDataSetChanged();
        veriAdaptoru2.notifyDataSetChanged();
        saniyefr=spinner1.getSelectedItem().toString();
        saniyef=Long.parseLong(saniyefr);
        saniyef=saniyef*1000;
        countdowntimer=new CountDownTimer(saniyef,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                try
                {
                    outputStream.write(forward.getBytes());//Komutu arduino bluetooth modülüne iletiyor
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinish()
            {
                Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
            }
        }.start();


    }
});


and this button is the start button that is necessary to run the sequential methods.
Code:
start_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        for(Button button:buttons)
        {
            button.performClick();
        }

    }
});
When i try to start app i get error like this:
error: no suitable method found for add(<anonymous OnClickListener>)
method Collection.add(Button) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to Button)
method List.add(Button) is not applicable

I would appreciate it if you could help.
 
Last edited:
Upvote 0
This line

Code:
buttons.add(this);

Question: What do you think "this" refers to? It's a bit of a zen question, but when you use the 'this' variable in Java, it's a reference to the object in which it's being used. So where you've put that line in the code, what type of object is it referencing?
I got it i need to write this code line like this:buttons.add("object of forward_btn") when i write this it's gonna work i think.
In the meantime, thank you very much, you helped me a lot.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones