0

I'm trying to have a single Toolbar (v7) that shows under the statusbar of Android in version 21+ while using the v4 compability library. I read a lot of posts and tried a lot of different configurations, but can't seem to get it working without setting the padding manually (which is something I want to prevent because the size of the SystemWindow might change on later Android versions) or using setSupportActionBar().

I started with the default Scrolling example in Android Studio and tried to replace the CollapsingToolbarLayout by a regular Toolbar.

My layout:

<?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="com.example.wbusey0.toolbartest.ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.AppBarOverlay" />
    </android.support.design.widget.AppBarLayout>

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

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

styles.xml:

<resources>

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

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

    <style name="AppTheme.AppBarOverlay" />

    <style name="AppTheme.PopupOverlay"  />

</resources>

v21 styles xml:

    <style name="AppTheme.NoActionBar" parent="@style/Theme.AppCompat.Light">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

My manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.wbusey0.toolbartest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".ScrollingActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I cannot use setSupportActionBar() because I want to use nested fragments later and thus like to extend FragmentActivity. However using setSupportActionBar() solves my problem. Any ideas on how to fix the toolbar height?

Toolbar height too small

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
user2220771
  • 419
  • 4
  • 6

2 Answers2

2

You should use AppCompatActivity, not FragmentActivity.

public ScrollingActivity extends AppCompatActivity {

NOT

public ScrollingActivity extends FragmentActivity {
Divers
  • 9,531
  • 7
  • 45
  • 88
  • 1
    Could you explain this answer? Since the OP's question is not clear(It's like two questions in one thread) I'd think that your answer is not in the right place. It could be a comment... – ʍѳђઽ૯ท Jan 07 '17 at 12:07
  • He is using `FragmentActivity` instead of `AppCompatActivity` in conjunction with `CoordinatorLayout`. And no, it's shouldn't be a comment, I've run his code, reproduced problem and fixed it as I described in my answer. If you didn't understand question, it doesn't mean that everybody did the same. – Divers Jan 07 '17 at 14:44
  • Ohh, You're right. Didn't think/see about that 'FragmentActivity'. At this point you are right! I just upvotes your answer. – ʍѳђઽ૯ท Jan 07 '17 at 17:41
1

To fix this problem, You may want to change android:layout_height of AppBarLayout to wrap_content.

So:

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

As well as the documentation says the same thing :

https://developer.android.com/reference/android/support/design/widget/AppBarLayout.html

UPDATE:

Honestly, I did not think about FragmentActivity. Anyways, Here is your answer:

https://stackoverflow.com/a/30371919/4409113

With the latest version of the support library you should make your Activity extend AppCompatActivity as ActionBarActivity has been deprecated.

But in your situation, This can help:

((AppCompatActivity) getActivity()).setSupportActionBar(toolbar)

And if this didn't work, Then ;

As @Divers said, You'll need to extend the 'Activity' to 'AppCompatActivity'..And just initializing the 'Toolbar'.

Community
  • 1
  • 1
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • How is your updated answer reflects author's problem? Author uses `FragmentActivity`, not `ActionBarActivity`. Also if he will try to cast `FragmentActivity` to `AppCompatActivity`, it will just throw exception. – Divers Jan 08 '17 at 09:10
  • @Divers Ohh yeah, Updated the answer...Thank you for your attention. – ʍѳђઽ૯ท Jan 08 '17 at 15:06