Search code examples
androidbuttonfindviewbyid

Multiple button initialization


I have ~10 buttons and I want to initialize them by calling a method. I mean now I have a lot of:

button= (Button) findViewById(R.id.button);
button1= (Button) findViewById(R.id.button1);

I want to have a method like this and call it for every button initialization:

private void initialize(Button mybutton){
  mybutton= (Button) findViewById(R.id.mybutton);
}

Solution

  • Try this way pass Your Button as well as ID of Your Button

    change initialize like this

    private void initialize(Button mybutton, int ID){
      mybutton= (Button) findViewById(ID);
    }
    

    and call Your initialize method like this

    initialize(button,R.id.button)