Search code examples
androidandroid-toolbarandroid-coordinatorlayoutandroid-collapsingtoolbarlayout

How to pin icon from image to toolbar after scrolling same like WhatsApp group edit icon


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <android.support.design.widget.CoordinatorLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:fitsSystemWindows="true">

       <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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

           <android.support.design.widget.CollapsingToolbarLayout
               android:id="@+id/collapsing_toolbar"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:accessibilityLiveRegion="assertive"
               android:fitsSystemWindows="true"
               app:contentScrim="?attr/colorPrimary"
               app:layout_scrollFlags="scroll|exitUntilCollapsed"
               app:titleEnabled="true">

               <ImageView
                   android:id="@+id/image"
                   android:layout_width="match_parent"
                   android:layout_height="350dp"
                   android:fitsSystemWindows="true"
                   android:scaleType="centerCrop"
                   android:src="@drawable/ic_launcher"
                   app:layout_collapseMode="parallax" />


               <ImageView
                   android:id="@+id/imgicon1"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_gravity="bottom|right"
                   android:layout_marginBottom="15dp"
                   android:fitsSystemWindows="true"
                   android:src="@drawable/fb"
                   app:layout_collapseMode="pin"
                   app:layout_collapseParallaxMultiplier="0.7" />

               <ImageView
                   android:id="@+id/imgicon2"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_gravity="bottom|right"
                   android:layout_marginBottom="15dp"
                   android:layout_marginRight="45dp"
                   android:fitsSystemWindows="true"
                   android:src="@drawable/insta"
                   app:layout_collapseMode="pin"
                   app:layout_collapseParallaxMultiplier="0.7" />

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

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

       <android.support.v7.widget.RecyclerView
           android:id="@+id/recyclerView"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           app:layout_behavior="@string/appbar_scrolling_view_behavior"></android.support.v7.widget.RecyclerView>

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

When I scroll the 2 icons are hide behind the image instead of setting inside the toolbar. Any idea how can i do this, I look into many examples but now working for me. I have implemented onOffsetChanged method of Appbarlayout and try to play with animation but didn't worked for me.


Solution

  • There is an accepted answer in Android: dynamically change ActionBar icon? refer this. You can get image from your image view and reduce its size to your requirement and can change icon using homeItem.setIcon(your image);