Skip to content

Commit 5bfb7c7

Browse files
committed
Added themehelper class, going to rationalize all theming and system bar configuration with the same methods accross all the application.
1 parent 1b5edeb commit 5bfb7c7

File tree

12 files changed

+184
-233
lines changed

12 files changed

+184
-233
lines changed

AI_MultiBarcodes_Capture/src/main/java/com/zebra/ai_multibarcodes_capture/AboutActivity.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.zebra.ai_multibarcodes_capture.helpers.LocaleHelper;
1818
import com.zebra.ai_multibarcodes_capture.helpers.LogUtils;
19+
import com.zebra.ai_multibarcodes_capture.helpers.ThemeHelpers;
1920

2021
import android.content.SharedPreferences;
2122
import static com.zebra.ai_multibarcodes_capture.helpers.Constants.*;
@@ -37,10 +38,12 @@ protected void onCreate(Bundle savedInstanceState) {
3738
super.onCreate(savedInstanceState);
3839

3940
// Apply theme before setting content view
40-
applyTheme();
41+
ThemeHelpers.applyTheme(this);
4142

4243
setContentView(R.layout.activity_about);
4344

45+
ThemeHelpers.configureSystemBars(this, R.id.cl_about_activity);
46+
4447
// Set up the toolbar and back button
4548
ImageView backButton = findViewById(R.id.back_button);
4649
backButton.setOnClickListener(new View.OnClickListener() {
@@ -199,16 +202,6 @@ private String getVersionString(String dependencyName) {
199202
}
200203
}
201204

202-
private void applyTheme() {
203-
SharedPreferences sharedPreferences = getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
204-
String theme = sharedPreferences.getString(SHARED_PREFERENCES_THEME, SHARED_PREFERENCES_THEME_DEFAULT);
205-
206-
if ("modern".equals(theme)) {
207-
setTheme(R.style.Base_Theme_AIMultiBarcodes_Capture_Modern);
208-
} else {
209-
setTheme(R.style.Base_Theme_AIMultiBarcodes_Capture_Legacy);
210-
}
211-
}
212205

213206
@Override
214207
protected void attachBaseContext(Context newBase) {

AI_MultiBarcodes_Capture/src/main/java/com/zebra/ai_multibarcodes_capture/EntryChoiceActivity.java

Lines changed: 5 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.zebra.ai_multibarcodes_capture.helpers.LocaleHelper;
5050
import com.zebra.ai_multibarcodes_capture.helpers.LogUtils;
5151
import com.zebra.ai_multibarcodes_capture.helpers.PreferencesHelper;
52+
import com.zebra.ai_multibarcodes_capture.helpers.ThemeHelpers;
5253
import com.zebra.ai_multibarcodes_capture.java.CameraXLivePreviewActivity;
5354
import com.zebra.ai_multibarcodes_capture.managedconfig.ManagedConfigurationReceiver;
5455
import com.zebra.ai_multibarcodes_capture.sessionmanagement.SessionViewerActivity;
@@ -136,13 +137,14 @@ protected void onCreate(Bundle savedInstanceState) {
136137
super.onCreate(savedInstanceState);
137138

138139
// Apply theme before setting content view
139-
applyTheme();
140+
ThemeHelpers.applyTheme(this);
141+
140142

141143
binding = ActivityEntryChoiceBinding.inflate(getLayoutInflater());
142144
setContentView(binding.getRoot());
143145

144146
// Configure system bar colors
145-
configureSystemBars();
147+
ThemeHelpers.configureSystemBars(this, R.id.cl_entry_choice_activity);
146148

147149
// Initializing the AI Vision SDK
148150
try {
@@ -357,85 +359,6 @@ public void onClick(View view) {
357359
}
358360

359361

360-
361-
private void configureSystemBars() {
362-
Window window = getWindow();
363-
364-
// 1. Set the Navigation Bar Background Color to Black
365-
window.setNavigationBarColor(Color.BLACK);
366-
367-
// 2. Control the Navigation Bar Icon Color (Light/White)
368-
// Ensure the system bars are drawn over the app's content
369-
WindowCompat.setDecorFitsSystemWindows(window, false);
370-
371-
// Use the compatibility controller for managing bar appearance
372-
WindowInsetsControllerCompat controller = new WindowInsetsControllerCompat(window, window.getDecorView());
373-
374-
// Request light navigation bar icons (white)
375-
// Setting this to 'false' tells the system to use light icons on a dark background.
376-
controller.setAppearanceLightNavigationBars(false);
377-
378-
// Force status bar color
379-
View rootLayout = findViewById(R.id.cl_entry_choice_activity);
380-
381-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { // Android 15+
382-
383-
// 1. Set Navigation Bar background color using the WindowInsetsListener on decorView
384-
window.getDecorView().setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
385-
@Override
386-
public WindowInsets onApplyWindowInsets(View view, WindowInsets insets) {
387-
// Set the background color to the view (decorView) - BLACK for navigation bar
388-
view.setBackgroundColor(Color.BLACK);
389-
return insets;
390-
}
391-
});
392-
393-
// 2. Handle Status Bar color and Root Layout padding using ViewCompat
394-
ViewCompat.setOnApplyWindowInsetsListener(rootLayout, (v, windowInsets) -> {
395-
// Get the system bar insets (status bar and navigation bar area)
396-
// Use getInsets(WindowInsetsCompat.Type.systemBars())
397-
// equivalent to the Kotlin line
398-
androidx.core.graphics.Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
399-
int statusBarHeight = insets.top;
400-
401-
// Below code is for adding padding top and bottom (setting margins on the rootLayout)
402-
ViewGroup.LayoutParams lp = v.getLayoutParams();
403-
if (lp instanceof ViewGroup.MarginLayoutParams) {
404-
ViewGroup.MarginLayoutParams marginLp = (ViewGroup.MarginLayoutParams) lp;
405-
406-
// The Kotlin updateLayoutParams<MarginLayoutParams> block is equivalent to this:
407-
marginLp.topMargin = insets.top;
408-
marginLp.bottomMargin = insets.bottom;
409-
v.setLayoutParams(marginLp); // Apply the updated layout params
410-
}
411-
412-
413-
// 3. Create and add a separate Status Bar View
414-
View statusBarView = new View(getApplicationContext());
415-
416-
// Below code is for setting color and height to notification bar
417-
// Height is the status bar height
418-
statusBarView.setLayoutParams(new ViewGroup.LayoutParams(
419-
ViewGroup.LayoutParams.MATCH_PARENT,
420-
statusBarHeight
421-
));
422-
423-
// Set the status bar color using ContextCompat
424-
statusBarView.setBackgroundColor(androidx.appcompat.R.attr.colorPrimary);
425-
426-
// Add the view to the activity's content view group
427-
addContentView(statusBarView, statusBarView.getLayoutParams());
428-
429-
// Consume the insets so they aren't passed down further
430-
return WindowInsetsCompat.CONSUMED;
431-
});
432-
433-
} else {
434-
// For Android 14 and below
435-
window.setStatusBarColor(androidx.appcompat.R.attr.colorPrimary);
436-
}
437-
}
438-
439362
private void updateCards()
440363
{
441364
if (eProcessingMode == EProcessingMode.FILE) {
@@ -487,7 +410,7 @@ protected void onResume() {
487410
super.onResume();
488411

489412
// Re-apply system bar appearance to ensure it persists
490-
configureSystemBars();
413+
ThemeHelpers.configureSystemBars(this, R.id.cl_entry_choice_activity);
491414

492415
checkCameraPermission();
493416

@@ -640,17 +563,6 @@ public void onRequestPermissionsResult(int requestCode,
640563
}
641564
}
642565

643-
private void applyTheme() {
644-
SharedPreferences sharedPreferences = getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
645-
String theme = sharedPreferences.getString(SHARED_PREFERENCES_THEME, SHARED_PREFERENCES_THEME_DEFAULT);
646-
647-
if ("modern".equals(theme)) {
648-
setTheme(R.style.Base_Theme_AIMultiBarcodes_Capture_Modern);
649-
} else {
650-
setTheme(R.style.Base_Theme_AIMultiBarcodes_Capture_Legacy);
651-
}
652-
}
653-
654566
@Override
655567
protected void attachBaseContext(Context newBase) {
656568
String languageCode = LocaleHelper.getCurrentLanguageCode(newBase);

AI_MultiBarcodes_Capture/src/main/java/com/zebra/ai_multibarcodes_capture/dataeditor/BarcodeDataEditorActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected void onCreate(Bundle savedInstanceState) {
7676
private void configureSystemBars() {
7777
Window window = getWindow();
7878
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
79-
window.setStatusBarColor(ContextCompat.getColor(this, R.color.zebra_pantone_285));
79+
window.setStatusBarColor(androidx.appcompat.R.attr.colorPrimary);
8080
window.setNavigationBarColor(ContextCompat.getColor(this, android.R.color.black));
8181
}
8282

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.zebra.ai_multibarcodes_capture.helpers;
2+
3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
import android.graphics.Color;
6+
import android.os.Build;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.view.Window;
10+
import android.view.WindowInsets;
11+
12+
import com.zebra.ai_multibarcodes_capture.R;
13+
14+
import androidx.appcompat.app.AppCompatActivity;
15+
import androidx.core.view.ViewCompat;
16+
import androidx.core.view.WindowCompat;
17+
import androidx.core.view.WindowInsetsCompat;
18+
import androidx.core.view.WindowInsetsControllerCompat;
19+
20+
import static com.zebra.ai_multibarcodes_capture.helpers.Constants.SHARED_PREFERENCES_THEME;
21+
import static com.zebra.ai_multibarcodes_capture.helpers.Constants.SHARED_PREFERENCES_THEME_DEFAULT;
22+
23+
public class ThemeHelpers {
24+
public static void configureSystemBars(AppCompatActivity activity, int resourceID) {
25+
Window window = activity.getWindow();
26+
27+
// 1. Set the Navigation Bar Background Color to Black
28+
window.setNavigationBarColor(Color.BLACK);
29+
30+
// 2. Control the Navigation Bar Icon Color (Light/White)
31+
// Ensure the system bars are drawn over the app's content
32+
WindowCompat.setDecorFitsSystemWindows(window, false);
33+
34+
// Use the compatibility controller for managing bar appearance
35+
WindowInsetsControllerCompat controller = new WindowInsetsControllerCompat(window, window.getDecorView());
36+
37+
// Request light navigation bar icons (white)
38+
// Setting this to 'false' tells the system to use light icons on a dark background.
39+
controller.setAppearanceLightNavigationBars(false);
40+
41+
// Force status bar color
42+
View rootLayout = activity.findViewById(resourceID);
43+
44+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { // Android 15+
45+
46+
// 1. Set Navigation Bar background color using the WindowInsetsListener on decorView
47+
window.getDecorView().setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
48+
@Override
49+
public WindowInsets onApplyWindowInsets(View view, WindowInsets insets) {
50+
// Set the background color to the view (decorView) - BLACK for navigation bar
51+
view.setBackgroundColor(Color.BLACK);
52+
return insets;
53+
}
54+
});
55+
56+
// 2. Handle Status Bar color and Root Layout padding using ViewCompat
57+
ViewCompat.setOnApplyWindowInsetsListener(rootLayout, (v, windowInsets) -> {
58+
// Get the system bar insets (status bar and navigation bar area)
59+
// Use getInsets(WindowInsetsCompat.Type.systemBars())
60+
// equivalent to the Kotlin line
61+
androidx.core.graphics.Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
62+
int statusBarHeight = insets.top;
63+
64+
// Below code is for adding padding top and bottom (setting margins on the rootLayout)
65+
ViewGroup.LayoutParams lp = v.getLayoutParams();
66+
if (lp instanceof ViewGroup.MarginLayoutParams) {
67+
ViewGroup.MarginLayoutParams marginLp = (ViewGroup.MarginLayoutParams) lp;
68+
69+
// The Kotlin updateLayoutParams<MarginLayoutParams> block is equivalent to this:
70+
marginLp.topMargin = insets.top;
71+
marginLp.bottomMargin = insets.bottom;
72+
v.setLayoutParams(marginLp); // Apply the updated layout params
73+
}
74+
75+
76+
// 3. Create and add a separate Status Bar View
77+
View statusBarView = new View(activity.getApplicationContext());
78+
79+
// Below code is for setting color and height to notification bar
80+
// Height is the status bar height
81+
statusBarView.setLayoutParams(new ViewGroup.LayoutParams(
82+
ViewGroup.LayoutParams.MATCH_PARENT,
83+
statusBarHeight
84+
));
85+
86+
// Set the status bar color using ContextCompat
87+
statusBarView.setBackgroundColor(androidx.appcompat.R.attr.colorPrimary);
88+
89+
// Add the view to the activity's content view group
90+
activity.addContentView(statusBarView, statusBarView.getLayoutParams());
91+
92+
// Consume the insets so they aren't passed down further
93+
return WindowInsetsCompat.CONSUMED;
94+
});
95+
96+
} else {
97+
// For Android 14 and below
98+
window.setStatusBarColor(androidx.appcompat.R.attr.colorPrimary);
99+
}
100+
}
101+
102+
public static void applyTheme(AppCompatActivity activity) {
103+
SharedPreferences sharedPreferences = activity.getSharedPreferences(activity.getPackageName(), Context.MODE_PRIVATE);
104+
String theme = sharedPreferences.getString(SHARED_PREFERENCES_THEME, SHARED_PREFERENCES_THEME_DEFAULT);
105+
106+
if ("modern".equals(theme)) {
107+
activity.setTheme(R.style.Base_Theme_AIMultiBarcodes_Capture_Modern);
108+
} else {
109+
activity.setTheme(R.style.Base_Theme_AIMultiBarcodes_Capture_Legacy);
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)