Change image of ImageView included in CollapsingToolbarLayout

2

I want the user of my application to be able to change the image of ImageView included in the CollapsingToolbarLayout , either by obtaining it through the camera or from a file in the internal memory. For now I just try to change it from a fragment in onCreateView by SetImageResource(R.drawable.koala) and I get an error loading the application and start that fragment. In the Activity inflo the layout that contains appBar and others. In the fragment inflo a fragment that I include it in mainactivity.xml .

so in OnCreateView()

final ImageView imagen_Appbar = (ImageView)v.findViewById(R.id.image_appbar);
imagen_Appbar.setImageResource(R.drawable.koala);

my layout toolbar

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title"
            app:expandedTitleMarginEnd="46dp"
            app:expandedTitleMarginStart="44dp"
            app:expandedTitleMarginBottom="12dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <ImageView
                android:layout_width="match_parent"
                android:scaleType="centerCrop"
                android:layout_height="@dimen/app_bar_height"
                android:id="@+id/image_appbar"
                android:background="@color/deep_green_700_alpha_50"
                android:minHeight="190dp"
                app:srcCompat="@drawable/cielo"
                android:contentDescription="@string/app_name"
                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:layout_scrollFlags="scroll|enterAlways"
                />
        </android.support.design.widget.CollapsingToolbarLayout>

I include this layout through another root with CoordinatorLayout

>
<!-- incluir Toolbar -->
<include layout="@layout/toolbar"/>

<android.support.v4.widget.NestedScrollView

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        >
    <!-- Layout real de la actividad -->
    <include layout="@layout/content_main" />

    </LinearLayout>

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


<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/fab_margin_bottom" android:layout_marginEnd="@dimen/fab_margin_right"
    app:layout_anchor="@+id/appbar"
    app:layout_anchorGravity="bottom|right|end"
    app:borderWidth="0dp"
    app:elevation="6dp"
    app:pressedTranslationZ="12dp"
    android:src="@android:drawable/ic_dialog_email"
    android:clickable="true"
    app:fabSize="mini"/>

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

I have used Glide and Picasso and it does not work either

    
asked by JOSE MARTINEZ 08.03.2016 в 11:29
source

1 answer

1

It is rare that you want to change the image in this type of elements within CoordinatorLayout , I see that you try to load it from the OnCreateView() , try waiting a bit with a handler :

private static final int DELAY = 500;

        new Handler().postDelayed(new Runnable() {
            public void run() {
                final ImageView imagen_Appbar = (ImageView)v.findViewById(R.id.image_appbar);
                imagen_Appbar.setImageResource(R.drawable.koala);
            }
        }, DELAY);

You can also do the same with Picasso or Glide .

    
answered by 08.03.2016 в 17:07