1

I have a toolbar that change color on scrolling basicly color is transparent and when scrolling it change color I want to set only one color instead of transparent and let one color be shown

I have tried to change color on toolbar xml and display scroling in activity

this my toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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"
    style="@style/ToolBarStyle"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:visibility="visible"
    app:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp" />

this toolbar is included in another activity this the activity

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/toolbar_actionbar"
        layout="@layout/toolbar_default"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_actionbar">

    </FrameLayout>

</RelativeLayout>

and the activity.java is

package com.sherdle.universal.util;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.sherdle.universal.R;
import com.sherdle.universal.util.layout.TrackingScrollView;

public abstract class DetailActivity extends AppCompatActivity {

    protected RelativeLayout coolblue;
    protected Toolbar mToolbar;
    protected ImageView thumb;

    boolean FadeBar = true;

    protected int mScrollableHeaderHeight;
    protected int latestAlpha;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected void setUpHeader(String imageurl){
        if (isTablet()){
            FadeBar = false;
        } else {
            coolblue.setVisibility(View.GONE);
        }

        if ((null != imageurl && !imageurl.equals("") && !imageurl.equals("null"))) {
            setParralaxHeader();
        } else if (!isTablet()){
            thumb.getLayoutParams().height = getActionBarHeight();
            FadeBar = false;
        } else if (isTablet()){
            setParralaxHeader();
            thumb.getLayoutParams().height = 0;
        }

        if (FadeBar) {
            mToolbar.getBackground().mutate().setAlpha(0);
            /*Helper.setStatusBarColor(DetailActivity.this,
                    ContextCompat.getColor(this, R.color.black)); Mahmoud comment*/
            Helper.setStatusBarColor(DetailActivity.this,
                    getResources().getColor(R.color.myPrimaryColor));
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }
    }
    // setBackgroundColor(getResources().getColor(R.color.WHITE));
    private void setParralaxHeader(){
        if(isTablet()){
            mScrollableHeaderHeight = coolblue.getLayoutParams().height;
        } else {
            mScrollableHeaderHeight = thumb.getLayoutParams().height;
   /*         thumb.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    thumb.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    mScrollableHeaderHeight= thumb.getHeight(); //height is ready
                }
            });*/
            mToolbar.setLogo(R.drawable.drawer_header);
        }

        ((TrackingScrollView) findViewById(R.id.scroller))
                .setOnScrollChangedListener(new TrackingScrollView.OnScrollChangedListener() {
                    @Override
                    public void onScrollChanged(
                            TrackingScrollView source, int left,
                            int top, int oldleft, int oldtop) {
                        handleScroll(top);
                    }
                });
    }

    private void handleScroll(int top) {

        mToolbar.setBackgroundColor(getResources().getColor(R.color.myPrimaryColor));
       // mToolbar.setLogo(R.drawable.drawer_header);
        /*int scrolledHeaderHeight = top;
        if (!isTablet()){
            scrolledHeaderHeight = Math.min(mScrollableHeaderHeight, Math.max(0, top));
        }

        ViewGroup.MarginLayoutParams headerParams = null;
        int newHeaderHeight = 0;
        if (isTablet()){
            headerParams = (ViewGroup.MarginLayoutParams) coolblue
                    .getLayoutParams();
            newHeaderHeight = mScrollableHeaderHeight - (scrolledHeaderHeight) / 2;
        } else {
            headerParams = (ViewGroup.MarginLayoutParams) thumb
                    .getLayoutParams();
            newHeaderHeight = mScrollableHeaderHeight - scrolledHeaderHeight;
        }

        if (headerParams.height != newHeaderHeight) {
            // Transfer image height to margin top
            headerParams.height = newHeaderHeight;
            if (!isTablet()){
                headerParams.topMargin = scrolledHeaderHeight;

                // Invalidate view
                thumb.setLayoutParams(headerParams);
            } else {
                coolblue.setLayoutParams(headerParams);
            }
        }
Mahmoud comment*/
        if (FadeBar) {
            /*final int imageheaderHeight = thumb.getHeight()
                    - getSupportActionBar().getHeight(); Mahmoud comment*/
            // t=how far you scrolled
            // ratio is from 0,0.1,0.2,...1
           /* final float ratio = (float) Math.min(Math.max(top, 0),
                    imageheaderHeight) / imageheaderHeight; Mahmoud comment*/
            // setting the new alpha value from 0-255 or transparent to opaque
           // final int newAlpha = (int) (ratio * 255);  Mahmoud comment
           // if (newAlpha != latestAlpha) {
                /*mToolbar.getBackground().mutate().setAlpha(newAlpha);
                Helper.setStatusBarColor(DetailActivity.this,
                        blendColors(ratio, this)); Mahmoud comment*/
              /*  mToolbar.setBackgroundColor(getResources().getColor(R.color.myPrimaryColor));
                mToolbar.setLogo(R.drawable.drawer_header); Mahmoud comment added*/
          //  }

           // latestAlpha = newAlpha;
        }
    }

    private boolean isTablet(){
        return getResources().getBoolean(R.bool.isTablet);
    }

    private int getActionBarHeight() {
        int actionBarHeight = getSupportActionBar().getHeight();
        if (actionBarHeight != 0)
            return actionBarHeight;
        final TypedValue tv = new TypedValue();
        if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
                    getResources().getDisplayMetrics());
        return actionBarHeight;
    }

    private static int blendColors(float ratio, Context c) {
        int color1 = ContextCompat.getColor(c, R.color.myPrimaryDarkColor);
        int color2 = ContextCompat.getColor(c, R.color.black);
        final float inverseRation = 1f - ratio;
        float r = (Color.red(color1) * ratio)
                + (Color.red(color2) * inverseRation);
        float g = (Color.green(color1) * ratio)
                + (Color.green(color2) * inverseRation);
        float b = (Color.blue(color1) * ratio)
                + (Color.blue(color2) * inverseRation);
        return Color.rgb((int) r, (int) g, (int) b);
    }

    @Override
    public void onPause() {
        super.onPause();
        mToolbar.getBackground().mutate().setAlpha(255);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (FadeBar)
            mToolbar.getBackground().mutate().setAlpha(latestAlpha);
    }

}

it take me already so mush time so please help me to fix it

no error

Mahmoud
  • 167
  • 2
  • 7

1 Answers1

0

This may help you please try and check below code and also check your toolbar style that you have add in Toolbar and you have to put this code in onScroll method of the page:-

mToolbar.setBackgroundDrawable(new ColorDrawable(0xff00DDED)); 
mToolbar.setBackgroundColor(0xff00DDED);

@Override
 public boolean dispatchTouchEvent(MotionEvent ev) {
    Toast.makeText(this, "dispatch motion", Toast.LENGTH_SHORT).show();
    return true;
}

Change actionbar color programmatically more than once

Thanks

  • The toolbar is transparent and change color on scrolling I want the color appear in the activity and no transparent color and any color change on scrolling – Mahmoud Jun 14 '19 at 08:27
  • this is "ToolBarStyle" ```````` "ToolBar" style ```` ```` – Mahmoud Jun 14 '19 at 08:32
  • I changed the Activity code where the scrolling code are executed please check and help me – Mahmoud Jun 14 '19 at 13:46