How to move the setContentView(View view) method to a fragment

99 views Asked by At

I'm using a library (VTM) that was designed to be used with activities, and requires the setContentView method passing it a view, like this:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MapView mapView = new MapView(this);
    setContentView(mapView); 
}

I want to move the class to a fragment. The MapView constructor does some initialization inside the library and then it needs to be set in the setContentView method, so I can't inflate the layout using the id of an xml layout (which is what the LayoutInflator requires. I want to do something like this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(new MapView(requireContext()), container, false);
}

but the inflate method requires the id to a layout xml. How can I inflate the mapView instead?

1

There are 1 answers

0
hata On BEST ANSWER

Generally, the onCreateView() requires to return a View instance. So, you can return your MapView instance itself. Hope it works.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return new MapView(requireContext());
}