androidandroid-layoutandroid-identifiersproteus

Get a list of TextFields that are available during runtime?


I'm using an android library called Proteus which inflates layouts during run time with a JSON file we have sitting on our server. This library allows for data binding but my question is how can I get data out of these fields while using my app? I can't reference any of the identifiers of the layout because they don't exist during compilation. Is there a way to ignore the error in the statement when (R.id.input) doesn't actually exist until runtime?:

View view = inflater.infalte(R.layout.fragment_form, container, false);
TextView textView = (TextView)view.findViewById(R.id.input);

Or possibly programmatically get a list of all available unique identifiers during runtime? Any info would be helpful. Thank you.


Solution

  • proteus generates view ids at runtime, unlike the int ids generated at compile time for Android XML layouts. Unlike Android the IDs of Views created by proteus are based on String, and the R.id.<something> will not work.

    Proteus provides utility methods to find a view by it's String id.

    Layout:

    {
      "type": "TextView",
      "id": "myId"
    }
    

    Java:

    View view = proteusView.getViewManager().findViewById("myId"); // can be null
    

    The returned view can also be a ProteusView

    Please refer to the wiki for more information.

    The wiki is slightly outdated, the answer above is for 5.0.0-rc11 release and above. The wiki should be updated soon.