Search code examples
androidandroid-palette

How to change tool bar and text background color using palette API picking from image dynamically


hi i am new to android i want to change my toolbar and text background color dynamically picking color using palette color picker from image ,any one please help me how to change my toolbar color i have multi images from json object how to change toolbar and text background from JSON,i want to change my toolbar color on every item when i click,plaese any one please help me how to got this HERE BELOW MY CODE

Meanswear.java

int pos = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_means_wear);

    image = (ImageView)findViewById(R.id.imageView);
    tv1 = (TextView)findViewById(R.id.textView2);

    tv3 = (TextView)findViewById(R.id.textView3);
    tv4 = (TextView)findViewById(R.id.textView6);
    tv5 = (TextView)findViewById(R.id.textView5);
    tex1 = (TextView)findViewById(R.id.textView1);
    text2 = (TextView)findViewById(R.id.textView4);

    btn = (Button)findViewById(R.id.button);
    pos = Integer.parseInt((getIntent().getExtras()).getString("pos"));

    tv1.setText(Women_clothing.gridData.get(pos).getDocumentName());

    tv3.setText(Women_clothing.gridData.get(pos).getDocumentContent());
    tv4.setText(Women_clothing.gridData.get(pos).getOffer());
    tv5.setText(Women_clothing.gridData.get(pos).getAddress());

    Picasso.with(getApplicationContext()).load((Women_clothing.gridData.get(pos)).getDocumentFile()).into(image);
}

Solution

  • Try this following

    public static void colorizeToolbar(Toolbar toolbarView, int toolbarIconsColor, Activity activity) {
        final PorterDuffColorFilter colorFilter
                = new PorterDuffColorFilter(toolbarIconsColor, PorterDuff.Mode.MULTIPLY);
    
        for(int i = 0; i < toolbarView.getChildCount(); i++) {
            final View v = toolbarView.getChildAt(i);
    
            //Step 1 : Changing the color of back button (or open drawer button).
            if(v instanceof ImageButton) {
                //Action Bar back button
                ((ImageButton)v).getDrawable().setColorFilter(colorFilter);
            }
    
            if(v instanceof ActionMenuView) {
                for(int j = 0; j < ((ActionMenuView)v).getChildCount(); j++) {
    
                    //Step 2: Changing the color of any ActionMenuViews - icons that
                    //are not back button, nor text, nor overflow menu icon.
                    final View innerView = ((ActionMenuView)v).getChildAt(j);
    
                    if(innerView instanceof ActionMenuItemView) {
                        int drawablesCount = ((ActionMenuItemView)innerView).getCompoundDrawables().length;
                        for(int k = 0; k < drawablesCount; k++) {
                            if(((ActionMenuItemView)innerView).getCompoundDrawables()[k] != null) {
                                final int finalK = k;
    
                                //Important to set the color filter in seperate thread, 
                                //by adding it to the message queue
                                //Won't work otherwise.
                                innerView.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        ((ActionMenuItemView) innerView).getCompoundDrawables()[finalK].setColorFilter(colorFilter);
                                    }
                                });
                            }
                        }
                    }
                }
            }
    
            //Step 3: Changing the color of title and subtitle.
            toolbarView.setTitleTextColor(toolbarIconsColor);
            toolbarView.setSubtitleTextColor(toolbarIconsColor);
    
            //Step 4: Changing the color of the Overflow Menu icon.
            setOverflowButtonColor(activity, colorFilter);
        }
    }
    

    See this Example 1 Example 2