labatyo
labatyo

Reputation: 486

Android onClickListener to delete itself

I have a custom LinearLayout class called LabelButton that contains a button and a TextView. I want to have the button's onclick listener delete the LabelButton. How can I pass something from an object of LabelButton to my Activity class, telling my main layout to remove that LabelButton?

public class LabelButton extends LinearLayout {
  private OnClickListener onClick;
    public LabelButton(Context context, final String text) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View labelView = inflater.inflate( R.layout.button_label, this );
        TextView textView = (TextView) labelView.findViewById( R.id.textLabelText);
        textView.setText( text );
        Button button = (Button) labelView.findViewById( R.id.buttonCancelLabel );

        onClick = new OnClickListener( ) {
          public void onClick( View v ) {
            System.out.println("Button: " + text + " clicked");
            // do something here to remove this button
          }
        };
        button.setOnClickListener( onClick );
    }
    @Override
    public void onFinishInflate() {
        super.onFinishInflate();

    }

}

In my Activity class, I add the LabelButtons to a list like this...

//labelButtons is a List of LabelButtons

LabelButton labelButton = new LabelButton( getApplicationContext( ),
            txtBagLabel.getText( ).toString( ) );
        labelButtons.add( labelButton );

Upvotes: 1

Views: 7253

Answers (6)

Hitesh Sahu
Hitesh Sahu

Reputation: 45140

Same thing in KOTLIN:

findViewById<View>(R.id.removeWidget)?.setOnClickListener {
        (parent as ViewGroup).removeView(this)
    }

Upvotes: 0

3vg3
3vg3

Reputation: 21

do like this:

 onClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //remove this button
            ((LinearLayout)v.getParent()).removeView(v);              
        }
 };

then set button that listener

 button.setOnClickListener( onClick );

Upvotes: 0

ZeroHour
ZeroHour

Reputation: 351

You can alias 'this' to a class variable like so:

ViewGroup self = this;

And then you can do the following:

onClick = new OnClickListener( ) {
    public void onClick( View v ) {
        System.out.println("Button: " + text + " clicked");
        // do something here to remove this button
        ViewGroup parent = (ViewGroup) getParent();
        parent.removeView(self);
    }
};

Upvotes: 0

speakingcode
speakingcode

Reputation: 1506

There's two ways. One, you can set the visibility to GONE on the button you want to remove.

labelButton.setVisibility(View.GONE);

If you're doing this from inside the button (i.e. it wants to hide itself) you might try

LabelButton.this.setVisibility(View.GONE);

The second way is to get a reference to the parent view and call

parentView.remove(child);

Where child is of course the reference to your LabelButton object.

Upvotes: 1

dmon
dmon

Reputation: 30168

((ViewGroup) LabelButton.this.getParent()).removeView(LabelButton.this);

Upvotes: 3

StuStirling
StuStirling

Reputation: 16211

Make your button object final and then you can reference it from inside your OnClickListener and maybe change its visibility or remove it from its view.

Upvotes: 0

Related Questions