Search code examples
androidandroid-toolbarandroidxandroid-themeandroid-appbarlayout

Toolbar is not showing below API 21 in Android


Toolbar is not showing when run below API 21. Currently, I am running this app it in API 19 emulator. This same layout works properly in devices running above and equal to API 21.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".pdfScreen.PdfActivity">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/pdf_activity_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize">

            </androidx.appcompat.widget.Toolbar>


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

        <com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdfView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <include
            android:id="@+id/include_pdf_menu"
            layout="@layout/pdf_activity_bottom_menu"/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

Here is the style.xml

 <style name="themeForApp" parent="Theme.AppCompat.Light.NoActionBar">
         Customize your theme here.
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColor">@color/textOnPrimary</item>
    </style>



    <style name="pdfViewTheme" parent="themeForApp.NoActionBar">
    </style>

Here is v21/style.xml.

    <style name="themeForApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>


    <style name="pdfViewTheme" parent="themeForApp">
        <item name="android:windowFullscreen">true</item>
    </style>

Here is the AndroidManifest.xml where I am applying pdfViewTheme to my activity

<activity android:name=".pdfScreen.PdfActivity" android:theme="@style/pdfViewTheme"/>

Solution

  • The issue is the android:layout_height="match_parent"

      <com.github.barteksc.pdfviewer.PDFView
          android:layout_height="match_parent" />
    

    In this way the PDFView covers the whole screen including the Toolbar with API<21.
    The reason is that AppBar/Toolbar has a default elevation and is positioned at higher elevations than others. It doesn't work on API<21.