Search code examples
androidandroid-layoutlayoutviewandroid-linearlayout

How I can save a linear layout and all its inner views and use it later android


I have one activity and inside it I have a Linear layout (lets name it the "main linear layout") and I am adding dynamically created view inside it (text views, linear layouts, edit texts, ...etc).

In the bottom of the screen there are two button (next and back).

If the user clicked on next button I should save the current "main linear layout " in a list for example and then I am generating a new views and I add it inside the "main linear layout" then .

And if the user clicked on back button I should restore the "main linear layout" and show all its views.

I don't know how I can do it.

I hope my description is clear :)


Solution

  • Thank you all for your comments.

    I haven't test Cyrus answer. I will post my answer as well in case someone has the same problem in Xamarin.Android since I am using it.

    I fixed it like this:

    I added this in next button click event and previous button click event:

    AddQuestionsLinearLayoutToPagesLinearLayoutsList();
    

    And the AddQuestionsLinearLayoutToPagesLinearLayoutsList() body is, of course you can change it the way you want:

    private void AddQuestionsLinearLayoutToPagesLinearLayoutsList()
        {
             //Create a new linear layout to add views into it and saved it in _mPagesLinearLayouts list at index _mPageIndex
             LinearLayout savedLinearLayout = new LinearLayout(this);
    
             //List of _mQuestionsLinearLayout views
             List<View> views = new List<View>();
    
             //Add all _mQuestionsLinearLayout to views list
             for (int i = 0; i < _mQuestionsLinearLayout.ChildCount; i++)
             {
                 views.Add(_mQuestionsLinearLayout.GetChildAt(i));
             }
    
             //Remove views from main questions linear layout
             _mQuestionsLinearLayout.RemoveAllViews();
    
             //Add all views from the views list to savedLinearLayout
             for (int i = 0; i < views.Count; i++)
             {
                 savedLinearLayout.AddView(views[i]);
             }
    
             //Add savedLinearLayout to _mPagesLinearLayouts list at index _mPageIndex
             _mPagesLinearLayouts.Insert(_mPageIndex, savedLinearLayout);
        }
    

    In creating views method:

        if(NeedToCreateViews())
        {
    //create views here and add it to _mQuestionsLinearLayout
    }
    //Get all saved views and add it to _mQuestionsLinearLayout
    else
                {
                    //List of _mPagesLinearLayouts[_mPageIndex] LinearLayout views
                    List<View> views = new List<View>();
    
                    //Add all _mPagesLinearLayouts[_mPageIndex] LinearLayout to views list
                    for (int i = 0; i < _mPagesLinearLayouts[_mPageIndex].ChildCount; i++)
                    {
                        views.Add(_mPagesLinearLayouts[_mPageIndex].GetChildAt(i));             
                    }
    
                    //Remove all views from  _mPagesLinearLayouts[_mPageIndex] linear layout
                    _mPagesLinearLayouts[_mPageIndex].RemoveAllViews();
    
                    //Remove the linear layout at index _mPageIndex
                    _mPagesLinearLayouts.RemoveAt(_mPageIndex);
    
                    //Add all views from views list to _mQuestionsLinearLayout
                    for (int i = 0; i < views.Count; i++)
                    {
                        _mQuestionsLinearLayout.AddView(views[i]);
                    }
    
                }