Java Code Examples for androidx.core.content.res.ResourcesCompat#getFont()

The following examples show how to use androidx.core.content.res.ResourcesCompat#getFont() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From user-interface-samples with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView textView = findViewById(R.id.styled_text);

    // This is a simple markdown parser, where:
    // Paragraphs starting with “> ” are transformed into quotes. Quotes can't contain
    // other markdown elements
    // Text enclosed in “`” will be transformed into inline code block
    // Lines starting with “+ ” or “* ” will be transformed into bullet points. Bullet
    // points can contain nested markdown elements, like code.
    int bulletPointColor = ContextCompat.getColor(this, R.color.colorAccent);
    int codeBackgroundColor = ContextCompat.getColor(this, R.color.code_background);
    Typeface codeBlockTypeface = ResourcesCompat.getFont(this, R.font.inconsolata);

    CharSequence text = new MarkdownBuilder(bulletPointColor, codeBackgroundColor,
            codeBlockTypeface, new Parser())
            .markdownToSpans(getString(R.string.display_text));
    textView.setText(text);
}
 
Example 2
Source File: VerticalWeekCalendar.java    From VerticalCalendar with Apache License 2.0 5 votes vote down vote up
@Override
public Typeface getCustomFont() {
    if (customFont == null) {
        return null;
    }
    try {
        return ResourcesCompat.getFont(context, getResources().getIdentifier(customFont.split("\\.")[0],
                "font", context.getPackageName()));
    } catch (Exception e) {
        return null;
    }
}
 
Example 3
Source File: FontsManager.java    From CommonUtils with Apache License 2.0 5 votes vote down vote up
@NonNull
public static Typeface get(@NonNull Context context, @FontRes int res) {
    if (instance == null) instance = new FontsManager();

    Typeface typeface = instance.cache.get(res);
    if (typeface == null) {
        typeface = ResourcesCompat.getFont(context, res);
        if (typeface == null) throw new IllegalArgumentException("Font doesn't exist!");
        instance.cache.put(res, typeface);
    }

    return typeface;
}
 
Example 4
Source File: Clock.java    From Clock-view with Apache License 2.0 5 votes vote down vote up
private void drawMinutesValues(Canvas canvas) {

        if (!showMinutesValues)
            return;

        Rect rect = new Rect();

        Typeface typeface = ResourcesCompat.getFont(getContext(), R.font.proxima_nova_thin);
        TextPaint textPaint = new TextPaint();
        textPaint.setColor(minutesProgressColor);
        textPaint.setTypeface(typeface);
        textPaint.setTextSize(size * MINUTES_TEXT_SIZE);

        int rText = (int) (centerX - ((1 - minutesValuesFactor - (2 * DEFAULT_BORDER_THICKNESS) - MINUTES_TEXT_SIZE) * radius));

        for (int i = 0; i < FULL_ANGLE; i = i + QUARTER_DEGREE_STEPS) {

            int value = i / 6;
            String formatted;
            switch (valueType) {

                case arabic:
                    formatted = ClockUtils.toArabic(value);
                    break;

                case roman:
                    formatted = ClockUtils.toRoman(value);
                    break;

                default:
                    formatted = String.format(Locale.getDefault(), "%02d", value);
                    break;
            }

            int textX = (int) (centerX + rText * Math.cos(Math.toRadians(REGULAR_ANGLE - i)));
            int textY = (int) (centerX - rText * Math.sin(Math.toRadians(REGULAR_ANGLE - i)));
            textPaint.getTextBounds(formatted, 0, formatted.length(), rect);
            canvas.drawText(formatted, textX - rect.width() / formatted.length(), textY + rect.height() / formatted.length(), textPaint);
        }
    }
 
Example 5
Source File: Clock.java    From Clock-view with Apache License 2.0 5 votes vote down vote up
public void setStopwatchTheme(StopwatchTheme stopwatchTheme) {

        this.clockType = stopwatchTheme.getClockType();

        this.clockBackground = ResourcesCompat.getDrawable(getContext().getResources(), stopwatchTheme.getClockBackground(), null);

        this.valuesFont = ResourcesCompat.getFont(getContext(), stopwatchTheme.getValuesFont());
        this.valuesColor = ResourcesCompat.getColor(getContext().getResources(), stopwatchTheme.getValuesColor(), null);

        this.showBorder = stopwatchTheme.isShowBorder();
        this.borderColor = stopwatchTheme.getBorderColor();
        this.borderRadiusRx = stopwatchTheme.getBorderRadiusRx();
        this.borderRadiusRy = stopwatchTheme.getBorderRadiusRy();
    }
 
Example 6
Source File: BindFontTest.java    From butterknife with Apache License 2.0 5 votes vote down vote up
@Test public void typeface() {
  TargetTypeface target = new TargetTypeface();
  Typeface expected = ResourcesCompat.getFont(context, R.font.inconsolata_regular);

  Unbinder unbinder = ButterKnife.bind(target, tree);
  assertThat(target.actual).isSameAs(expected);

  unbinder.unbind();
  assertThat(target.actual).isSameAs(expected);
}
 
Example 7
Source File: PixelWatchFace.java    From PixelWatchFace with GNU General Public License v3.0 4 votes vote down vote up
@Override
    public void onCreate(SurfaceHolder holder) {
      super.onCreate(holder);

      setDefaultSystemComplicationProvider(BATTERY_COMPLICATION_ID, SystemProviders.WATCH_BATTERY,
          ComplicationData.TYPE_RANGED_VALUE);
//      setDefaultComplicationProvider(WEATHER_COMPLICATION_ID,
//          new ComponentName("com.google.android.wearable.app", WEATHER_PROVIDER_SERVICE), ComplicationData.TYPE_ICON);
      //setDefaultSystemComplicationProvider(STEP_COUNT_COMPLICATION_ID, SystemProviders.STEP_COUNT, ComplicationData.TYPE_SHORT_TEXT);


      setWatchFaceStyle(new WatchFaceStyle.Builder(PixelWatchFace.this)
          .setHideNotificationIndicator(true)
          .setShowUnreadCountIndicator(true)
          .setStatusBarGravity(Gravity.CENTER_HORIZONTAL)
          .setStatusBarGravity(Gravity.TOP)
          .build());

      // Initializes syncing with companion app
      Wearable.getDataClient(getApplicationContext()).addListener(this);

      // Initializes background.
      mBackgroundPaint = new Paint();
      mBackgroundPaint.setColor(
          ContextCompat.getColor(getApplicationContext(), R.color.background));
      mProductSans = ResourcesCompat.getFont(getApplicationContext(), R.font.product_sans_regular);
      mProductSansThin = ResourcesCompat.getFont(getApplicationContext(), R.font.product_sans_thin);

      // Initializes Watch Face.
      mTimePaint = new Paint();
      mTimePaint.setTypeface(mProductSans);
      mTimePaint.setAntiAlias(true);
      mTimePaint.setColor(
          ContextCompat.getColor(getApplicationContext(), R.color.digital_text));
      mTimePaint.setStrokeWidth(3f);

      mInfoPaint = new Paint();
      mInfoPaint.setTypeface(mProductSans);
      mInfoPaint.setAntiAlias(true);
      mInfoPaint.setColor(ContextCompat.getColor(getApplicationContext(), R.color.digital_text));
      mInfoPaint.setStrokeWidth(2f);

      if (shouldSuggestSettings()) {
        setSuggestedSettings();
      }
    }
 
Example 8
Source File: Clock.java    From Clock-view with Apache License 2.0 4 votes vote down vote up
private void init(Context context, AttributeSet attrs) {
    mClockRunnable = new ClockRunnable(this);
    mHandler = new Handler();

    TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.Clock, 0, 0);

    try {

        this.clockType = ClockType.fromId(typedArray.getInt(R.styleable.Clock_clock_type, ClockType.analogical.getId()));

        this.clockBackground = typedArray.getDrawable(R.styleable.Clock_clock_background);

        this.showCenter = typedArray.getBoolean(R.styleable.Clock_show_center, DEFAULT_STATE);
        this.centerInnerColor = typedArray.getColor(R.styleable.Clock_center_inner_color, DEFAULT_PRIMARY_COLOR);
        this.centerOuterColor = typedArray.getColor(R.styleable.Clock_center_outer_color, DEFAULT_SECONDARY_COLOR);

        this.showBorder = typedArray.getBoolean(R.styleable.Clock_show_border, DEFAULT_STATE);
        this.borderColor = typedArray.getColor(R.styleable.Clock_border_color, DEFAULT_PRIMARY_COLOR);
        this.borderStyle = BorderStyle.fromId(typedArray.getInt(R.styleable.Clock_border_style, BorderStyle.rectangle.getId()));
        int typedBorderRadiusX = typedArray.getInt(R.styleable.Clock_border_radius_rx, DEFAULT_BORDER_RADIUS);

        if (typedBorderRadiusX > DEFAULT_MIN_RADIUS_ && typedBorderRadiusX < DEFAULT_MAX_RADIUS) {
            this.borderRadiusRx = typedBorderRadiusX;
        } else {
            throw new IllegalArgumentException("border_radius_rx should be in ]" + DEFAULT_MIN_RADIUS_ + "," + DEFAULT_MAX_RADIUS + "[");
        }

        int typedBorderRadiusY = typedArray.getInt(R.styleable.Clock_border_radius_ry, DEFAULT_BORDER_RADIUS);
        if (typedBorderRadiusY > DEFAULT_MIN_RADIUS_ && typedBorderRadiusY < DEFAULT_MAX_RADIUS) {
            this.borderRadiusRy = typedBorderRadiusY;
        } else {
            throw new IllegalArgumentException("border_radius_ry should be in ]" + DEFAULT_MIN_RADIUS_ + "," + DEFAULT_MAX_RADIUS + "[");
        }

        this.showSecondsNeedle = typedArray.getBoolean(R.styleable.Clock_show_seconds_needle, DEFAULT_STATE);
        this.needleHoursColor = typedArray.getColor(R.styleable.Clock_hours_needle_color, DEFAULT_PRIMARY_COLOR);
        this.needleMinutesColor = typedArray.getColor(R.styleable.Clock_minutes_needle_color, DEFAULT_PRIMARY_COLOR);
        this.needleSecondsColor = typedArray.getColor(R.styleable.Clock_seconds_needle_color, DEFAULT_SECONDARY_COLOR);

        this.showProgress = typedArray.getBoolean(R.styleable.Clock_show_progress, DEFAULT_STATE);
        this.progressColor = typedArray.getColor(R.styleable.Clock_progress_color, DEFAULT_SECONDARY_COLOR);

        this.showMinutesProgress = typedArray.getBoolean(R.styleable.Clock_show_minutes_progress, DEFAULT_STATE);
        this.minutesProgressColor = typedArray.getColor(R.styleable.Clock_minutes_progress_color, DEFAULT_PRIMARY_COLOR);
        this.minutesProgressFactor = typedArray.getFloat(R.styleable.Clock_minutes_progress_factor, DEFAULT_MINUTES_BORDER_FACTOR);

        this.showSecondsProgress = typedArray.getBoolean(R.styleable.Clock_show_seconds_progress, DEFAULT_STATE);
        this.secondsProgressFactor = typedArray.getFloat(R.styleable.Clock_seconds_progress_factor, DEFAULT_SECONDS_BORDER_FACTOR);
        this.secondsProgressColor = typedArray.getColor(R.styleable.Clock_seconds_progress_color, DEFAULT_PRIMARY_COLOR);

        this.showDegrees = typedArray.getBoolean(R.styleable.Clock_show_degree, DEFAULT_STATE);
        this.degreesColor = typedArray.getColor(R.styleable.Clock_degree_color, DEFAULT_PRIMARY_COLOR);
        this.degreesType = DegreeType.fromId(typedArray.getInt(R.styleable.Clock_degree_type, DegreeType.line.getId()));
        this.degreesStep = DegreesStep.fromId(typedArray.getInt(R.styleable.Clock_degree_step, DegreesStep.full.getId()));

        this.valuesFont = ResourcesCompat.getFont(getContext(), typedArray.getResourceId(R.styleable.Clock_values_font, R.font.proxima_nova_thin));
        this.valuesColor = typedArray.getColor(R.styleable.Clock_values_color, DEFAULT_PRIMARY_COLOR);
        this.showHoursValues = typedArray.getBoolean(R.styleable.Clock_show_hours_values, DEFAULT_STATE);
        this.showMinutesValues = typedArray.getBoolean(R.styleable.Clock_show_minutes_value, DEFAULT_STATE);
        this.minutesValuesFactor = typedArray.getFloat(R.styleable.Clock_minutes_values_factor, DEFAULT_MINUTES_BORDER_FACTOR);
        this.valueStep = ValueStep.fromId(typedArray.getInt(R.styleable.Clock_clock_value_step, ValueStep.full.getId()));
        this.valueType = ValueType.fromId(typedArray.getInt(R.styleable.Clock_clock_value_type, ValueType.none.getId()));
        this.valueDisposition = ValueDisposition.fromId(typedArray.getInt(R.styleable.Clock_clock_value_disposition, ValueDisposition.regular.getId()));

        this.numericFormat = NumericFormat.fromId(typedArray.getInt(R.styleable.Clock_numeric_format, NumericFormat.hour_12.getId()));

        this.numericShowSeconds = typedArray.getBoolean(R.styleable.Clock_numeric_show_seconds, DEFAULT_STATE);

        typedArray.recycle();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
Example 9
Source File: Clock.java    From Clock-view with Apache License 2.0 4 votes vote down vote up
public void setValuesFont(int valuesFont) {
    this.valuesFont = ResourcesCompat.getFont(getContext(), valuesFont);
}
 
Example 10
Source File: Clock.java    From Clock-view with Apache License 2.0 4 votes vote down vote up
public void setAnalogicalTheme(AnalogicalTheme analogicalTheme) {

        this.clockType = analogicalTheme.getClockType();

        this.clockBackground = ResourcesCompat.getDrawable(getContext().getResources(), analogicalTheme.getClockBackground(), null);

        this.showCenter = analogicalTheme.isShowCenter();
        this.centerInnerColor = analogicalTheme.getCenterInnerColor();
        this.centerOuterColor = analogicalTheme.getCenterOuterColor();

        this.showBorder = analogicalTheme.isShowBorder();
        this.borderColor = analogicalTheme.getBorderColor();

        this.showSecondsNeedle = analogicalTheme.isShowSecondsNeedle();
        this.needleHoursColor = analogicalTheme.getNeedleHoursColor();
        this.needleMinutesColor = analogicalTheme.getNeedleMinutesColor();
        this.needleSecondsColor = analogicalTheme.getNeedleSecondsColor();

        this.showProgress = analogicalTheme.isShowProgress();
        this.progressColor = analogicalTheme.getProgressColor();
        this.showMinutesProgress = analogicalTheme.isShowMinutesProgress();
        this.minutesProgressColor = analogicalTheme.getMinutesProgressColor();
        this.minutesProgressFactor = analogicalTheme.getMinutesProgressFactor();
        this.showSecondsProgress = analogicalTheme.isShowSecondsProgress();
        this.secondsProgressColor = analogicalTheme.getSecondsProgressColor();
        this.secondsProgressFactor = analogicalTheme.getSecondsProgressFactor();

        this.showDegrees = analogicalTheme.isShowDegrees();
        this.degreesColor = ResourcesCompat.getColor(getContext().getResources(), analogicalTheme.getDegreesColor(), null);
        this.degreesType = analogicalTheme.getDegreesType();
        this.degreesStep = analogicalTheme.getDegreesStep();

        this.valuesFont = ResourcesCompat.getFont(getContext(), analogicalTheme.getValuesFont());
        this.valuesColor = ResourcesCompat.getColor(getContext().getResources(), analogicalTheme.getValuesColor(), null);
        this.showHoursValues = analogicalTheme.isShowHoursValues();
        this.showMinutesValues = analogicalTheme.isShowMinutesValues();
        this.minutesValuesFactor = analogicalTheme.getMinutesValuesFactor();
        this.valueStep = analogicalTheme.getValueStep();
        this.valueType = analogicalTheme.getValueType();
        this.valueDisposition = analogicalTheme.getValueDisposition();
    }
 
Example 11
Source File: Clock.java    From Clock-view with Apache License 2.0 4 votes vote down vote up
public void setNumericTheme(NumericTheme numericTheme) {

        this.clockType = numericTheme.getClockType();

        this.clockBackground = ResourcesCompat.getDrawable(getContext().getResources(), numericTheme.getClockBackground(), null);

        this.valuesFont = ResourcesCompat.getFont(getContext(), numericTheme.getValuesFont());
        this.valuesColor = ResourcesCompat.getColor(getContext().getResources(), numericTheme.getValuesColor(), null);

        this.showBorder = numericTheme.isShowBorder();
        this.borderColor = numericTheme.getBorderColor();
        this.borderRadiusRx = numericTheme.getBorderRadiusRx();
        this.borderRadiusRy = numericTheme.getBorderRadiusRy();

        this.numericFormat = numericTheme.getNumericFormat();
    }
 
Example 12
Source File: StepView.java    From StepView with Apache License 2.0 4 votes vote down vote up
private void applyStyles(Context context, AttributeSet attrs, int defStyleAttr) {
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.StepView, defStyleAttr, R.style.StepView);
    selectedCircleColor = ta.getColor(R.styleable.StepView_sv_selectedCircleColor, 0);
    selectedCircleRadius = ta.getDimensionPixelSize(R.styleable.StepView_sv_selectedCircleRadius, 0);
    selectedTextColor = ta.getColor(R.styleable.StepView_sv_selectedTextColor, 0);
    selectedStepNumberColor = ta.getColor(R.styleable.StepView_sv_selectedStepNumberColor, 0);
    doneStepMarkColor = ta.getColor(R.styleable.StepView_sv_doneStepMarkColor, 0);
    doneCircleColor = ta.getColor(R.styleable.StepView_sv_doneCircleColor, 0);
    doneCircleRadius = ta.getDimensionPixelSize(R.styleable.StepView_sv_doneCircleRadius, 0);
    doneTextColor = ta.getColor(R.styleable.StepView_sv_doneTextColor, 0);
    nextTextColor = ta.getColor(R.styleable.StepView_sv_nextTextColor, 0);
    stepPadding = ta.getDimensionPixelSize(R.styleable.StepView_sv_stepPadding, 0);
    nextStepLineColor = ta.getColor(R.styleable.StepView_sv_nextStepLineColor, 0);
    doneStepLineColor = ta.getColor(R.styleable.StepView_sv_doneStepLineColor, 0);
    stepLineWidth = ta.getDimensionPixelSize(R.styleable.StepView_sv_stepLineWidth, 0);
    textPadding = ta.getDimensionPixelSize(R.styleable.StepView_sv_textPadding, 0);
    stepNumberTextSize = ta.getDimension(R.styleable.StepView_sv_stepNumberTextSize, 0);
    textSize = ta.getDimension(R.styleable.StepView_sv_textSize, 0);
    animationDuration = ta.getInteger(R.styleable.StepView_sv_animationDuration, 0);
    animationType = ta.getInteger(R.styleable.StepView_sv_animationType, 0);
    stepsNumber = ta.getInteger(R.styleable.StepView_sv_stepsNumber, 0);
    nextStepCircleEnabled = ta.getBoolean(R.styleable.StepView_sv_nextStepCircleEnabled, false);
    nextStepCircleColor = ta.getColor(R.styleable.StepView_sv_nextStepCircleColor, 0);
    CharSequence[] descriptions = ta.getTextArray(R.styleable.StepView_sv_steps);
    if (descriptions != null) {
        for (CharSequence description : descriptions) {
            steps.add(description.toString());
        }
        displayMode = DISPLAY_MODE_WITH_TEXT;
    } else {
        displayMode = DISPLAY_MODE_NO_TEXT;
    }
    Drawable background = ta.getDrawable(R.styleable.StepView_sv_background);
    if (background != null) {
        setBackgroundDrawable(background);
    }
    int fontId = ta.getResourceId(R.styleable.StepView_sv_typeface, 0);
    if (fontId != 0) {
        Typeface typeface = ResourcesCompat.getFont(context, fontId);
        setTypeface(typeface);
    }
    textPaint.setTextSize(textSize);
    ta.recycle();
}
 
Example 13
Source File: Clock.java    From Clock-view with Apache License 2.0 3 votes vote down vote up
public void setTimeCounterTheme(TimeCounterTheme timeCounterTheme) {

        this.clockType = timeCounterTheme.getClockType();

        this.clockBackground = ResourcesCompat.getDrawable(getContext().getResources(), timeCounterTheme.getClockBackground(), null);

        this.valuesFont = ResourcesCompat.getFont(getContext(), timeCounterTheme.getValuesFont());
        this.valuesColor = ResourcesCompat.getColor(getContext().getResources(), timeCounterTheme.getValuesColor(), null);

        this.showProgress = timeCounterTheme.isShowProgress();
        this.progressColor = timeCounterTheme.getProgressColor();
        this.borderColor = timeCounterTheme.getBorderColor();
    }