Search code examples
androidandroid-layoutimageviewandroid-toolbar

Setting CollapsingToolbarLayout image programmatically


I am using a ScrollingActivity which comes with a CollapsingToolbarLayout. I want to add an ImageView to this layout, and be able to toggle the image programmatically, but it doesn't seem to be doing anything.

XML of my layout:

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <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" />

        <ImageView
            android:id="@+id/header_logo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="center"
            android:layout_gravity="center"
            app:layout_collapseMode="parallax"
            />

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

When I try to do something like the following, on an event:

ImageView im = (ImageView)findViewById(R.id.toolbar_layout);
im.setBitmap(image_here);

But when I trigger this event, nothing happens. I know that my image is being parsed correctly because I'm not getting any exceptions from it, in fact; I'm not getting any exceptions at all throughout this process. Am I doing something wrong here? Is there a way to refresh this Toolbar to update the image?


Solution

  • Maybe because you are calling the layout id of Toolbar to ImageView

    Try Changing

    ImageView im = (ImageView)findViewById(R.id.toolbar_layout);
    im.setBitmap(image_here);
    

    to

    ImageView im = (ImageView)findViewById(R.id.header_logo);
    im.setBitmap(image_here);