ackfloverstow
ackfloverstow

Reputation: 81

Android Toolbar will not display

I am trying to create a toolbar. I call: setSupportActionBar(binding.appBarMain.toolbar);

However, nothing appears at the top of the application.

Here is my toolbar xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:theme="@style/AppTheme.AppBarOverlay">


        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_marginBottom="676dp"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:popupTheme="@style/AppTheme.PopupOverlay" />



    </com.google.android.material.appbar.AppBarLayout>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

I have a navigation that slides in from the left and works fine, however I would like a toolbar that displays the navigation as well.

style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/black</item>
        <item name="colorPrimaryDark">@color/black</item>
        <item name="colorAccent">@color/black</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:textColorPrimary">@color/black</item>
        <item name="android:textColorSecondary">@color/black</item>
    </style>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="AppTheme.NoActionBar" parent="AppTheme">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

</resources>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <include
        android:id="@+id/app_bar_main"
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
    <androidx.fragment.app.FragmentContainerView
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:id="@+id/nav_host_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

    <!-- Container for contents of drawer - use NavigationView to make configuration easier -->
    <fragment
        android:id="@+id/nav_host_fragment_content_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/mobile_navigation"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


    <FrameLayout
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:visibility="visible"
        android:layout_weight="9"
        app:layout_constraintTop_toTopOf="@+id/nav_view">

        <EditText
            android:id="@+id/editTextTextPersonName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="50dp"
            android:textColor="#000000"
            android:ems="10"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/txtLabelLogin"
            android:layout_width="115dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="275dp"
            android:layout_marginTop="50dp"
            android:textColor="#000000"
            android:ems="10"
            android:enabled="false"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/txtLabelUsername"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="50dp"
            android:textColor="#000000"
            android:editable="false"
            android:ems="10"
            android:enabled="false"
            android:inputType="textPersonName"
            android:text="UN:" />

        <EditText
            android:id="@+id/txtPW"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="100dp"
            android:textColor="#000000"
            android:ems="10"
            android:inputType="textPassword" />

        <TextView
            android:id="@+id/txtTimer"
            android:layout_width="225dp"
            android:layout_height="25dp"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="165dp"
            android:textColor="#000000"
            android:ems="25"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/txtLabelPassword"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="100dp"
            android:textColor="#000000"
            android:ems="10"
            android:enabled="false"
            android:inputType="textPersonName"
            android:text="PW:" />

        <Button
            android:id="@+id/btnLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="275dp"
            android:layout_marginTop="100dp"
            android:backgroundTint="@color/purple_200"
            android:text="Login" />

        <Button
            android:id="@+id/btnPrint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="275dp"
            android:layout_marginTop="150dp"
            android:backgroundTint="@color/purple_200"
            android:enabled="false"
            android:text="Print" />

        <EditText
            android:id="@+id/txtClockTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="200dp"
            android:textColor="#000000"
            android:editable="false"
            android:ems="10"
            android:enabled="false"
            android:inputType="time" />

        <EditText
            android:id="@+id/txtGPS"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="275dp"
            android:textColor="#000000"
            android:editable="false"
            android:ems="10"
            android:enabled="false"
            android:inputType="textMultiLine" />

        <Button
            android:id="@+id/btnClockIn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="400dp"
            android:backgroundTint="@color/teal_200"
            android:enabled="false"
            android:text="Clock In" />

        <Button
            android:id="@+id/btnClockOut"
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="250dp"
            android:layout_marginTop="400dp"
            android:backgroundTint="@color/teal_200"
            android:enabled="false"
            android:text="Clock Out" />


        <Button
            android:id="@+id/btnBreak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="150dp"
            android:layout_marginTop="400dp"
            android:backgroundTint="@color/teal_200"
            android:enabled="false"
            android:text="Break" />


        <fragment
            android:id="@+id/nav_host_fragment_activity_main"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="600dp"
            app:defaultNavHost="true"
            app:navGraph="@navigation/mobile_navigation" />

        <EditText
            android:id="@+id/txtGUID"
            android:layout_width="363dp"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:ems="10"
            android:enabled="false"
            android:inputType="text" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:layout_weight="1"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav_menu" />

    </FrameLayout>


    <FrameLayout
        android:id="@+id/rootNotify"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:visibility="gone"
        tools:layout_editor_absoluteY="56dp"
        android:layout_weight="9"
        app:layout_constraintTop_toTopOf="@+id/nav_view">

        <EditText
            android:id="@+id/txtLabelTodayHrs"
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="0dp"
            android:textColor="#000000"
            android:editable="false"
            android:ems="10"
            android:enabled="false"
            android:inputType="textPersonName"
            android:text="Today Hours:" />

        <EditText
            android:id="@+id/txtLabelTodayHrsVal"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="130dp"
            android:layout_marginTop="0dp"
            android:textColor="#000000"
            android:editable="false"
            android:ems="10"
            android:enabled="false"
            android:inputType="textPersonName"
            android:text="" />

        <EditText
            android:id="@+id/txtLabelWeekHrs"
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="25dp"
            android:textColor="#000000"
            android:editable="false"
            android:ems="10"
            android:enabled="false"
            android:inputType="textPersonName"
            android:text="Week Hours:" />

        <EditText
            android:id="@+id/txtLabelWeekHrsVal"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="130dp"
            android:layout_marginTop="25dp"
            android:textColor="#000000"
            android:editable="false"
            android:ems="10"
            android:enabled="false"
            android:inputType="textPersonName"
            android:text="" />

        <EditText
            android:id="@+id/txtLabelHrsLeft"
            android:layout_width="125dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="50dp"
            android:textColor="#000000"
            android:editable="false"
            android:ems="10"
            android:enabled="false"
            android:inputType="textPersonName"
            android:text="Hours Left:" />

        <EditText
            android:id="@+id/txtLabelHrsLeftVal"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="130dp"
            android:layout_marginTop="50dp"
            android:textColor="#000000"
            android:editable="false"
            android:ems="10"
            android:enabled="false"
            android:inputType="textPersonName"
            android:text="" />

        <Button
            android:id="@+id/btnRefreshNotify"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="150dp"
            android:layout_marginTop="400dp"
            android:backgroundTint="@color/teal_200"
            android:text="Refresh" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view_notify"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:layout_weight="1"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav_menu" />

    </FrameLayout>

    <FrameLayout
        android:id="@+id/rootDash"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:visibility="gone"
        tools:layout_editor_absoluteY="56dp"
        android:layout_weight="9"
        app:layout_constraintTop_toTopOf="@+id/nav_view">

        <org.osmdroid.views.MapView
            android:id="@+id/mapView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"
            android:focusable="true" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view_dash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:layout_weight="1"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav_menu" />

    </FrameLayout>

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view_top"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/drawer_menu"/>

</androidx.drawerlayout.widget.DrawerLayout>

MainActivity:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        
        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navViewTop;
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.

        //NavController navControllerTop = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        NavController navControllerTop = navHostFragment.getNavController();

        mAppBarConfiguration = new AppBarConfiguration.Builder(
                navControllerTop.getGraph())
                .setOpenableLayout(drawer)
                .build();

        NavController navControllerMenu = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        NavigationUI.setupActionBarWithNavController(this, navControllerMenu, mAppBarConfiguration);
        //NavigationUI.setupWithNavController(navigationView, navControllerMenu);
        navigationView.setNavigationItemSelectedListener(...

Upvotes: 0

Views: 62

Answers (1)

Beant Singh
Beant Singh

Reputation: 179

either add toolbar like below

<include
    android:id="@+id/app_bar_main"
    layout="@layout/item_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view_top"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"/>

or display your fragments below toolbar

Upvotes: 1

Related Questions