-1

My layout have repeating views ,ie a row having 5 buttons , currently I created separate xml and inserted in different parts of the view nut now am confused because I have no idea how I can implement click listeners for each button because everywhere id's are same except container.

I can fix this with help of container view as parent view but I don't know how I can use butterknife here

Please help me...

Bytecode
  • 6,551
  • 16
  • 54
  • 101

1 Answers1

1

I use ButterKnife all of the time, but I can't really understand your question. Could you maybe give a code example?

From what I have mustered you have 5 buttons on one row and you don't think you can attach a click listener to each? You would have to assign a seperate ID to each Button and then capture that with an @OnClick method, like so:

@OnClick(R.id.buttonOne) 
public void buttonOneCLicked() {

}

@OnClick(R.id.buttonTwo) 
public void buttonTwoCLicked() {

}

But I have a feeling I have misunderstood your question, could you clarify?

EDIT:

If you want all of your buttons to execute the same code you could either call a unified method in each of the @OnClick methods, or you could just do it the old fashioned way. ButterKnife uses this technique so your code is easily readable, it is intended to attach to each function.

In-case you don't know, the old fashioned way would mean implementing View.OnClickListener inside your Activity/Fragment and then assigning each of your views that has an OnClickListener with

yourView.setOnClickListener(this);

If you would like some views to handle the click differently then assign that view to the ButterKnife @OnClick, or set a tag on that view and search for it with an if statement in your contextual onClick.

public class YourActivity extends Activity implements View.OnClickListener {

    @Override
    public void onClick(View v) {

    }
}

I hope this answers your question.

vguzzi
  • 2,420
  • 2
  • 15
  • 19
  • Row one : ids 1,2,3,4,5 Row two : 1,2,3,4,5 Row three: 1,2,3,4,5 , All buttons ids are same but it's parent view ids are different .ie I use include statement and inserted the buttons in different places. My questions how I can uniquely identify the each click event – Bytecode Nov 23 '15 at 18:59
  • @Bytecode What are you struggling to achieve? Are you using the include keyword in your xml layouts to include other layouts so they are all the same ID? – vguzzi Nov 23 '15 at 19:02
  • the problem with your answers is that I have to write 15 methods or @OnClick with 15 arguments – Bytecode Nov 23 '15 at 19:03
  • my doubts is how I can implement click event with minimum codes, I don't want to write 15 methods or 3 different layouts – Bytecode Nov 23 '15 at 19:04
  • 1
    @Bytecode That is how ButterKnife works. If you want to only have one click listener it would be incredibly easy to do without ButterKnife, so you could not use it in this case (you can mix ButterKnife and none ButterKnife OnClick's by the way. I've edited my answer. – vguzzi Nov 23 '15 at 19:12
  • If you post some of your code I'll be able to give you a more precise answer. – vguzzi Nov 23 '15 at 19:28