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);
    }
1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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
    }
}

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.