1. Are you ready for the Galaxy S20? Here is everything we know so far!

Run and collect the methods of buttons in android studio

Discussion in 'Android Development' started by Emre Can AYDIN, Dec 21, 2018.

  1. Emre Can AYDIN

    Thread Starter

    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


  2. Best Answer:
    Post #9 by Deleted User, Dec 26, 2018 (3 points)
  3. Deleted User

    Deleted User Guest

    So if I understand your requirements correctly, you want to track the sequence of arrow button presses, then replay that sequence when you click the start button?
  4. Emre Can AYDIN

    Thread Starter

    Yes I want to do that.
  5. Deleted User

    Deleted User Guest

    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 (Text):
    2. ArrayList<Button> buttons = new ArrayList<>();
    3. ....
    4. // In the onClick() method of each Button
    5. public void onClick() {
    6.   buttons.add(this);
    7. }
    8. ...
    9. // In the onClick() method of the start Button
    10. public void onClick() {
    11.   for (Button button : buttons) {
    12.     button.performClick();
    13. }
    GameTheory and Emre Can AYDIN like this.
  6. Emre Can AYDIN

    Thread Starter

    Thank you so much.I will try to do that.
  7. Emre Can AYDIN

    Thread Starter

    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 (Text):
    2. ArrayList<Button>  buttons = new ArrayList<>();
    This is method of the one of my 4 buttons;
    Code (Text):
    2. forward_btn.setOnClickListener(new View.OnClickListener() {
    3.     @Override
    5.     public void onClick(View v) {
    6.         komutlistesi.add(i);
    7.         buttons.add(this);
    8.         veriAdaptoru.notifyDataSetChanged();
    9.         veriAdaptoru2.notifyDataSetChanged();
    10.         saniyefr=spinner1.getSelectedItem().toString();
    11.         saniyef=Long.parseLong(saniyefr);
    12.         saniyef=saniyef*1000;
    13.         countdowntimer=new CountDownTimer(saniyef,1000) {
    14.             @Override
    15.             public void onTick(long millisUntilFinished) {
    16.                 try
    17.                 {
    18.                     outputStream.write(forward.getBytes());//Komutu arduino bluetooth modülüne iletiyor
    19.                 }
    20.                 catch (IOException e)
    21.                 {
    22.                     e.printStackTrace();
    23.                 }
    25.             }
    27.             @Override
    28.             public void onFinish()
    29.             {
    30.                 Toast.makeText(getApplicationContext(), "Komut Tamamlandı!", Toast.LENGTH_LONG).show();
    31.             }
    32.         }.start();
    35.     }
    36. });

    and this button is the start button that is necessary to run the sequential methods.
    Code (Text):
    2. start_btn.setOnClickListener(new View.OnClickListener() {
    3.     @Override
    4.     public void onClick(View v) {
    5.         for(Button button:buttons)
    6.         {
    7.             button.performClick();
    8.         }
    10.     }
    11. });
    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.
    #6 Emre Can AYDIN, Dec 25, 2018
    Last edited: Dec 25, 2018
  8. Deleted User

    Deleted User Guest

    This line

    Code (Text):
    2. 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?
    Emre Can AYDIN likes this.
  9. Emre Can AYDIN

    Thread Starter

    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.
  10. Deleted User

    Deleted User Guest

    Correct. And the object you want to add is the parameter to your onClick method

    Code (Text):
    2. public void onClick(View v)
    v is actually the Button. It can be cast into a Button object like this

    Code (Text):
    2. Button button = (Button)v;
    Emre Can AYDIN and GameTheory like this.

Share This Page