0

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();
        }
    }
}
Natig Babayev
  • 3,128
  • 15
  • 23
Wini
  • 1,906
  • 1
  • 12
  • 31

3 Answers3

1

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

vikas kumar
  • 10,447
  • 2
  • 46
  • 52
  • can't we use intent??passing java and xml code to demo fragment using intent? – Wini Dec 30 '19 at 06:37
  • none of the solutions are understandable for me...see..there is exactly one app is present in play store..it has performed well like java, xml,demo...if that app developer can make that means it is possible..that app is performing demo well based upon java and xml..here is the app link--> https://play.google.com/store/apps/details?id=com.riyazsiddiquiexti.androidlearningtutorial&hl=en – Wini Dec 30 '19 at 07:04
  • 1
    you are taking it differently, those are using some server-side services to compile the java files and they are just displaying the response which they are getting from the server or api. similar to online coding platforms like tutorial point they don;t have any sdk installed in browser to perform these computations its all done in backend they are just showing. and android is very different from Java it is not using the same class files as java does it uses dex files. its also does packaging using aapt it has to be bundled all together to run. – vikas kumar Dec 30 '19 at 07:30
  • okay I got your point...is there a way to insert demo manually ? – Wini Dec 30 '19 at 07:42
  • sorry you can't do that as well. It as to be packaged in smaller apk files at least. there are few implementations but also that are not end to end. There is a dynamic delivery or features module in android its not exact but close have a look. https://developer.android.com/guide/app-bundle/dynamic-delivery – vikas kumar Dec 30 '19 at 09:24
  • Other thing you can try is that using JSON structure. as shown above. have a look at sdk documentation in the link. – vikas kumar Dec 30 '19 at 09:25
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

Rahul sharma
  • 1,492
  • 12
  • 26
  • I think this is the only solution that will work on my problem..I took lot of time think...n this solution actually worked... – Wini Jan 01 '20 at 13:37
  • hey i need your help here --> https://stackoverflow.com/questions/63114801/how-should-i-fetch-images-under-the-array-using-retrofit?noredirect=1#comment111607832_63114801 – Wini Jul 27 '20 at 13:31
0

My Dear Friend, Unfortunately, It is not possible. as you are saying XML and java code from serverside. To run java code we need it to be compiled which is not possible on mobile.

Hope you understand. But, what you can do is you can make a helper class which can parse JSON and returns a view which is generated from JSON and JSON contains element description like elementType, width, height, which can be predefined in server.

Or you can use a Webview that loads a webpage from the server. and to use it in the application. or for some operation use intercept javascript.

Pranav P
  • 1,755
  • 19
  • 39
Amit Goswami
  • 314
  • 2
  • 11