Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright 2017 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.example.android.wearable.wear.messaging.util;
     17 
     18 import android.content.Context;
     19 import android.graphics.drawable.Drawable;
     20 import android.support.v4.content.ContextCompat;
     21 import android.support.v4.graphics.drawable.DrawableCompat;
     22 import android.view.Menu;
     23 import android.view.MenuItem;
     24 
     25 /** Helper method to tint menu item icons. */
     26 public class MenuTinter {
     27     private MenuTinter() {}
     28 
     29     /**
     30      * Tints the menu icons
     31      *
     32      * @param context resource context
     33      * @param menu menu to tint
     34      * @param refColor color to tint
     35      */
     36     public static void tintMenu(Context context, Menu menu, int refColor) {
     37         for (int i = 0; i < menu.size(); i++) {
     38             MenuItem item = menu.getItem(i);
     39             Drawable drawable = item.getIcon();
     40             if (drawable != null) {
     41                 drawable = DrawableCompat.wrap(drawable);
     42                 drawable.mutate();
     43                 DrawableCompat.setTint(drawable, ContextCompat.getColor(context, refColor));
     44                 item.setIcon(drawable);
     45             }
     46         }
     47     }
     48 }
     49