prody
prody

Reputation: 15

How can I remove / delete a dynamically generated button in Android?

So I generated some button. The numbers it depends on the user (when clicked a button, create a new one).

This is how I made it:

   RelativeLayout layout = (RelativeLayout) findViewById(R.id.layoutcprompt);
    RelativeLayout.LayoutParams OBJ = new RelativeLayout.LayoutParams (140,80);
    if ((commandsi%6)==0) {adjust=adjust+86; commandsi=1;}
    OBJ.leftMargin =(140*(commandsi-1))+10;
    OBJ.topMargin =250+adjust;
    Button command = new Button(this);
    command.setLayoutParams(OBJ);
    command.setId(ID);
    command.setText(edittxt.getText().toString());
    edittxt.setText("");
    command.setBackgroundResource(R.drawable.costum_button);
    command.setTextColor(Color.WHITE);
    command.setTextSize(14);
    layout.addView(command);
    command.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Button b = (Button)view;
            scommand=b.getText().toString();
        }
    });
    command.setVisibility(View.VISIBLE);

I want to remove/delete them, but I don't know how.... I gave them a unique id, but I still dunno how can I remove them :/

Upvotes: 1

Views: 6719

Answers (6)

Amaresh Jana
Amaresh Jana

Reputation: 742

You can also use this snipper

For Adding the Button

        LinearLayout dynamicview = (LinearLayout)findViewById(R.id.buttonlayout);
        LinearLayout.LayoutParams  lprams = new LinearLayout.LayoutParams(  LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

            Button btn = new Button(this);
            btn.setId(count);
            final int id_ = btn.getId();
            btn.setText("Capture Image" + id_);
            btn.setTextColor(Color.WHITE);
            btn.setBackgroundColor(Color.rgb(70, 80, 90));
            dynamicview.addView(btn, lprams);
            btn = ((Button) findViewById(id_));
            btn.setOnClickListener(this);

For removing the button

            ViewGroup layout = (ViewGroup) findViewById(R.id.buttonlayout);
            View command = layout.findViewById(count);
            layout.removeView(command);

Upvotes: 0

transcend3nt
transcend3nt

Reputation: 101

One other implementation that may help others who view this thread is that you can consider removing all the child elements in the parent layout. Once you get the view's parent (which I assume is a layout container),you can remove all the child elements.

command.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            ViewGroup vg = (ViewGroup)view.getParent();
            vg.removeAllViews();
    }
});

Upvotes: 1

Täg
Täg

Reputation: 431

I cannot comment another post, but using

command = new Button(this)

might involve an implicit memory Leak on this! (which can be the Activity). Rather use Context object. Or remove the button at least.

Then because you have the parent of your Button. Just remove it:

ViewGroup layout = (ViewGroup) findViewById(R.id.layoutcprompt);
View command = layout.findViewById(ID);
layout.removeView(command);

Upvotes: 3

caiocpricci2
caiocpricci2

Reputation: 7788

Try using the documentation, 5 seconds of research can lead you to the RemoveView method.

layout.removeView(command);

Update

If you have a null pointer exception on this line, means your layout is null, not your command. Make your layout variable global for that class.

Also be sure to keep different variables for each of your created buttons. If you have a global variable, and create 10 buttons using the same variable you will only have a reference to the last one created. If you explain exactly when you want to remove the button we might be able to help you further.

As an example, if you want to remove the button when the user clicks on it you can change your clickListener:

command.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Button b = (Button)view;
            scommand=b.getText().toString();
            layout.removeView(view);    
    }
    });

Upvotes: 1

Vikram Singh
Vikram Singh

Reputation: 1420

You can also do it like this for security purpose:

ViewGroup layout = (ViewGroup) command.getParent();
if(null!=layout) //for safety only  as you are doing onClick
layout.removeView(command);

Upvotes: 0

IAmGroot
IAmGroot

Reputation: 13855

Make command a global variable. Then you can access it later, and call command.setVisibility(View.GONE);

So at the top, of your class file, you would declare the global variable:

Button command;

Then remove the redefinition later on and instead assign to the global variable:

command = new Button(this);

Then when you want to hide it, call:

command.setVisibility(View.GONE);

Upvotes: 2

Related Questions