1

I am trying to make simple android app that uses proteus plugin for creating dynamic view from json file.I am facing issues in passing layout and data in proteus inflator.if anyone can help me with how data is passed to proteus inflator?

2 Answers2

0

layout and data both are passed to the proteus inflator as JSON Object. So if you are using any web service, you have to receive both layout and data as JSON Objects and then create a view using proteusLayoutInflater.inflate(<layout>, <data>) and then add the view to a ViewGroup.

Antonio
  • 1,264
  • 7
  • 12
  • @Prajakta Chavan did you get it? – Antonio Jun 28 '19 at 12:09
  • i was trying to use objectvalue() for for data.trying your answer as well. – Prajakta Chavan Jun 28 '19 at 12:30
  • I can help but probably tomorrow, travelling right now. Try to reverse engineer [proteus-demo](https://github.com/adityasharat/proteus-demo). Also if possible please share the layout and data JSON. Remember to register or use the Proteus Type Adapter Factory with Gson. – vader Jul 01 '19 at 13:33
0

Try the following code in the onCreate of the Activity

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);  // A FrameLayout

final String LAYOUT = "{\n" +
"  \"type\": \"TextView\",\n" +
"  \"textSize\": \"28sp\",\n" +
"  \"text\": \"Hello World!\"\n" +
"}";
final String DATA = "{}";

ViewGroup container = findViewById(R.id.container); // container is the FrameLayout

// create a new instance of proteus
Proteus proteus = new ProteusBuilder().build();

// register proteus with a ProteusTypeAdapterFactory to deserialize proteus jsons
ProteusTypeAdapterFactory adapter = new ProteusTypeAdapterFactory(this);
ProteusTypeAdapterFactory.PROTEUS_INSTANCE_HOLDER.setProteus(proteus);

// deserialize layout and data
Layout layout;
ObjectValue data;
try {
  layout = adapter.LAYOUT_TYPE_ADAPTER.read(new JsonReader(new StringReader(LAYOUT)));
  data = adapter.OBJECT_TYPE_ADAPTER.read(new JsonReader(new StringReader(DATA)));
} catch (IOException e) {
  throw new RuntimeException(e);
}

// create a new ProteusLayoutInflater
ProteusContext context = proteus.createContextBuilder(this).build();
ProteusLayoutInflater inflater = context.getInflater();

// Inflate the layout
ProteusView view = inflater.inflate(layout, data, container, 0);

// Add the inflated layout into the container
container.addView(view.getAsView());

Check out simple-proteus-demo

vader
  • 889
  • 8
  • 22