notReallyDev
notReallyDev

Reputation: 179

Toolbar not visible after adding ConstraintLayout

I am adding a ConstraintLayout to a LinearLayout with a toolbar. Adding the ConstraintLayout causes the toolbar to no longer be visible, and the Layout Inspector shows that the toolbar has visibility GONE though the debugger shows VISIBLE. Am I missing something here? Using LayoutParams.WRAP_CONTENT doesn't work either.

    protected void setView(View view) {
            super.setContentView(R.layout.content_view);

            toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            LinearLayout layout = findViewById(R.id.linear_layout);
            layout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }

content_view.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linear_layout"
    android:orientation="vertical">
    <include layout="@layout/toolbar"/>
</LinearLayout>

view being passed in:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white">
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph"
        app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Views: 253

Answers (1)

kami
kami

Reputation: 26

You have a few things that you may be missing.

  1. the picture is not complete - Why adding the view dynamically? Try to add it in the layout xml with <include layout="@layout/[NAME_OF_THE_VIEW_LAYOUT]"/> and comment out the java code - just to be sure.
  2. You are adding to LinearLayout, but using ViewGroup.LayoutParams, and that is not OK, use the LinearLayout.LayoutParams instead.
  3. This may not be related to the missing toolbar, but inside the ConstraintLayout there may be Nothing. Put a simple textview or change the color to something very visible (for example #00F) to be sure that it is added.

Upvotes: 1

Related Questions