Fix font issues

This commit is contained in:
serso 2016-04-29 18:52:25 +02:00
parent 10daedc21b
commit f618f2cd44
9 changed files with 44 additions and 38 deletions

View File

@ -87,7 +87,13 @@ public abstract class BaseActivity extends AppCompatActivity implements SharedPr
public static void setFont(@Nonnull View view, @Nonnull Typeface newTypeface) {
if (view instanceof TextView) {
setFont((TextView) view, newTypeface);
final TextView textView = (TextView) view;
final Typeface oldTypeface = textView.getTypeface();
if (oldTypeface == newTypeface) {
return;
}
final int style = oldTypeface != null ? oldTypeface.getStyle() : Typeface.NORMAL;
textView.setTypeface(newTypeface, style);
} else if (view instanceof DirectionDragImageButton) {
((DirectionDragImageButton) view).setTypeface(newTypeface);
}
@ -98,15 +104,6 @@ public abstract class BaseActivity extends AppCompatActivity implements SharedPr
return mode;
}
public static void setFont(@Nonnull TextView view, @Nonnull Typeface newTypeface) {
final Typeface oldTypeface = view.getTypeface();
if (oldTypeface == newTypeface) {
return;
}
final int style = oldTypeface != null ? oldTypeface.getStyle() : Typeface.NORMAL;
view.setTypeface(newTypeface, style);
}
public boolean restartIfModeChanged() {
final Preferences.Gui.Mode newMode = Preferences.Gui.mode.getPreference(preferences);
if (newMode != mode) {
@ -162,7 +159,7 @@ public abstract class BaseActivity extends AppCompatActivity implements SharedPr
}
ButterKnife.bind(this, this);
fixFonts(mainView);
fixFonts(mainView, typeface);
initToolbar();
populateTabs(tabs);
tabs.onCreate();
@ -285,14 +282,15 @@ public abstract class BaseActivity extends AppCompatActivity implements SharedPr
fab.setOnClickListener(listener);
}
protected void fixFonts(@Nonnull View root) {
// some devices ship own fonts which causes issues with rendering. Let's use our own font for all text views
App.processViewsOfType(root, TextView.class, new App.ViewProcessor<TextView>() {
@Override
public void process(@Nonnull TextView view) {
setFont(view, typeface);
public static void fixFonts(@Nonnull View view, @Nonnull Typeface typeface) {
if (view instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) view;
for (int index = 0; index < group.getChildCount(); index++) {
fixFonts(group.getChildAt(index), typeface);
}
});
} else {
setFont(view, typeface);
}
}
@Override

View File

@ -3,6 +3,7 @@ package org.solovyev.android.calculator;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@ -29,6 +30,8 @@ public abstract class BaseDialogFragment extends DialogFragment implements View.
protected SharedPreferences preferences;
@Inject
Ga ga;
@Inject
Typeface typeface;
@Nullable
private Button positiveButton;
@Nullable
@ -57,6 +60,7 @@ public abstract class BaseDialogFragment extends DialogFragment implements View.
if (view != null) {
final int spacing = context.getResources().getDimensionPixelSize(R.dimen.cpp_dialog_spacing);
b.setView(view, spacing, spacing, spacing, spacing);
BaseActivity.fixFonts(view, typeface);
}
onPrepareDialog(b);
final AlertDialog dialog = b.create();

View File

@ -1,31 +1,29 @@
package org.solovyev.android.calculator;
import static android.view.Menu.NONE;
import static org.solovyev.android.calculator.App.cast;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.*;
import org.solovyev.android.calculator.ads.AdUi;
import org.solovyev.android.plotter.Check;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import static android.view.Menu.NONE;
import static org.solovyev.android.calculator.App.cast;
public abstract class BaseFragment extends Fragment {
private final int layout;
@Inject
AdUi adUi;
@Inject
Typeface typeface;
protected BaseFragment(@LayoutRes int layout) {
this.layout = layout;
@ -61,6 +59,7 @@ public abstract class BaseFragment extends Fragment {
Bundle savedInstanceState) {
final View view = inflater.inflate(layout, container, false);
adUi.onCreateView(view);
BaseActivity.fixFonts(view, typeface);
return view;
}

View File

@ -266,6 +266,7 @@ public class FloatingCalculatorView {
final Preferences.SimpleTheme resolvedTheme = theme.resolveThemeFor(appTheme);
this.context = new ContextThemeWrapper(context, resolvedTheme.light ? R.style.Cpp_Theme_Light : R.style.Cpp_Theme);
this.root = View.inflate(this.context, theme.getOnscreenLayout(appTheme), null);
BaseActivity.fixFonts(this.root, typeface);
final State persistedState = State.fromPrefs(myPreferences);
if (persistedState != null) {
this.state = persistedState;
@ -357,7 +358,6 @@ public class FloatingCalculatorView {
} else {
BaseKeyboardUi.adjustButton(button);
}
BaseActivity.setFont(button, typeface);
}
final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

View File

@ -4,7 +4,6 @@ import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
@ -47,8 +46,6 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
@Inject
SharedPreferences preferences;
@Inject
Typeface typeface;
@Inject
Keyboard keyboard;
@Inject
Editor editor;
@ -133,7 +130,6 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
// we call android.view.View.performHapticFeedback(int, int) from #onClick
button.setHapticFeedbackEnabled(false);
button.setOnClickListener(this);
BaseActivity.setFont(button, typeface);
}
protected final void prepareButton(@Nullable DirectionDragImageButton button) {

View File

@ -28,7 +28,6 @@ import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.keyboard.BaseKeyboardUi;
@ -73,7 +72,6 @@ public class ChooseModeWizardStep extends WizardFragment implements AdapterView.
spinner.setOnItemSelectedListener(this);
button = (DirectionDragButton) root.findViewById(R.id.wizard_mode_button);
BaseActivity.setFont(button, typeface);
Adjuster.adjustText(button, BaseKeyboardUi.getTextScale(getActivity()));
description = (TextView) root.findViewById(R.id.wizard_mode_description);
updateDescription(mode);

View File

@ -30,6 +30,7 @@ import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.Spinner;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.keyboard.BaseKeyboardUi;
@ -69,7 +70,7 @@ public class ChooseThemeWizardStep extends WizardFragment implements AdapterView
spinner.setOnItemSelectedListener(this);
preview = (FrameLayout) root.findViewById(R.id.wizard_theme_preview);
updateImage(theme);
updatePreview(theme);
}
private int findPosition(@Nonnull Preferences.Gui.Theme theme) {
@ -82,7 +83,7 @@ public class ChooseThemeWizardStep extends WizardFragment implements AdapterView
return 0;
}
private void updateImage(@Nonnull Preferences.Gui.Theme theme) {
private void updatePreview(@Nonnull Preferences.Gui.Theme theme) {
preview.removeAllViews();
final ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), theme.theme);
LayoutInflater.from(context).inflate(R.layout.cpp_wizard_step_choose_theme_preview, preview);
@ -90,6 +91,7 @@ public class ChooseThemeWizardStep extends WizardFragment implements AdapterView
@Override
public void process(@Nonnull View view) {
BaseKeyboardUi.adjustButton(view);
BaseActivity.setFont(view, typeface);
}
});
}
@ -98,7 +100,7 @@ public class ChooseThemeWizardStep extends WizardFragment implements AdapterView
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final ThemeUi theme = adapter.getItem(position);
Preferences.Gui.theme.putPreference(preferences, theme.theme);
updateImage(theme.theme);
updatePreview(theme.theme);
}
@Override

View File

@ -83,7 +83,6 @@ public class DragButtonWizardStep extends WizardFragment {
return false;
}
});
BaseActivity.setFont(dragButton, typeface);
Adjuster.adjustText(dragButton, BaseKeyboardUi.getTextScale(getActivity()));
actionTextView = (TextView) root.findViewById(R.id.wizard_dragbutton_action_textview);
if (savedInstanceState != null) {

View File

@ -1,6 +1,7 @@
package org.solovyev.android.calculator.wizard;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment;
@ -10,6 +11,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.solovyev.android.calculator.AppComponent;
import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.release.ChooseThemeReleaseNoteFragment;
import org.solovyev.android.calculator.release.ChooseThemeReleaseNoteStep;
@ -35,6 +37,8 @@ public abstract class WizardFragment extends Fragment implements View.OnClickLis
protected TextView prevButton;
@Inject
SharedPreferences preferences;
@Inject
Typeface typeface;
private WizardStep step;
@Override
@ -112,6 +116,12 @@ public abstract class WizardFragment extends Fragment implements View.OnClickLis
return view;
}
@Override
public void onViewCreated(View view, @android.support.annotation.Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
BaseActivity.fixFonts(view, typeface);
}
protected final void setupNextButton(int textResId) {
assert nextButton != null;
nextButton.setText(textResId);