Reputation:
I noticed I was doing this in my code:
ResultButton = new Button( theActivity );
ButtonUtils.setButtonValues( ... );
((ViewGroup) (theActivity).findViewById( android.R.id.content )).addView( ResultButton );
ResultButton.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View arg0) {
doStuff();
((ViewGroup) (getActivity()).findViewById( android.R.id.content )).removeView( ResultButton );
}
});
That seems clearly wrong, to remove itself inside its OnClickListener. But what is the right way to deal with these things. Since there is no main method in an Android program I cannot just set a flag and then have it deal with it later. You never really remove things? You just set them to invisible?
Upvotes: 2
Views: 57
Reputation: 21122
Inside your onClick
implementation, the argument that you didn't rename corresponds to the view that triggered the event, you can call the parent of said view and ask it to remove the said child view.
ViewGroup parentView = (ViewGroup) view.getParent();
parentView.removeView(view);
To do this, rename arg0
to view
, and you should be fine
setVisibility
method, to either View.GONE
or View.INVISIBLE
depending on wether you want to to keep taking the screen space it was taking when visible or be completely gone, but since you asked to remove the view, the first option should suffice.
Upvotes: 3