11package com .d4rk .androidtutorials .java .ui .screens .onboarding ;
22
3+ import android .content .res .Resources ;
4+ import android .graphics .Typeface ;
35import android .os .Bundle ;
6+ import android .util .Log ;
47import android .view .LayoutInflater ;
58import android .view .View ;
69import android .view .ViewGroup ;
10+ import android .widget .TextView ;
711
812import androidx .annotation .NonNull ;
913import androidx .annotation .Nullable ;
1721
1822public class FontFragment extends Fragment {
1923
24+ private static final String TAG = "FontFragment" ;
25+ 2026 private FragmentOnboardingFontBinding binding ;
2127 private OnboardingViewModel viewModel ;
2228 private MaterialRadioButton [] radioButtons ;
@@ -53,13 +59,13 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
5359 binding .cardGoogleSansCode
5460 };
5561
56- binding .optionAudiowide .titleText . setTypeface ( ResourcesCompat . getFont ( requireContext () , R .font .font_audiowide ) );
57- binding .optionFiraCode .titleText . setTypeface ( ResourcesCompat . getFont ( requireContext () , R .font .font_fira_code ) );
58- binding .optionJetbrainsMono .titleText . setTypeface ( ResourcesCompat . getFont ( requireContext () , R .font .font_jetbrains_mono ) );
59- binding .optionNotoSansMono .titleText . setTypeface ( ResourcesCompat . getFont ( requireContext () , R .font .font_noto_sans_mono ) );
60- binding .optionPoppins .titleText . setTypeface ( ResourcesCompat . getFont ( requireContext () , R .font .font_poppins ) );
61- binding .optionRobotoMono .titleText . setTypeface ( ResourcesCompat . getFont ( requireContext () , R .font .font_roboto_mono ) );
62- binding .optionGoogleSansCode .titleText . setTypeface ( ResourcesCompat . getFont ( requireContext () , R .font .font_google_sans_code ) );
62+ applyFontIfAvailable ( binding .optionAudiowide .titleText , R .font .font_audiowide );
63+ applyFontIfAvailable ( binding .optionFiraCode .titleText , R .font .font_fira_code );
64+ applyFontIfAvailable ( binding .optionJetbrainsMono .titleText , R .font .font_jetbrains_mono );
65+ applyFontIfAvailable ( binding .optionNotoSansMono .titleText , R .font .font_noto_sans_mono );
66+ applyFontIfAvailable ( binding .optionPoppins .titleText , R .font .font_poppins );
67+ applyFontIfAvailable ( binding .optionRobotoMono .titleText , R .font .font_roboto_mono );
68+ applyFontIfAvailable ( binding .optionGoogleSansCode .titleText , R .font .font_google_sans_code );
6369
6470 for (int i = 0 ; i < radioButtons .length ; i ++) {
6571 int index = i ;
@@ -80,6 +86,17 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
8086 selectOption (index );
8187 }
8288
89+ private void applyFontIfAvailable (@ NonNull TextView textView , int fontResId ) {
90+ try {
91+ Typeface typeface = ResourcesCompat .getFont (requireContext (), fontResId );
92+ if (typeface != null ) {
93+ textView .setTypeface (typeface );
94+ }
95+ } catch (Resources .NotFoundException exception ) {
96+ Log .w (TAG , "Font resource not found: " + fontResId , exception );
97+ }
98+ }
99+ 83100 private void selectOption (int index ) {
84101 for (int i = 0 ; i < radioButtons .length ; i ++) {
85102 radioButtons [i ].setChecked (i == index );
0 commit comments