setContentView(R.layout.new_layout) to change layout, but then GoogleMap fragment is blank

49 views Asked by At

My app displays a GoogleMap fragment successfully. But when clicking the "expand" button to load a new layout xml file, the map is now blank. I could load a new activity for the new layout, but this would duplicate quite a bit of code. Ideally, I would like to click the expand button and have the new layout load with the same map, but with reduced clutter on the screen.

I am unsure about how to handle the button click. I initially tried setContentView(R.layout.activity_create_route_expanded); but then none of the buttons were clickable. My current attempt inflates the new layout and then calls onCreate again
createRouteLayout = getLayoutInflater().inflate(R.layout.activity_create_route, null); Everything works except the blank map.

Here is a simplified version of my code:

public class CreateRouteActivity extends FragmentActivity implements OnMapReadyCallback {

    private View createRouteLayout;
    private ViewModelCreateRoute viewModelCreateRoute; // Save variable state when changing the layout

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // If user has changed to/from expanded view, use the saved layout. Otherwise default to activity_create_route
        if (createRouteLayout == null) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_create_route);
        } else {
            setContentView(createRouteLayout);
        }

        viewModelCreateRoute = new ViewModelProvider(this).get(ViewModelCreateRoute.class);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapCreateRoute);
        mapFragment.getMapAsync(this); //See the callback below: onMapReady()

        findViewById(R.id.createRouteExpandButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (viewModelCreateRoute.isExpanded()) {
                    viewModelCreateRoute.toggleExpanded();
                    createRouteLayout = getLayoutInflater().inflate(R.layout.activity_create_route, null);
                } else {
                    viewModelCreateRoute.toggleExpanded();
                    createRouteLayout = getLayoutInflater().inflate(R.layout.activity_create_route_expanded, null);
                }
                onCreate(savedInstanceState);
            }
        });
    }

    @Override
    public void onMapReady(GoogleMap gMap) {
        viewModelCreateRoute.getMapDisplayContainer().setgMap(gMap);
        MapUtils.populateMap(viewModelCreateRoute.getMapDisplayContainer(), true, false);
    }
}
1

There are 1 answers

0
beebopbogo On

I still can't find a way to change the ContentView dynamically while displaying the map, but I did find an easier way to accomplish my goal:

Rather than building up a new layout, just change the ConstraintLayout dynamically:

In the Activity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "CreateRouteActivity: onCreate: ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_route);

        findViewById(R.id.createRouteExpandButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(viewModelCreateRoute.isExpanded()){
                    viewModelCreateRoute.toggleExpanded();
                    shrinkLayout();

                } else {
                    viewModelCreateRoute.toggleExpanded();
                    expandLayout();
                }
            }
        });
    ...
    }

    private void expandLayout(){
        RecyclerView recyclerView = findViewById(R.id.createRouteRecycle);
        EditText raceInstructions = findViewById(R.id.raceInstructionsEditText);
        recyclerView.setVisibility(View.GONE);
        raceInstructions.setVisibility(View.GONE);

        ConstraintLayout constraintLayout = findViewById(R.id.createRouteConstraintLayout);
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(constraintLayout);
        constraintSet.connect(R.id.createRouteStartText,ConstraintSet.BOTTOM,R.id.saveRouteButton,ConstraintSet.TOP,0);
        constraintSet.applyTo(constraintLayout);
    }

    private void shrinkLayout(){
        RecyclerView recyclerView = findViewById(R.id.createRouteRecycle);
        EditText raceInstructions = findViewById(R.id.raceInstructionsEditText);
        recyclerView.setVisibility(View.VISIBLE);
        raceInstructions.setVisibility(View.VISIBLE);

        ConstraintLayout constraintLayout = findViewById(R.id.createRouteConstraintLayout);
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(constraintLayout);
        constraintSet.connect(R.id.createRouteStartText,ConstraintSet.BOTTOM,R.id.createRouteRecycle,ConstraintSet.TOP,0);
        constraintSet.applyTo(constraintLayout);
    }