9

I have a button and when I press it, i want to remove it (not make it invisible). I read that I can do that using layout.removeView(mybutton) but what is the layout ? and how can I get it in my activity

Button showQuestion;
private void initialize() {
    showQuestion = (Button) findViewById(R.id.bAnswerQuestionShowQuestion);
}
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.bAnswerQuestionShowQuestion:
                showQuestion.setVisibility(View.INVISIBLE);
                //Here i want to delete the button
                question.setVisibility(View.VISIBLE);
                theAnswer.setVisibility(View.VISIBLE);
                answerQuestion.setVisibility(View.VISIBLE);
                showChoices.setVisibility(View.VISIBLE);
                showHint.setVisibility(View.VISIBLE);
            break;
    }
}
4

4 に答える 4

18

see link

ViewGroup layout = (ViewGroup) button.getParent();
if(null!=layout) //for safety only  as you are doing onClick
  layout.removeView(button);
于 2012-06-23T12:07:44.190 に答える
14

i have a button and when i press it , i want to remove it (not make it invisible)

=> You did as below:

 showQuestion.setVisibility(View.INVISIBLE);

Try with:

 showQuestion.setVisibility(View.GONE);

FYI, INVISIBLE just hide the view but physically present there and GONE hide as well remove the presence physically as well.

于 2012-06-23T12:13:21.007 に答える
4

You can use

      button.setVisibility(View.GONE);
于 2012-06-23T12:10:36.403 に答える
1

Layout is the parent Layout of your Button, usually a RelativeLayout or LinearLayout.

You can get it as follows:

ViewParent layout = button.getParent();
于 2012-06-23T12:09:23.910 に答える