Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change toolbar icons color on collapsing

I'm using CoordinatorLayout in my project . It works fine but there is a problem I need to fix .I'm using white actionbar icons and When the CoordinatorLayout is expanded and the background color is white, I can't see any of those icons .

I saw this in an application . when the CoordinatorLayout is expanded , the icons are dark and when scrolling down, they begin to change into white .

http://i.stack.imgur.com/rCCQb.png

as you can see, when it's expanded the icons are dark and when it closes , the icons becomes white .

it begins to change when scroll down until it completely convert from dark icons to white icons

sorry for not attaching the code , here is my codes :

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="240dp"
    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:fitsSystemWindows="true"
        app:contentScrim="#FF4548"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <RelativeLayout
            android:id="@+id/slidelayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="?attr/actionBarSize"
            android:background="#fff">

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <com.viewpagerindicator.CirclePageIndicator
                android:id="@+id/indicator"
                android:layout_width="fill_parent"
                android:layout_height="10dp"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="7dp" />
        </RelativeLayout>

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:paddingLeft="3dp"
            android:layout_gravity="top"
            android:paddingRight="3dp"
            app:layout_collapseMode="pin">

            <ImageView
                android:id="@+id/imgsearch"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="2dp"
                android:paddingBottom="5dp"
                android:src="@drawable/ic_action_search"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/imgshare"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@+id/imgsearch"
                android:paddingBottom="5dp"
                android:src="@drawable/ic_action_share"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/imglike"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@+id/imgshare"
                android:paddingBottom="5dp"
                android:src="@drawable/ic_action_favorite"
                android:visibility="visible" />

            <RelativeLayout
                android:id="@+id/sabadkharidslayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/imglike"
                android:visibility="gone">

                <ImageView
                    android:id="@+id/img_sabad"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="10dp"
                    android:src="@drawable/sabad" />

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="15dp"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/circle">

                    <TextView
                        android:id="@+id/text_numkharid"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:gravity="right"
                        android:includeFontPadding="false"
                        android:singleLine="true"
                        android:textColor="#fff"
                        android:textSize="12sp" />
                </RelativeLayout>
            </RelativeLayout>

            <ImageView
                android:id="@+id/back"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="5dp"
                android:paddingBottom="5dp"
                android:rotationY="180"
                android:src="@drawable/ic_action_back"
                android:visibility="visible" />

            <TextView
                android:id="@+id/textVieww1"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:layout_marginRight="5dp"
                android:layout_toLeftOf="@+id/back"
                android:gravity="right|center_vertical"
                android:shadowColor="#eee"
                android:shadowDx="2"
                android:shadowDy="2"
                android:shadowRadius="4"
                android:singleLine="true"
                android:textColor="#fff"
                android:textSize="17sp" />

            <AutoCompleteTextView
                android:id="@+id/editText1"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:layout_toLeftOf="@+id/textVieww1"
                android:layout_toRightOf="@+id/sabadkharidslayout"
                android:background="#fff"
                android:completionThreshold="1"
                android:dropDownWidth="match_parent"
                android:ems="10"
                android:singleLine="true"
                android:gravity="right"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:textSize="12sp"
                android:visibility="gone">

                <requestFocus />
            </AutoCompleteTextView>
        </RelativeLayout>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

How can I do so ?

like image 681
donald draper Avatar asked Sep 27 '15 15:09

donald draper


1 Answers

You can do this similar to as shown in this Stack Overflow post. Thanks to tachyonflux

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
AppBarLayout appBar = (AppBarLayout)  findViewById(R.id.appbar);

appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if ((collapsingToolbar.getHeight() + verticalOffset) < (2 * ViewCompat.getMinimumHeight(collapsingToolbar))) {
                toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
            } else {
                toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.black), PorterDuff.Mode.SRC_ATOP);
            }
        }
    });

Similarly you can apply it for any drawable or images within toolbar or collapsing toolbar layout.

like image 154
Nihal Avatar answered Nov 17 '22 00:11

Nihal