josemigallas josemigallas - 4 months ago 26
Android Question

Android Toolbar behind Status Bar on API 19

I have been struggled with this for days now and have not found out a solution yet. I have a NavigationDrawer activity with a Toolbar and several fragments, simple as that. Inexplicably, the Toolbar remains behind the Status Bar ONLY on Android 4.4 (API 19).

enter image description here

I have used everything I found, I even copied the code from another apps where this is working but nothing changed.

This is all relevant code:

build.gradle

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'realm-android'

android {

compileSdkVersion 24
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "xxx"
minSdkVersion 16
targetSdkVersion 24
versionCode 3
versionName "1.0"
}

signingConfigs {
...
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
debug {
debuggable true
useProguard false
applicationIdSuffix ".debug"
}
}
}

repositories {
mavenCentral()
maven { url "https://jitpack.io" }
maven { url 'https://maven.fabric.io/public' }
maven { url "https://clojars.org/repo/" }
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
transitive = true;
}
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:design:24.1.1'

compile 'com.mcxiaoke.volley:library:1.0.18'

compile 'com.google.android.gms:play-services-gcm:9.2.1'
compile 'com.google.android.gms:play-services-location:9.2.1'
compile 'com.google.android.gms:play-services-maps:9.2.1'

compile 'com.squareup:android-times-square:1.6.5@aar'

compile 'frankiesardo:icepick:3.2.0'
provided 'frankiesardo:icepick-processor:3.2.0'

compile 'com.squareup.picasso:picasso:2.5.2'

compile 'com.jakewharton:butterknife:7.0.1'

compile 'net.danlew:android.joda:2.9.3'

compile 'com.github.JakeWharton:ViewPagerIndicator:2.4.1@aar'
}


Android manifest

<application
android:name=".MyApplication"
android:allowBackup="true"
android:fullBackupContent="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="false"
android:theme="@style/Base.AppTheme">

<activity
android:name=".activity.MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/Base.AppTheme.Main"
android:windowSoftInputMode="stateHidden"/>

</application>


themes.xml

<resources>
<!-- Base -->
<style name="Base.AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primaryColor</item>
<item name="colorPrimaryDark">@color/primaryColor_dark</item>
<item name="colorAccent">@color/accentColor</item>
<item name="android:windowBackground">@color/background_activity</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:fontFamily">sans-serif</item>
</style>

<!-- Main Activity -->
<style name="Base.AppTheme.Main">
<item name="android:textColorSecondary">@android:color/white</item>
</style>
</resources>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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"
tools:openDrawer="start">

<include
layout="@layout/app_bar_drawer_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/background_navigationDrawer"
android:fitsSystemWindows="true"
app:itemBackground="@drawable/drawer_item"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:menu="@menu/activity_drawer_drawer">

<TextView
android:id="@+id/nav_version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:padding="16dp"
android:textColor="@android:color/white"
tools:text="Version 0.0.0"/>
</android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>


app_bar_drawer_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context=".activity.MainActivity"
tools:showIn="@layout/activity_main">


<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

<TextView
android:id="@+id/toolbar_title"
style="@style/TextView.Toolbar" />

<ImageView
android:id="@+id/toolbar_logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:contentDescription="@string/content_desc_logo"
android:padding="12dp"
android:scaleType="fitCenter"
android:src="@drawable/logo_toolbar"
android:visibility="gone" />

</android.support.v7.widget.Toolbar>

<android.support.design.widget.TabLayout
android:id="@+id/toolbat_tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:visibility="gone" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<include layout="@layout/component_loading_spinner" />

</android.support.design.widget.CoordinatorLayout>


content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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/main_frame_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activity.MainActivity"
tools:showIn="@layout/app_bar_drawer_main" />


fragment_browser.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:orientation="vertical">

...

</LinearLayout>
</ScrollView/>


What am I doing wrong?? What am I have to do on API 19 to have
fitSystemWindows
working so
Toolbar
is placed below status bar?

Answer

Finally I solved it. The problem was that I was overriding the themes in a styles-v19.xml and I had this line added:

<item name="android:windowTranslucentStatus">true</item>

Once removed this, the Toolbar works perfect. I'll update the styles snippet to include this just in case someone still found this post useful.