I am wondering about this weird thing. I want to create a button and when the button is clicked I want a new button to be created and the old one removed, all with only code.
Here is my code so far and it does not work as I had hoped. Any input here? Thanks.
public void createRounds(int rounds){
ArrayList<Button> buttonArray = new ArrayList<>();
for(int i=0;i<=rounds;i++){
bk = new Button(getActivity());
bk.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
bk.setBackground(getResources().getDrawable(R.drawable.btnshapegreen));
bk.setId(i);
bk.setText("Button "+i);
buttonArray.add(bk);
}
for(final Button a : buttonArray){
generated_time.addView(a);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(),a.getText().toString(),Toast.LENGTH_LONG).show();
generated_time.removeView(a);
}
});
}
}
I know that the for-each loop does add all buttons at once, but isnt there a way to add one at a time?