Reputation: 55
I've created a custom linear layout within android studio. This layout gets inflated into another vertical layout programatically. Now I want to map a button inside this layout, which can delete the whole object. Here is my layout:
And as you can see the button "DELETE HERE" should remove the 3 items, time, weekday and the button itself.
This is my class and here
class AlarmCard @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0,
defStyleRes: Int = 0,
) : LinearLayout(context, attrs, defStyle, defStyleRes) {
init {
LayoutInflater.from(context)
.inflate(R.layout.alarmcard, this, true)
btnDelete.setOnClickListener(){
**/* Call destructor or remove view !?!*/**
}
}
}
which get added to the linear layout with:
val monday = AlarmCard(this)
alarmCards.addView(monday)
The problem is for me how can I delete the object with a button? I tried using alarmCards.removeView(this) within btnDelete.setOnClickListener() but It crashes. :(
Thank you!!
Upvotes: 0
Views: 105
Reputation: 1017
Try this:
btnDelete.setOnClickListener {
(getParent() as? ViewGroup)?.removeView(this@AlarmCard)
}
Upvotes: 1