0

Im trying to create a UI for a fragment in an app. When i use onCreateView i have to use a layout file. How do i create a layout from code and use that. I need to do this because my the content for the layout is from the server

This is what i have in my fragment class

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

This is what i want to do

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

1 Answers1

1

You can do that by replacing the inflation of layout with your own Custom View.

    View myCustomView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // No inflation here..
        LinearLayout linearLayout = new LinearLayout(getActivity());
        // Add a TextView, you can add any other view you want
        linearLayout.addView(new TextView(getActivity()));

        myCustomView = linearLayout;

        return myCustomView ;
    }
}
Bidhan
  • 10,607
  • 3
  • 39
  • 50