1

I have a bunch of dynamic buttons which I am setting an onClickListeners as they are produced, as well as tagging them with IDs.

Not sure if this a simple one which I have just spent too much time staring at but this is the problem.

If a user clicks a button, it changes colour this is simple and has been achieved by:

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {


if (counter == 0) {

button.setBackgroundColor(Color.parseColor("#FF4DCBBF"));

Toast.makeText(getActivity(), "User Has Been Marked As Present",Toast.LENGTH_LONG).show();

//change boolean value

userPresent = true;

counter++;

} else {

button.setBackgroundColor(Color.parseColor("#FFFFFF"));

Toast.makeText(getActivity(), "User Has Been Marked As Absent",Toast.LENGTH_LONG).show();

//change boolean value

userPresent = false;

counter = 0;
}

}
});

If the user clicks it again, it will change back to the previous colour - but...

If the user clicks one of the other dynamic buttons that hasn't been previously clicked, the counter is thrown out.

I need to know if the button has been clicked and if not, should mark the user as present.

Currently, If on one button I click it and mark the user as present, and then move onto the next button, I will have to click it once (which marks the user as absent due to the counter) then press it again to mark the user as present.

I need the counter to treat each button individually, any ideas how this could be achieved?

Display name
  • 730
  • 2
  • 8
  • 23
  • Huh? If the user clicking one button sets the `userPresent` flag then why do the other buttons also need to mark the user as present? – DigitalNinja Jan 12 '18 at 21:01
  • Each button represents a users name, there are many users - sorry for the confusion. – Display name Jan 12 '18 at 21:03
  • Okay gotcha... and the `counter` represents the number of users present? – DigitalNinja Jan 12 '18 at 21:05
  • No, the `counter` is just a counter initialised at 0 and keeps track if a click has occurred and if so, adds 1, and marks the user as present. Then if a button is clicked again, the else statement kicks in and then sets it back to 0. I have a feeling I am just doing something daft as it feels it should be a simple fix! – Display name Jan 12 '18 at 21:09
  • Well it seems to me that the flag and counter are serving the same purpose. You can just use the flag instead of the counter. You could just have a flag for each button... button1 has userPresent1, button2 has userPresent2 and so on. – DigitalNinja Jan 12 '18 at 21:13
  • You're right about the flag and counter serving the same purpose (told you i've been staring at this too long) but after getting rid of the counter and only using a flag, I have the same problem. I am not sure how to set flag for the dynamically created buttons - will have to look that up. Thanks for the help. – Display name Jan 12 '18 at 21:30
  • @DigitalNinja You gave me an idea which helped me to fix it - I ended up setting static IDs for all the dynamic buttons and changed it based on the view clicked! Its now working perfectly! I can also get the button based on where it is by setting tags, so its all good! – Display name Jan 12 '18 at 21:54
  • 1
    Alright, nice job! You're welcome! – DigitalNinja Jan 12 '18 at 21:59

2 Answers2

0

Once the user has been marked present,maybe disable the onClick listener for that button since you wouldn't need it anymore?

  • I would like to avoid this because if a user was accidentally marked as present, they wouldn't be able to correct their mistake. – Display name Jan 12 '18 at 21:11
  • Wouldn't using indiviual counters for each button fix your problem? Counter1 for button1,counter2 for the second and so on? – Vishnuvardhan Prem Jan 12 '18 at 21:25
  • Not on this occasion because the buttons are dynamic, but I can see your point. I managed to solve the issues in the end by setting static ids for each dynamic button created and changing the id based on the view clicked. Thanks for trying to help and offering suggestions. – Display name Jan 12 '18 at 22:01
  • Okay. Should have paid more attention to the fact that you are adding the buttons dynamically. Nice job! – Vishnuvardhan Prem Jan 13 '18 at 02:52
0

I don't mean to sound condescending but I'm having trouble understanding what you're trying to achieve, but if each button is supposed to hold different information about a user, why not make a custom button that does just that? Make a class called customButton in your package and paste the following code there:

import android.content.Context;
import android.widget.Button;

public class customButton extends Button {

boolean haveIBeenClicked; //false by default
public customButton(Context context) {
    super(context);
    }

public void toggleHaveIBeenClicked(){
    haveIBeenClicked=!haveIBeenClicked;
    updateBackgroundColor();    

}

void updateBackgroundColor(){
if (haveIBeenClicked){
    this.setBackgroundColor(Color.parseColor("#FF4DCBBF"));
}
else{

    this.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
    }

}

then, inside the onClick method (in the activity whose snippet you've shown earlier) you can just call

((customButton)v).toggleHaveIBeenClicked();

...after having created a customButton object and setting an on click listener on it.

Please let me know if this achieves what you desired. If you have trouble running this code, make sure to let me know if the comments and we'll work it out

MEDBRO
  • 56
  • 4
  • No worries, I thought as I was writing out the question that it didn't sound clear but I couldn't come up with a better way to articulate my problem. Anyway, I have managed to solve the issues in the end by setting static ids for each dynamic button created and changing the id based on the view clicked. Thanks for trying to help and offering suggestions. – Display name Jan 12 '18 at 22:02