opacapp / multiline-collapsingtoolbar

A modified CollapsingToolbarLayout that can deal with multiline titles
Other
782 stars 114 forks source link

You need to use a Theme.AppCompat theme (or descendant) with the design library #14

Closed samy-baili closed 8 years ago

samy-baili commented 8 years ago

Samsung S3, API 19 (4.4.4)

Grade compile 'net.opacapp:multiline-collapsingtoolbar:1.2.0'

my style :

<style name="PlanningTheme" parent="Theme.AppCompat">
   <item name="windowActionBarOverlay">true</item>
   <item name="windowActionBar">false</item>
   <item name="windowNoTitle">true</item>
  <item name="colorPrimary">@color/main_blue</item>
  <item name="android:textColor">@color/white</item>
  <item name="colorControlNormal">@color/white</item>
</style>

Manifest:

<activity
     android:name=".phone.PlanningActivity" 
     android:screenOrientation="portrait"
     android:theme="@style/PlanningTheme"
     android:windowSoftInputMode="adjustPan"/>

Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library. at net.opacapp.multilinecollapsingtoolbar.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:36) at net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout.<init>(CollapsingToolbarLayout.java:142) at net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout.<init>(CollapsingToolbarLayout.java:136) at java.lang.reflect.Constructor.constructNative(Native Method)  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)  at android.view.LayoutInflater.createView(LayoutInflater.java:600)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)  at android.view.LayoutInflater.inflate(LayoutInflater.java:498)  at android.view.LayoutInflater.inflate(LayoutInflater.java:398)  at android.view.LayoutInflater.inflate(LayoutInflater.java:354)  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)  at com.swapcard.apps.android.phone.ScheduleDetailActivity.onCreate(ScheduleDetailActivity.java:108)  at android.app.Activity.performCreate(Activity.java:5582)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)  at android.app.ActivityThread.access$900(ActivityThread.java:171)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:146)  at android.app.ActivityThread.main(ActivityThread.java:5679)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)

raphaelm commented 8 years ago

Hi! Thank you for the feedback, we appreciate it.

Could you first confirm that this is a bug that is specific to our library and not a problem with the default CollapsingToolbarLayout from the Android support library? On first sight, I don't believe this is a problem with our library but the Android support library.

samy-baili commented 8 years ago

Yeah, because I am using the default CollapsingToolbarLayout somewhere else in my appllication... No issue detected

johan12345 commented 8 years ago

Are you also using the current version of the Support Library?

    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'

Did you try to replace the net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout with a android.support.design.widget.CollapsingToolbarLayout to see if the problem persists?

samy-baili commented 8 years ago

Yes, all my support library are updated and when I replace "net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout" by "android.support.design.widget.CollapsingToolbarLayout", I have no Issue

johan12345 commented 8 years ago

I can't reproduce the bug, even using older Android versions and adjusting the styles from the demo app to the ones you provided. Does running the demo app work on the same device? If so, could you give us some more code (e.g. the part of your layout XML file where you use net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout etc.)?

samy-baili commented 8 years ago
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:alpha="0"
            android:paddingBottom="80dp"/>

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

    <io.codetail.widget.RevealFrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <View
            android:id="@+id/view_color"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"/>

    </io.codetail.widget.RevealFrameLayout>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:background="@android:color/transparent">

        <net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:expandedTitleMarginEnd="@dimen/detail_padding"
            app:expandedTitleMarginStart="@dimen/detail_padding"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:alpha="0">

            <View
                android:id="@+id/empty_color_view"
                android:layout_width="match_parent"
                android:layout_height="@dimen/header_schedule_height"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"/>

        </net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout>

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

    <com.swapcard.apps.android.views.likebutton.FloatingLikeButton
        android:id="@+id/floating_planning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_margin="@dimen/floating_button_padding"
        android:alpha="0"/>

    <ProgressBar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/progress_bar_schedule"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone"/>

</android.support.design.widget.CoordinatorLayout>
johan12345 commented 8 years ago

Hmm, I still cannot reproduce it.

Is there a way you could set a breakpoint on net.opacapp.multilinecollapsingtoolbar.ThemeUtils:30 and evaluate the expression context.getResources().getResourceEntryName(context.getThemeResId()) there? It seems the theme it obtains there is not an AppCompat theme.

svenjacobs commented 8 years ago

I have the same issue. A layout with the default android.support.design.widget.CollapsingToolbarLayout works. When I just replace this by net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout and add the app:maxLines attribute I get this error.

raphaelm commented 8 years ago

@svenjacobs Just to clarify: Does it work with our layout without the app:maxLines attribute?

svenjacobs commented 8 years ago

@raphaelm No, it doesn't work without app:maxLines. Just replacing CollapsingToolbarLayout in the layout suffices to provoke this error.

johan12345 commented 8 years ago

@svenjacobs Could you try if it works with an older version of the library?

compile 'net.opacapp:multiline-collapsingtoolbar:1.1.0'

If that is the case, I think I know where the problem is.

svenjacobs commented 8 years ago

@johan12345 Same error with version 1.1.0

johan12345 commented 8 years ago

@svenjacobs Hm, strange.

As I wrote before:

Is there a way you could set a breakpoint on net.opacapp.multilinecollapsingtoolbar.ThemeUtils:30 and evaluate the expression context.getResources().getResourceEntryName(context.getThemeResId()) there? It seems the theme it obtains there is not an AppCompat theme.

svenjacobs commented 8 years ago

@johan12345 See attached screenshot

screen shot 2016-07-14 at 15 47 47

svenjacobs commented 8 years ago

Update: Please forget my last comment. My evaluation was wrong. Here's the correct screenshot.

@johan12345 And here's the same evaluation with the original CollapsingToolbarLayout from Android Design Support 24.0.0.

The value of TypedArray a is different but I'm not sure why...

screen shot 2016-07-15 at 08 21 35

johan12345 commented 8 years ago

I still can't reproduce the issue and don't see where it comes from looking at the screenshots.

In https://github.com/opacapp/multiline-collapsingtoolbar/commit/97e1bbe5d6c3d2645003df7fed631ff6d9fd4ace, I commented out the call to checkAppCompatTheme. Maybe it just works without it? It is not absolutely necessary for us to include the check in this library, as it should already be done by the other Design Library components (e.g. AppBarLayout).

svenjacobs commented 8 years ago

Without looking at the source code, could it be that your CollapsingToolbarLayout does not inherit from the correct base theme of the support library?

johan12345 commented 8 years ago

Well, a View can't inherit from a Theme, the theme is set by the LayoutInflater depending on the Activity theme (or additional themes specified in the XML layout). All the code (except for the changes that were marked) is basically copied from the support library.

svenjacobs commented 8 years ago

I'm aware of that ;) I was talking about the style which is applied to the view.

johan12345 commented 8 years ago

Ah, okay. If that's the issue, it should work with version 1.0.1 (and Support Library 23.2.1), which did not need any custom styles (it didn't even have a src/main/res folder).

johan12345 commented 8 years ago

And one more thing I haven't asked yet: Does this issue also appear with our demo app or only when using the library in your own application? And does it only appear on a specific device/Android version?

svenjacobs commented 8 years ago

@johan12345 Here are a few answers:

  1. The demo application works on the same device (Nexus 5, Android 6.0.1)
  2. My application with your library version 1.2.1 crashes on a Nexus 5 and Nexus 5X (both Android 6.0.1)
  3. My application does not crash with version 1.0.1 of your library and Android Support 23.2.1 (Same devices used for testing)
johan12345 commented 8 years ago

@svenjacobs Oh, okay, so it is definitely related to some of the styles/themes you have in your app. Maybe you could share some of those (and the layout attributes of the CollapsingToolbarLayout) so I can try to reproduce it in the demo app? Or try to copy them over to the demo app to find out which of them causes the crash?

svenjacobs commented 8 years ago

This is a snippet from our styles.xml. I removed all attributed which are hopefully not related to this issue:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary_500</item>
    <item name="colorPrimaryDark">@color/primary_700</item>
    <item name="colorAccent">@color/primary_700</item>
    <item name="colorControlHighlight">@color/colorControlHighlight</item>
</style>

<style name="AppTheme.Launch">
    <item name="android:windowBackground">@drawable/splash_screen</item>
</style>

The AppTheme.Launch theme is applied to <application> in AndroidManifest.xml. We do not set any Activity theme in the manifest. We programmatically change the theme to AppTheme in onCreate() of an Activity like described here.

This is the 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">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/recipes_details_backdrop_height"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:visibility="gone"
        tools:visibility="visible">

        <net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:collapsedTitleTextAppearance="@style/CollapingToolbar.CollapsedTitleTextAppearance"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="16dp"
            app:expandedTitleMarginStart="16dp"
            app:expandedTitleTextAppearance="@style/CollapingToolbar.ExpandedTitleTextAppearance"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@string/empty_content_description"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar
                android:id="@id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

        </net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout>

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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/contentView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

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

</android.support.design.widget.CoordinatorLayout>
johan12345 commented 8 years ago

@svenjacobs In https://github.com/opacapp/multiline-collapsingtoolbar/commit/988fce8092fbe767fc4888ad36969663bd646f58 (on the branch svenjacobs-style-tests), I copied most of your styles to the demo app, and it is still running fine for me (Nexus 5X/6.0.1 and Moto G2/6.0)... :/

svenjacobs commented 8 years ago

@johan12345 Are you sure that in version 1.2.1 of your library you properly copied all files from Android Support 24.0.0? Because what's odd is that your library in version 1.0.1 works with my project...

johan12345 commented 8 years ago

@svenjacobs That's indeed odd, but when I can't reproduce the issue, its difficult to find out what change after version 1.0.1 caused it.

lageev commented 8 years ago

I use a custom view in fragment containing this Activity。THIS IS led to the emergence of error.

Sorry, I used Google Translation

johan12345 commented 8 years ago

@geekvxyz Is there any way you can give me a minimal example that reproduces the issue, e.g. by modifying the demo app?

castrojr913 commented 8 years ago

@johan12345 The issue came me up with the version 1.2.2 too. I could fix the bug, commenting the exception IllegalArgumentException inside the method checkAppCompatTheme(Context context) of the class ThemeUtils. It tested with a Nexus 5 and com.android.support:design:24.1.1. What is that validation for?

johan12345 commented 8 years ago

@castrojr913 Well, that validation is directly copied from the Support Library, so I don't understand why it doesn't work here.

Additionally, this problem never showed up in crash reports from our app or when testing the demo app. This is why I am still asking for an example project (e.g. based on the demo app) where the problem occurs so that I can reproduce it.

But on the other hand, I guess it would not matter very much if we simply removed that validation, as it will still be performed by all the Support Library components. @raphaelm, what do you think?

cameronlund4 commented 8 years ago

Having the same issue, with the same theme. Was a fix/workaround decided for this issue?

johan12345 commented 8 years ago

@Cameronlund4 As mentioned above, the fix seems to be removing the validation. But before we include this change in a new release of the library, I would like to be able to reproduce the issue. Are you able to provide a minimal working example that shows the bug?

castrojr913 commented 8 years ago

@Cameronlund4 I created a fork, fixing this issue. Please, declare on build.gradle:

repositories {
    maven { url "https://jitpack.io" }
}
dependencies {
   compile 'com.github.castrojr913:multiline-collapsingtoolbar:v1.2.2'
}
johan12345 commented 8 years ago

@castrojr913 As I said, we would also incorporate this change into our version on JCenter, but before that, I would like to to reproduce the crash and find out what causes it. And as nobody provided an example project, I was not able to do so. 😕

johan12345 commented 8 years ago

It seems that @jfri found a more elegant solution to this problem without telling us :wink:. I just merged it and it will be included in the next release.

castrojr913 commented 8 years ago

@johan12345 oh, good news :)

raphaelm commented 8 years ago

Should be in the 1.3.0 release :)

siralam commented 7 years ago

Hey, I was just randomly googling about the error "You need to use a Theme.AppCompat theme (or descendant) with the design library", and come to this page. And especially I see some of you cannot reproduce this problem, I wonder if this is the same as what happened to me.

In fact, I found that "theme" is an "attritube" of context; and LayoutInflator needs to reference a context to inflate Layouts. If you are trying to inflate a layout using a context that does not have the correct theme (e.g. you used getApplicationContext() to obtain a Layout Inflator) then this Layout Inflator may not have the correct context which you have already defined your AppCompact theme.

For example I used setTheme(R.style.AppThemeWithBackground); in a BaseActivity which extends AppCompactActivity, and AppThemeWithBackground also inherits an AppCompact theme. Later if I call this line this BaseActivity LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); I will get the error. But if I change it to LayoutInflater inflater = LayoutInflater.from(this); Then the error will go away.

Therefore I suspect this is the real cause, so may be restoring the default behavior of design library is better.