how to run java and xml code (fetched from api) into another fragment

okay I will breifly define what I exactly want.I'm getting an java code(for textview, button,etc) from api like this--> image description here and Xml code(for textview, button,etc)from api like this--> image description here..what I want is to run these java and xml and display it like this--> enter image description here.. I really have no idea how to do that..can anyone help..I will post up java/xml code and its adapters following please have look ..I haven't done anything in demo fragment..what should I do in demo fragment???

java:

public class JavaFragment extends Fragment {

    private RecyclerView recyclerView;
    private NextSLJavaAdapter adapter;
    private NextSLModel DescriptList;
    ProgressDialog progressDialog;
    public JavaFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.nextsl_layout, container, false);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Toolbar toolbar = (Toolbar) getView().findViewById(R.id. toolbar );
       // setSupportActionBar( toolbar );
        //if (getSupportActionBar() != null) {
          //  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
           // getSupportActionBar().setDisplayShowHomeEnabled(true);
        //}
        progressDialog = new ProgressDialog(getContext());
        progressDialog.setMessage("Loading....");
        progressDialog.show();
        Intent intent = getActivity().getIntent();
        String title = intent.getStringExtra("title");
        //getSupportActionBar().setTitle(title);
        String id = intent.getStringExtra("idSLnext");
        Log.e("ashwini", String.valueOf(id));


        /*Create handle for the RetrofitInstance interface*/
        SLApiSevice service = SLApiClient.getRetrofitInstance().create(SLApiSevice.class);
        Call<NextSLModel> call = service.getnextslmodel(id);
        call.enqueue(new Callback<NextSLModel>() {
            @Override
            public void onResponse(Call<NextSLModel> call, Response<NextSLModel> response) {
                progressDialog.dismiss();
                DescriptList=response.body();
                generateDataList(DescriptList);
            }

            @Override
            public void onFailure(Call<NextSLModel> call, Throwable t) {
                 progressDialog.dismiss();

                Toast.makeText(getContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
            }
        });

    }
    private void generateDataList(NextSLModel photoList) {
        recyclerView = getView().findViewById(R.id.nextSLrecycle);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        adapter = new NextSLJavaAdapter(getContext(),photoList);
        recyclerView.setAdapter(adapter);
    }
}

java adapter:

public class NextSLJavaAdapter extends RecyclerView.Adapter<NextSLJavaAdapter.CustomViewHolder> {

    NextSLModel Slmdel;
    Context context;

    public NextSLJavaAdapter(Context context, NextSLModel employees) {
        this.Slmdel = employees;
        this.context = context;
    }

    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.nextsl_item, parent, false);

        return new CustomViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(CustomViewHolder holder, int position) {

        holder.employeeName.setText(Slmdel.getJava());
        Log.e("sl",Slmdel.getJava());


    }

    @Override
    public int getItemCount() {
        return 1;
        //return (employees == null) ? 0 : employees.size();

    }

    public class CustomViewHolder extends RecyclerView.ViewHolder {
        public TextView employeeName;
        TextView textView;

        public CustomViewHolder(View view) {
            super(view);
            employeeName = (TextView) view.findViewById(R.id.detailsStartLearning);
            textView = view.findViewById(R.id.Sl1);
        }
    }
}

xml code:

public class XMLFragmet extends Fragment {
    private RecyclerView recyclerView;
    private NextSLXmlAdapter adapter;
    private NextSLModel DescriptList;
    ProgressDialog progressDialog;
    public XMLFragmet() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.nextsl_layout, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        /*Create handle for the RetrofitInstance interface*/
        progressDialog = new ProgressDialog(getContext());
        progressDialog.setMessage("Loading....");
        progressDialog.show();
        Intent intent = getActivity().getIntent();

        String id = intent.getStringExtra("idSLnext");
        Log.e("ashwini", String.valueOf(id));
        SLApiSevice service = SLApiClient.getRetrofitInstance().create(SLApiSevice.class);
        Call<NextSLModel> call = service.getnextslmodel(id);
        call.enqueue(new Callback<NextSLModel>() {
            @Override
            public void onResponse(Call<NextSLModel> call, Response<NextSLModel> response) {
                 progressDialog.dismiss();
                DescriptList = response.body();
                generateDataList(DescriptList);
            }

            @Override
            public void onFailure(Call<NextSLModel> call, Throwable t) {
                 progressDialog.dismiss();

                Toast.makeText(getContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
            }
        });

    }
    private void generateDataList(NextSLModel photoList) {
        recyclerView = getView().findViewById(R.id.nextSLrecycle);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        adapter = new NextSLXmlAdapter(getContext(),photoList);
        recyclerView.setAdapter(adapter);
    }
}

xml adapter:

public class NextSLXmlAdapter extends RecyclerView.Adapter<NextSLXmlAdapter.CustomViewHolder> {

    NextSLModel Slmdel;
    Context context;

    public NextSLXmlAdapter(Context context, NextSLModel employees) {
        this.Slmdel = employees;
        this.context = context;
    }

    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.nextsl_item, parent, false);

        return new CustomViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(CustomViewHolder holder, int position) {

        holder.employeeName.setText(Slmdel.getXml());


    }

    @Override
    public int getItemCount() {
        return 1;
        //return (employees == null) ? 0 : employees.size();

    }

    public class CustomViewHolder extends RecyclerView.ViewHolder {
        public TextView employeeName;
        TextView textView;

        public CustomViewHolder(View view) {
            super(view);
            employeeName = (TextView) view.findViewById(R.id.detailsStartLearning);
            textView = view.findViewById(R.id.Sl1);
        }
    }
}

demo activity:

public class DemoFragment extends Fragment {


        public DemoFragment() {
            // Required empty public constructor
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_demo, container, false);
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
           Toast.makeText(getContext(),"hi Edit",Toast.LENGTH_LONG).show();

            Intent intent = getActivity().getIntent();


            String id = intent.getStringExtra("idSLnext");
            Log.e("demo", id);

            if(id.matches("11"))
          {
            ///can't I put logic over here which im getting from api(java and xml)
              Toast.makeText(getContext(),"hi textview",Toast.LENGTH_LONG).show();;
          }
            else if(id.matches("10"))
            {
                Toast.makeText(getContext(),"hi Edit",Toast.LENGTH_LONG).show();
            }
            else if(id.matches("9"))
            {
                Toast.makeText(getContext(),"hi Imageview",Toast.LENGTH_LONG).show();
            }
            else if(id.matches("8"))
            {
                Toast.makeText(getContext(),"hi Button",Toast.LENGTH_LONG).show();
            }
            else if(id.matches("7"))
            {
                Toast.makeText(getContext(),"hi CheckBox",Toast.LENGTH_LONG).show();
            }
            else if(id.matches("6"))
            {
                Toast.makeText(getContext(),"hi RadioButton & RadioGroup",Toast.LENGTH_LONG).show();
            }
            else if(id.matches("5"))
            {
                Toast.makeText(getContext(),"hi DatePicker",Toast.LENGTH_LONG).show();
            }
            else if(id.matches("4"))
            {
                Toast.makeText(getContext(),"hi TimePicker",Toast.LENGTH_LONG).show();
            }
            else if(id.matches("3"))
            {
                Toast.makeText(getContext(),"hi DatePicker",Toast.LENGTH_LONG).show();
            }
            else if(id.matches("2"))
            {
                Toast.makeText(getContext(),"hi Switch",Toast.LENGTH_LONG).show();
            }
            else if(id.matches("1"))
            {
                Toast.makeText(getContext(),"hi Simple & custom Toast",Toast.LENGTH_LONG).show();
        }
    }
}

Solution 1:

There two way to add view dynamically .

  1. you create you xml file .then using LayoutInflater you lnflater that xml to your view .

  2. Or You can create view dynamically like var textview=TextView() then textview.text="xyz"

You need relativelayout or linearlayout where will add those will using addView(); method.

But i condition you have declare your view then you can customize at runtime.

you can check the example

Solution 2:

Remote code injection is not supported from the very beginning in android. The framework developers may have thought from the security perspective as well as the limited computation that a mobiles have. Please note android studio build process converts these java, xml files into dex files which can't be done in runtime so its not supported.

Important For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

The alternative approach is to use some JSON format for the views and properties and it will draw the widgets dynamically on a blank canvas will add it to the layout parent.

for example.

    {
  "type": "LinearLayout",
  "orientation": "vertical",
  "padding": "16dp",
  "children": [{
    "layout_width": "200dp",
    "gravity": "center",
    "type": "TextView",
    "text": "@{user.profile.name}"
  }, {
    "type": "HorizontalProgressBar",
    "layout_width": "200dp",
    "layout_marginTop": "8dp",
    "max": 6000,
    "progress": "@{user.profile.experience}"
  }]
}

This will add a LinearLayout with Childerns as TextView, HorizontalScrollBar. and data can also be inserted like below.

    {
  "user": {
    "profile": {
      "name": "John Doe",
      "experience": 4192
    }
  }
}

On how to use this SDK hit the below link. https://github.com/flipkart-incubator/proteus