Procházet zdrojové kódy

Set tint color for text buttons in Toolbar

Guy Carmeli před 8 roky
rodič
revize
4d1a940c09

+ 42
- 5
android/app/src/main/java/com/reactnativenavigation/views/RnnToolBar.java Zobrazit soubor

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