Serialize Android view to xml layout

Hi, I'm working on Xamarin.Android app which dynamically creates UI based on external elements.
My question is if can I save my dynamically created view as android layout xml file to be able to load that in future with setContentView(R.layout.layout_name) or inflater.Inflate().

UI is builded like this:

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
          var view = inflater.Inflate(Resource.Layout.root_layout, null);
          var layout = view as LinearLayout;
          layout.AddView(BuildScreen());
          return view;
    }

where BuildScreen returns whole UI Tree as object.

root_layout.xml only contains this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">  
    </LinearLayout>

Goal is to save view object with all dynamically created children as valid xml layout resource file.

Tagged:

Answers

  • suraj007suraj007 Member ✭✭✭

    i think dynamically created view load only use of inflater.Inflate().

  • LandLuLandLu Member, Xamarin Team Xamurai

    Create a public utilization class where you can define a static class returns the view you needed.

    public class UtliTools
    {
        public static View BuildScreen(Context context)
        {
                // ...
        }
    }
    

    And you can pass the view directly to the SetContentView(View): https://docs.microsoft.com/en-us/dotnet/api/android.app.activity.setcontentview?view=xamarin-android-sdk-9#Android_App_Activity_SetContentView_Android_Views_View_

  • Mike456Mike456 Member ✭✭

    Problem is not how to show View in Activity or Fragment. But I need that dynamically created view (object of type View) serialized to xml in valid android layout to be used in future.
    Method oposite to SetContentView(View), like take what is currently shown on screen and save it as XML.

  • LandLuLandLu Member, Xamarin Team Xamurai

    No, we won't do that.
    If you dynamically created views using some methods then make it as a static one so that you can reuse it somewhere else.
    We can't convert it back to xml files.
    We only store data to storage instead of interfaces.

  • Mike456Mike456 Member ✭✭

    Ok, I'll have to make custom serialization format then to save it into file.
    Thanks for your help.

  • LandLuLandLu Member, Xamarin Team Xamurai

    @Mike456 Yes you can iterate your view group to obtain all the children elements. Then record it with some special identifiers. However, this is not easy to achieve.

Sign In or Register to comment.