-4

I need a way to know which of my buttons was pressed first. The app layout is more or less like this.:

Button1 Button2
Button3 Button4
Button5 Button6
Button7 Button8
Button9 Button10
Button11 Button12

And if one of the buttons of it's "line" is pressed, the other disappears. The thing is, I have no idea how to know which one of all these 12 buttons was pressed first, then pressed second, then pressed third and so on...

The code I have for hiding buttons works well, but that's pretty much the easy part.

button1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {

                        button1.setVisibility(View.GONE);
                        button2.setVisibility(View.GONE);

                    }
                });

I searched but maybe I don't know what exactly to search for, and I didn't find a good answer.

Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
Luis
  • 123
  • 1
  • 3
  • 18
  • Can't you repeat the same code through all the 12 buttons? That should work. – devgianlu Feb 26 '19 at 15:19
  • Do you as the developer want to know or do you need some way for the user to know? – Karan Modi Feb 26 '19 at 15:20
  • 4
    Unless I'm not understanding properly, you could just fill an array, or something similar, which for each entry has the date of that moment and the name of the button that has been pressed. – user2638180 Feb 26 '19 at 15:20
  • @devgianlu I have that for all the buttons, as I said, hiding them is the easy part, and it's done already, I just need to know which order they were pressed. – Luis Feb 26 '19 at 15:32
  • @user2638180 So, would it work if I stored the time variable and then checked it? But won't this solution give me problems ahead? Like if somehow to date gets changed, imagine a hypotetical yet possible scenario where I use the app on the verge of the summer/winter time hour change near midnight? That could potentially mess things up a bit. – Luis Feb 26 '19 at 15:32
  • @KaranModi both me and the user, but the way the user will see, I can work out later without major issues I guess. The results will be shown in another activity. – Luis Feb 26 '19 at 15:34
  • 1
    @Luis, if you don't want to work with dates, you can just use the position in which they have been pressed,so use a variable that starts at 0 and gets increased for a click in any button by one and fill the array with the position and the name of the button. – user2638180 Feb 26 '19 at 15:35
  • @user2638180 Ok, so at first search I'm guessing this two links here will help me achieve what I need? I have to read more about arrays... [link](https://stackoverflow.com/a/41649402/1085701) [link](https://stackoverflow.com/a/13260774/1085701) – Luis Feb 26 '19 at 15:44

1 Answers1

1

You can do it like this: 1) Have a HashMap where you link both of your buttons on the same line to each other. 2) Have an ArrayList of button id's where you can hold the order of presses. 3) Implement a method which will perform the mapping and call it in your Activity's #onCreate method. 4) Set your global listener instance to all of your buttons.

private HashMap<Integer, Integer> buttonMap = new HashMap<>();
private ArrayList<Integer> buttonPressedOrder = new ArrayList<>();

// A global listener instance to be set to all of your buttons
private View.OnClickListener listener = new View.OnClickListener() {
    public void onClick(View selectedButton) {
      int selectedButtonId = selectedButton.getId();

      //Add pressed button to pressed buttons list
      buttonPressedOrder.add(selectedButton.getId());

      //Find button to hide and hide it
      int hidingButtonId = buttonMap.get(selectedButtonId);
      Button hidingButton = findViewById(hidingButtonId);
      hidingButton.setVisibility(View.GONE); 
    }
  }

//Put these inside your activity#onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    mapButtons();
    Button button1 = findViewById(R.id.button1);
    Button button2 = findViewById(R.id.button2);
    ...
    button1.setOnClickListener(listener);
    button2.setOnClickListener(listener);
}

// A method for mapping your buttons in the same line to each other
private void mapButtons(){
    buttonMap.put(R.id.button1, R.id.button2)
    buttonMap.put(R.id.button2, R.id.button1)
    buttonMap.put(R.id.button3, R.id.button4)
    buttonMap.put(R.id.button4, R.id.button3)
    ...

}

Whenever you need to see in which order the buttons are pressed, use this method

public void getButtonPressedOrder(){
    Resources res = getResources();
    int numberOfPressedButtons = buttonPressedOrder.size();
    for(int i=0; i<numberOfPressedButtons; i++){
       Log.i("PressOrder", res.getResourceEntryName(buttonPressedOrder.get(i)) 
                         + " is pressed at " + (i+1) + " order");
    }
}

which will log something like:

I/PressOrder: button1 is pressed at 1 order

I/PressOrder: button5 is pressed at 2 order

I/PressOrder: button10 is pressed at 3 order

Hope this helps!

Onur D.
  • 515
  • 3
  • 11
  • Nice way to tidy up the code! I'm learning new great things here =D. just the last method, I am getting cannot resolve symbol Resource. Am I missing some import here? Also, when going to another activity, to take this data with it, should I use putExtra? – Luis Feb 26 '19 at 17:05
  • 1
    @Luis it was my mistake sorry. It should be Resources. I just edited my answer. Also yes, you can put your arraylist with putExtra in your Intent and you can get it in another activity with #getIntegerArrayListExtra method of Intent. – Onur D. Feb 26 '19 at 17:10
  • Thanks man, that works flawlessly! Amazing to see that my post is getting so downvoted, it's probably one of the worst questions ever, I should have been born knowing how to do it since there are no other like it on the internet. – Luis Feb 26 '19 at 17:29
  • Glad that I could help! I don't know why you get so many downvotes maybe it has something to do with rules of asking a question here. But in the end, I do not see what is wrong with your question :) I find it very explanatory and I had no problem understanding it. All in all, I suggest deep diving on Data Structures since it would really help you tackling challenges like this in your code. Definitely read more on Arrays, Stacks, Queues, LinkedLists, Hash tables etc. Happy coding! – Onur D. Feb 26 '19 at 17:36
  • Will do that, been reading some very interesting articles about Arrays and Hash tables these last few hours, it's really mind opening. Btw, android studio said SparseIntArray would be more efficient than Hashmap. It won't really make much of a difference in my case I guess? Well, stackoverflow being stackoverflow I guess lol. – Luis Feb 26 '19 at 17:39
  • Yes Android Studio tends to warn you in this case. I guess using SparseArray would improve the performance if we were dealing with large number of data. In this case, it is guaranteed that we will have exactly 12 buttons so it would not make much of a difference. – Onur D. Feb 26 '19 at 17:47