Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android remove button dynamically

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;
    }
}
like image 276
Totti Avatar asked Jun 23 '12 12:06

Totti


3 Answers

see link

ViewGroup layout = (ViewGroup) button.getParent();
if(null!=layout) //for safety only  as you are doing onClick
  layout.removeView(button);
like image 72
Dheeresh Singh Avatar answered Nov 08 '22 02:11

Dheeresh Singh


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.

like image 20
Paresh Mayani Avatar answered Nov 08 '22 02:11

Paresh Mayani


You can use

      button.setVisibility(View.GONE);
like image 4
Yogesh Somani Avatar answered Nov 08 '22 02:11

Yogesh Somani