Search code examples
androidandroid-fragmentsandroid-actionbarandroid-linearlayoutandroid-scrollview

Strange empty white space above Fragment's toolbar. How to fix it?


I'm writing an Android app, whoose is based on Fragments, which are inserted inside root emty activity. For every Fragment I configure separate Toolbar. When I open the first screen-fragment it works fine. The xml, toolbar code and screenshots of this screen

<?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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        <include
                android:id="@+id/toolbar_main"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                layout="@layout/layout_toolbar_main"/>

        <android.support.v7.widget.RecyclerView
                android:id="@+id/listOfClientsWithActions"
                android:layout_height="match_parent"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:clipToPadding="false" android:paddingTop="8dp"/>
    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabAddContact"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:src="@drawable/ic_baseline_add_white_24"
            android:layout_margin="16dp" app:fabSize="normal"/>
</android.support.design.widget.CoordinatorLayout>

<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:fitsSystemWindows="true"
        app:itemIconTint="@color/drawer_item"
        app:menu="@menu/drawer_menu"
        app:itemTextAppearance="@style/NavDrawerItem"
        app:headerLayout="@layout/nav_header"/>
</android.support.v4.widget.DrawerLayout>

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    initToolbar(view)
}

private fun initToolbar(view: View) {
    val toolbar = view.findViewById<Toolbar>(R.id.toolbar_main)
    (activity as AppCompatActivity).setSupportActionBar(toolbar)
    val actionbar: ActionBar? = (activity as AppCompatActivity).supportActionBar
    actionbar?.apply {
        setHasOptionsMenu(true)
        setDisplayHomeAsUpEnabled(true)
        setHomeAsUpIndicator(R.drawable.ic_baseline_menu_white_24)
        title = getString(R.string.app_name)
    }
}

Then when I open the next Fragment by pushing FAB. The screenshot and code for this screen:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@android:color/white"
    android:paddingBottom="8dp"
    android:orientation="vertical">

<include
        android:id="@+id/toolbar_add_contact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/layout_toolbar_add_contact"/>

<ScrollView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">

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

        <EditText
                android:id="@+id/editTextName"
                android:inputType="textPersonName"
                android:hint="@string/name"
                tools:text="Федоров Алексей"
                android:theme="@style/CustomEditTextTheme"
                style="@style/AddContactEditTextBasicStyle"/>

        <EditText
                android:id="@+id/editTextPhone"
                android:digits="1234567890+-() "
                android:inputType="number"
                android:hint="@string/phone_number"
                android:theme="@style/CustomEditTextTheme"
                style="@style/AddContactEditTextBasicStyle"/>

        <EditText
                android:id="@+id/editTextEmail"
                android:inputType="textEmailAddress"
                android:hint="@string/email"
                tools:text="aleksey.fedorovjob@mail.ru"
                android:theme="@style/CustomEditTextTheme"
                style="@style/AddContactEditTextBasicStyle"/>

        <com.winwin_tech.fixapp.fixapp.util.CustomEditText
                android:id="@+id/editTextClientType"
                android:theme="@style/CustomEditTextTheme"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:hintText="@string/client_type"
                app:starterText="@string/seller"
                style="@style/AddContactEditTextBasicStyle"/>

        <com.winwin_tech.fixapp.fixapp.util.CustomEditText
                android:id="@+id/editTextObjectType"
                android:theme="@style/CustomEditTextTheme"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:hintText="@string/object_type"
                app:starterText="@string/apartments"
                style="@style/AddContactEditTextBasicStyle"/>

        <EditText
                android:id="@+id/editTextAddress"
                android:inputType="textPostalAddress"
                android:hint="@string/address"
                tools:text="пр. Новгородский, д. 158"
                android:theme="@style/CustomEditTextTheme"
                style="@style/AddContactEditTextBasicStyle"/>

        <faranjit.currency.edittext.CurrencyEditText
                android:id="@+id/editTextPrice"
                android:inputType="numberDecimal"
                android:hint="@string/price"
                app:showSymbol="false"
                app:groupDivider="."
                app:monetaryDivider="."
                tools:text="3.200.000"
                android:theme="@style/CustomEditTextTheme"
                style="@style/AddContactEditTextBasicStyle"/>

        <EditText
                android:id="@+id/editTextLocation"
                android:inputType="textPostalAddress"
                android:hint="@string/location"
                tools:text="Московский район"
                android:theme="@style/CustomEditTextTheme"
                style="@style/AddContactEditTextBasicStyle"/>

        <com.winwin_tech.fixapp.fixapp.util.CustomEditText
                android:id="@+id/editTextWcCount"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/CustomEditTextTheme"
                android:paddingBottom="0dp"
                app:hintText="@string/wc_count"
                app:starterText="0"
                style="@style/AddContactEditTextBasicStyle"/>

        <com.winwin_tech.fixapp.fixapp.util.CustomEditText
                android:id="@+id/editTextBrCount"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/CustomEditTextTheme"
                app:hintText="@string/br_count"
                app:starterText="0"
                style="@style/AddContactEditTextBasicStyle"/>

        <com.winwin_tech.fixapp.fixapp.util.CustomEditText
                android:id="@+id/editTextAction"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/CustomEditTextTheme"
                app:hintText="@string/action"
                app:starterText="@string/call"
                style="@style/AddContactEditTextBasicStyle"/>

        <com.winwin_tech.fixapp.fixapp.util.CustomEditText
                android:id="@+id/editTextDate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/CustomEditTextTheme"
                app:hintText="@string/date"
                app:starterText="01.01.1970"
                style="@style/AddContactEditTextBasicStyle"/>

        <com.winwin_tech.fixapp.fixapp.util.CustomEditText
                android:id="@+id/editTextTime"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/CustomEditTextTheme"
                app:hintText="@string/time"
                app:starterText="12:00"
                style="@style/AddContactEditTextBasicStyle"/>
    </LinearLayout>
</ScrollView>
</LinearLayout>

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    initToolbar(view)
}

private fun initToolbar(view: View) {
    val toolbar = view.findViewById<Toolbar>(R.id.toolbar_add_contact)
    (activity as AppCompatActivity).setSupportActionBar(toolbar)
    val actionbar: ActionBar? = (activity as AppCompatActivity).supportActionBar
    actionbar?.apply {
        setHasOptionsMenu(true)
        setDisplayHomeAsUpEnabled(true)
    }
}

As you can see om the second screen there is strange space above toolbar. So that's my issue and I don't know how to fix it. Please show how to resolve it.

P.S Th layout of add_contact_toolbar

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    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:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:elevation="4dp"
    android:background="?attr/colorPrimary"
    app:title="@string/add_contact"
    app:titleTextAppearance="@style/ToolbarTitle"
    app:titleTextColor="@android:color/white" app:navigationIcon="@drawable/ic_baseline_close_white_24"
    tools:menu="@menu/menu_add_contact_screen"/>

Solution

  • It might be related to the use of "android:fitsSystemWindows="true", since your layout takes the statusbar height into account. Try fiddling around with that value.