How do I add and remove views such as TextView
s from Android app like on the original stock Android contacts screen where you press a small icon on the right side of a field and it adds or deletes a field which consists of a TextView
and an editTextView
(from what I can see).
Any examples on how to achieve this?
ViewParent
s in general can't remove views, but ViewGroup
s can. You need to cast your parent to a ViewGroup
(if it is a ViewGroup
) to accomplish what you want.
For example:
View namebar = View.findViewById(R.id.namebar);
((ViewGroup) namebar.getParent()).removeView(namebar);
Note that all Layout
s are ViewGroup
s.