Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove layouts except first one

When the below method is called the dynamic layout will be filled to the parent_view.

public void Generate_tour_plan(String mark, OnClickListener o) {
        parent_view = (LinearLayout) rootView
                .findViewById(R.id.tour_iternary_wrapper);
        hiddenInfo1 = getActivity().getLayoutInflater().inflate(
                R.layout.tour_plan, null, false);
        Button minus = (Button) hiddenInfo1.findViewById(R.id.plus);
        minus.setText(mark);
        CustomAutoCompleteTextView dealer_search = (CustomAutoCompleteTextView) hiddenInfo1
                .findViewById(R.id.account_code);
        CustomAutoCompleteTextView town_search = (CustomAutoCompleteTextView) hiddenInfo1
                .findViewById(R.id.town);
        town_search.setAdapter(city_adapter);
        dealer_search.setAdapter(adapter);
        minus.setOnClickListener(o);

        parent_view.addView(hiddenInfo1, 0);

    }

Here external layout will be added like a stack eg.(5,4,3,2,1), so I need to remove all the layout except first one.

for (int j2 = parent.getChildCount() - 1; j2 > 0; j2--) {
                    Log.i("TAG IS", j2 + "");
                    ViewGroup vv = (ViewGroup) parent.getChildAt(j2);
                    vv.removeAllViews();

                }

Above code is for removing the items top to bottom.But this works on bottom to top. I want to remove the item like a stack.

like image 236
supun lakshan Avatar asked Jul 31 '14 03:07

supun lakshan


3 Answers

You can use the ViewGroup.removeViews(start, count) method.

To remove all child views/layouts except the first one:

layout.removeViews(1, layout.getChildCount() - 1)

like image 135
Ahmad Melegy Avatar answered Oct 13 '22 22:10

Ahmad Melegy


You can remove a Child View from a parent by calling removeView(View view), for example like this :

 parent.removeView(child);
like image 1
VVB Avatar answered Oct 13 '22 21:10

VVB


int count = (your viewGroup).getChildCount() - 1;
for (int i = count; i > 0; i--) {
    (your viewGroup).removeViewAt(i);
}
like image 1
Vijay Makwana Avatar answered Oct 13 '22 22:10

Vijay Makwana