Browse Source

Set tint color for text buttons in Toolbar

Guy Carmeli 8 years ago
parent
commit
4d1a940c09

+ 42
- 5
android/app/src/main/java/com/reactnativenavigation/views/RnnToolBar.java View File

@@ -1,10 +1,12 @@
1 1
 package com.reactnativenavigation.views;
2 2
 
3 3
 import android.annotation.SuppressLint;
4
+import android.app.Activity;
4 5
 import android.content.Context;
5 6
 import android.content.res.Resources;
6 7
 import android.graphics.drawable.Drawable;
7 8
 import android.os.AsyncTask;
9
+import android.support.annotation.ColorInt;
8 10
 import android.support.annotation.UiThread;
9 11
 import android.support.v4.content.ContextCompat;
10 12
 import android.support.v4.content.res.ResourcesCompat;
@@ -14,6 +16,9 @@ import android.support.v7.widget.Toolbar;
14 16
 import android.util.AttributeSet;
15 17
 import android.view.Menu;
16 18
 import android.view.MenuItem;
19
+import android.view.View;
20
+import android.view.ViewTreeObserver;
21
+import android.widget.TextView;
17 22
 
18 23
 import com.reactnativenavigation.R;
19 24
 import com.reactnativenavigation.activities.BaseReactActivity;
@@ -23,6 +28,7 @@ import com.reactnativenavigation.utils.ContextProvider;
23 28
 import com.reactnativenavigation.utils.ImageUtils;
24 29
 
25 30
 import java.lang.ref.WeakReference;
31
+import java.util.ArrayList;
26 32
 import java.util.HashMap;
27 33
 import java.util.List;
28 34
 import java.util.Map;
@@ -35,6 +41,7 @@ public class RnnToolBar extends Toolbar {
35 41
     private List<Screen> mScreens;
36 42
     private AsyncTask mSetupToolbarTask;
37 43
     private Drawable mBackground;
44
+    private ArrayList<View> mMenuItems;
38 45
 
39 46
     public RnnToolBar(Context context) {
40 47
         super(context);
@@ -52,6 +59,7 @@ public class RnnToolBar extends Toolbar {
52 59
     }
53 60
 
54 61
     private void init() {
62
+        mMenuItems = new ArrayList<>();
55 63
         mBackground = getBackground();
56 64
     }
57 65
 
@@ -168,7 +176,7 @@ public class RnnToolBar extends Toolbar {
168 176
         private final List<Button> mOldButtons;
169 177
         private final List<Button> mNewButtons;
170 178
         private final WeakReference<RnnToolBar> mToolbarWR;
171
-        private final Integer mTintColor;
179
+        @ColorInt private final Integer mTintColor;
172 180
         private final int mIconDimensions;
173 181
 
174 182
         public SetupToolbarButtonsTask(RnnToolBar toolBar, Screen oldScreen, Screen newScreen) {
@@ -197,13 +205,12 @@ public class RnnToolBar extends Toolbar {
197 205
 
198 206
         @Override
199 207
         protected void onPostExecute(Map<String, Drawable> icons) {
200
-            Context context = ContextProvider.getActivityContext();
208
+            final Context context = ContextProvider.getActivityContext();
201 209
             if (context == null) {
202 210
                 return;
203 211
             }
204 212
 
205 213
             Menu menu = ((BaseReactActivity) context).getMenu();
206
-
207 214
             if (menu == null) {
208 215
                 RnnToolBar toolBar = mToolbarWR.get();
209 216
                 if (toolBar != null) {
@@ -223,30 +230,60 @@ public class RnnToolBar extends Toolbar {
223 230
             }
224 231
 
225 232
             // Add new screen buttons
233
+            final List<String> textButtons = new ArrayList<>();
234
+
226 235
             for (int i = 0; i < mNewButtons.size(); i++) {
227 236
                 Button button = mNewButtons.get(i);
228 237
                 MenuItem item = menu.add(Menu.NONE, button.getItemId(), i, button.title);
229 238
                 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
230 239
 
231
-                if (icons.containsKey(button.id)) {
240
+                // Set button icon
241
+                if (button.hasIcon()) {
232 242
                     Drawable icon = icons.get(button.id);
233 243
                     if (mTintColor != null) {
234 244
                         ImageUtils.tint(icon, mTintColor);
235 245
                     }
236 246
                     item.setIcon(icon);
247
+                } else {
248
+                    textButtons.add(button.title);
237 249
                 }
238 250
 
251
+                // Disable button if needed
239 252
                 if (button.disabled) {
240 253
                     item.setEnabled(false);
241 254
                 }
242 255
             }
243 256
 
244
-            RnnToolBar toolBar = mToolbarWR.get();
257
+            final RnnToolBar toolBar = mToolbarWR.get();
245 258
             if (toolBar != null) {
259
+                // Tint overflow icon which appears when there's not enough space in Toolbar for icons
246 260
                 if (mTintColor != null) {
247 261
                     ImageUtils.tint(toolBar.getOverflowIcon(), mTintColor);
248 262
                 }
249 263
 
264
+                // Tint text buttons
265
+                if (textButtons.size() > 0 && mTintColor != null) {
266
+                    final View decorView = ((Activity) context).getWindow().getDecorView();
267
+                    decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
268
+                        @Override
269
+                        public void onGlobalLayout() {
270
+                            decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
271
+
272
+                            // Find TextViews
273
+                            for (String text : textButtons) {
274
+                                decorView.findViewsWithText(toolBar.mMenuItems, text, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
275
+                            }
276
+
277
+                            // Set text color
278
+                            for (View button : toolBar.mMenuItems) {
279
+                                ((TextView) button).setTextColor(mTintColor);
280
+                            }
281
+
282
+                            toolBar.mMenuItems.clear();
283
+                        }
284
+                    });
285
+                }
286
+
250 287
                 toolBar.mSetupToolbarTask = null;
251 288
                 mToolbarWR.clear();
252 289
             }