6

A TextView and Button are programmatically created and added to a pre-existing vertical layout making it look like a vertical list of views. These views are only created based off the user entering data into an edittext and that data being saved into an ArrayList.

How can I add an onClick function to my 'trash' buttons that are programmatically created that would allow them to remove the view they are associated with.

public static ArrayList<String> deckNameArray = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainLayout);

    for(int i = 0; i < deckNameArray.size(); i++)
    {
        LinearLayout layout = new LinearLayout(this);
        if ((i % 2) == 0) {
            layout.setBackgroundColor(Color.CYAN);
        } else {
            layout.setBackgroundColor(Color.WHITE);
        }
        layout.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layout.setLayoutParams(layoutParams);
        layout.setPadding(10, 5, 10, 5);
        layout.setWeightSum(5);
        mainLayout.addView(layout);

        LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 4f);
        TextView deckName = new TextView(this);
        deckName.setText(deckNameArray.get(i));
        deckName.setTextColor(Color.BLACK);
        deckName.setTextSize(18);
        deckName.setGravity(Gravity.CENTER_VERTICAL);
        deckName.setLayoutParams(textViewParams);
        layout.addView(deckName);

        LinearLayout.LayoutParams imageButtonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1f);
        ImageButton remove = new ImageButton(this);
        remove.setImageResource(R.mipmap.trash);
        if ((i % 2) == 0) {
            remove.setBackgroundColor(Color.CYAN);
        } else {
            remove.setBackgroundColor(Color.WHITE);
        }
        remove.setLayoutParams(imageButtonParams);
        layout.addView(remove);
    }
Dinith Rukshan Kumara
  • 638
  • 2
  • 10
  • 19
th3ramr0d
  • 484
  • 1
  • 7
  • 23
  • There's a really good answer to this already at: http://stackoverflow.com/questions/4401028/dynamically-creating-buttons-and-setting-onclicklistener – raddevus Nov 27 '15 at 19:41

2 Answers2

11
remove.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
         }
     });

See http://developer.android.com/reference/android/widget/Button.html

QuantumTiger
  • 970
  • 1
  • 10
  • 22
5

You can set a click listener on any View, using code similar to the sample below:

View myView;

myView.setOnClickListener(new OnClickListener() {
    @Override public void onClick(View v) {
         // TODO your logic here
    }
}
fractalwrench
  • 4,028
  • 7
  • 33
  • 49
  • 1
    If you've created more than 1 view then set an id to it using `myView.setId(index)` and inside `onClickListener` get the id to differentiate between those views – Prabs Jan 04 '17 at 05:54