Olav Gulbrandsen Blaaflat Olav Gulbrandsen Blaaflat - 26 days ago 7
Android Question

AppBarLayout/Toolbar not showing (height = 0)

I am developing an app to display details about movie/tv shows. When opening the movie from search results it opens correctly. Then I add the movie to my collection, turns to MainActivity where all my movies are listed. Selecting the same movie, but now the entire AppBarLayout is hidden! I don't get any exceptions of any kind, in debug I see that it is visible, but has height of 0. Why is this happening?

I have a method that collapses the CollapsingToolbarLayout if no banner image is found, but this is not the case here. It should anyhow have a toolbar with title when collapsed, and expand on scroll.

Correctly displayed AppBarLayout not visible



Layout of DetailActivity:

<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:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.NoActionBar"
tools:context=".activites.DetailActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="240dp"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_behavior="@string/appbar_spring_behavior">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="60dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<ImageView
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@null"
android:fitsSystemWindows="true"
android:focusableInTouchMode="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/AppTheme.PopupOverlay" />

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

<android.support.design.widget.NestedFixFlingScrollView
[...]


Some relevant setup methods in DetailActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
movieChanges = InterfaceHolder.movieChanges;
findNodes();
setupToolbar();
setupCurrentMovie();
setupImageGallery();
setupFAB();

displayDetails();
fetchTmdbDetails();
}

private void setupToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
getWindow().setStatusBarColor(Color.TRANSPARENT);
setTitleWhenCollapsed();
}

private void setupCurrentMovie() {
movie = new Gson().fromJson(getIntent().getStringExtra(EXTRA_MOVIE), Movie.class);
if (movie == null) {
Toast.makeText(this, R.string.movie_not_found, Toast.LENGTH_SHORT).show();
finish();
}
}
[...]


Starting DetailActivity from search result:

holder.listItemContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (resultModel.searchResult.isTvShow() || resultModel.searchResult.isMovie()) {
final Movie movie = new Movie();
movie.setTitle(resultModel.displayText);
movie.setYear(resultModel.year);
movie.setFormat(MovieFormat.BLU_RAY);
movie.setType(MediaType.from(resultModel.mediaType));
movie.setPosterPath(resultModel.posterUrl);
movie.setTmdbId(resultModel.searchResult.getId());
final Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra(DetailActivity.EXTRA_MOVIE, new Gson().toJson(movie));
context.startActivity(intent);
}
}
});


Starting from MainActivity:

final Movie movie = movieList().get(position);
final Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
intent.putExtra(DetailActivity.EXTRA_MOVIE, new Gson().toJson(movie));
startActivity(intent);


styles.xml:

<resources>

<style name="AppTheme" parent="Theme.AppCompat">
<!-- 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="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/background_splash</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />




styles.xml-21:

<resources>

<style name="AppTheme21" parent="AppTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>




Both the SearchActivitiy and DetailActivity uses AppTheme.NoActionBar. MainActivity uses AppTheme. I now noticed that when opening a movie from SearchActivity it is showing correctly, but if I press back (to search) and then opens the same movie again, it does not show correctly.

I had some suspicion that it whould be this line in the SearchActivity that caused the trouble, but it was not:

getSupportActionBar().hide();


It seems that it is only after using the SearchActivity that the DetailActivity does not show correctly. All of my other example data in MainActivity opens correctly in DetailActivity, until i have used the SearchActivity. Even some of the movies that I have not searched for.




Sorry if this question grew to be to long/detailed, but I often see that people are asking for more details, so I chose to include as much relevant info as I thought was necessary.

Please let me know if you need any more info?

Answer

I figured it out! I am using Ion for image loading, and had a callback method "collapseAppBarOnError":

private FutureCallback<ImageView> collapseAppBarOnError() {
    return new FutureCallback<ImageView>() {
        @Override
        public void onCompleted(Exception e, ImageView result) {
            stopLoading();
            if (e != null && !(e instanceof CancellationException)) {//TODO: Denne vil ikke funke om errorDrawable ligger i cache?
                collapse();
            } else if (result != null && result.getDrawable() != null && !result.getDrawable().equals(getDrawable(R.drawable.ic_movie_white_48dp))) {
                expand();
            }
        }
    };
}

It was the "else if" in the callback method that caused the trouble. Sorry, for this was impossible for you guys to see when I had not postet all my code.

It turns out that it actually had nothing to do with the SearchActivity, the error was also happening if I opened a movie I already had in my collection two times or more, even if not using the search! It works correctly the first time, but not the following times.