-2

I'm new to Android development so bear with me. I'm trying to implement a day planner that has 24 Buttons that represent each hour of a day. On button click there should appear a new Activity in which you can type what you have planned for that hour and/or change the button's color.

My question is, how should I implement the click listeners so that I don't have to write a separate one for each button? How do I link each button to a description?

What I thought about doing is to create a class "Hour" that has as attributes a Button and a String (the description) and then creating an array of those but how do I then map each Button to an entry of that array dynamically without hardcoding?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Link
  • 120
  • 1
  • 8

3 Answers3

0

I think you can try make two ArrayList<> instances, one for Buttons and one for String descriptions. Then you can use for each loop to get the same position from these ArrayLists.

ArrayList<Button> buttons = new ArrayList<Button>();
ArrayList<String> descriptions = new ArrayList<String>();

//fill this two arraylists like you want
//try to keep the same position for button and description
//then
for(Buttons b: buttons){
    b.setText(description.get(buttons.indexOf(b));
    //Then make onClickListener for b
 }
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
MrFisherman
  • 720
  • 1
  • 7
  • 27
0

Create button in java dynamically according to description size and add button in layout like this.

LinearLayout layout;//nasalize your layout in with findviewbyid
            layout.removeAllViews();
            ArrayList<String> descriptions = new ArrayList<String>();
            //fill this two arraylists like you want
            //try to keep the same position for button and description
            //then
            for(int i=0;i<descriptions.size();i++){
                Button b=new Button(MainActivity.this);
                b.setText(descriptions.get(i));
                b.setId(i);
                b.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       switch(b.getId())
                       {
                           case 0:
                               //hour handel heere
                               break;
                           case 1:
                               //next and so on
                               break;
                       }
                    }
                });
               layout.addView(b);
            }
asim
  • 310
  • 1
  • 8
0

Implement OnClickListener Interface and Override its onClick method like below

public class MyClass extends AppCompatActivity implements 
View.OnClickListener {

        @Override
       public void onClick(View view) {

           if (view == button1) {
                 //Code here
           }else if(view == button2){
                //Code here 
           }
           //and so on ... 
      }

}

Simplest way .

Rashid Kalhoro
  • 340
  • 4
  • 14