Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Allow setting font for progress text #22

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Add view to your layout:
app:startAngle="270"
app:textColor="#fffc59"
app:textSize="14sp"
app:textFont="@font/open_sans"
app:fillBackground="false"
app:gradientType="linear"
app:gradientEndColor="@color/colorAccent" />
Expand Down Expand Up @@ -105,6 +106,7 @@ circularProgress.getMaxProgress() // returns 10000
| Dot width | `app:dotWidth` | setters: `setDotWidthDp(widthInDp)` or `setDotWidthPx(widthInPx)`<br/>getter: `getDotWidth()` (returns width in pixels) | same as progress stroke width |
| Progress text size | `app:textSize` | setters: `setTextSizeSp(sizeInSp)` or `setTextSizePx(sizeInPx)`<br/>getter: `getTextSize()` (returns size in pixels) | `24sp` |
| Progress text color | `app:textColor` | setter: `setTextColor(textColor)`<br/>getter: `getTextColor()` | same as progress color |
| Progress text font | `app:textFont` | setter: `setTextFont(typeface)`<br/>getter: `getTextFont()` | System default font (Roboto) |
| Formatting pattern to be used in `PatternProgressTextAdapter`. Checkout [Formatting progress text](#formatting-progress-text) section. | `app:formattingPattern` | setter: `setProgressTextAdapter(progressTextAdapter)`<br/>getter: `getProgressTextAdapter()` | not specified |
| Direction of the progress arc (`clockwise` or `counterclockwise`) | `app:direction` | setter: `setDirection(direction)`<br/>getter: `getDirection()` | `counterclockwise` |
| Start angle. Checkout [Start angle](#setting-start-angle) section. | `app:startAngle` | setter: `setStartAngle(startAngle)`<br/>getter: `getStartAngle()` | `270` |
Expand Down
1 change: 1 addition & 0 deletions circularprogressindicator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:support-annotations:27.1.1'
implementation 'com.android.support:support-compat:27.1.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
Expand All @@ -17,13 +18,15 @@
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.Typeface;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.Dimension;
import android.support.annotation.IntDef;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.res.ResourcesCompat;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
Expand Down Expand Up @@ -83,6 +86,18 @@ public class CircularProgressIndicator extends View {
private float textX;
private float textY;

private ResourcesCompat.FontCallback fontCallback = new ResourcesCompat.FontCallback() {
@Override
public void onFontRetrieved(@NonNull Typeface typeface) {
setTextFont(typeface);
}

@Override
public void onFontRetrievalFailed(int reason) {

}
};

private float radius;

private boolean shouldDrawDot;
Expand Down Expand Up @@ -154,6 +169,17 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
textColor = a.getColor(R.styleable.CircularProgressIndicator_textColor, progressColor);
textSize = a.getDimensionPixelSize(R.styleable.CircularProgressIndicator_textSize, textSize);

String fontPath = a.getString(R.styleable.CircularProgressIndicator_textFont);
if (fontPath != null && !fontPath.isEmpty()) {
try {
int fontResourceId = a.getResourceId(R.styleable.CircularProgressIndicator_textFont, 0);
if(fontResourceId != 0) {
ResourcesCompat.getFont(getContext(), fontResourceId, fontCallback, null);
}
} catch (Resources.NotFoundException ignored) {
}
}

shouldDrawDot = a.getBoolean(R.styleable.CircularProgressIndicator_drawDot, true);
dotColor = a.getColor(R.styleable.CircularProgressIndicator_dotColor, progressColor);
dotWidth = a.getDimensionPixelSize(R.styleable.CircularProgressIndicator_dotWidth, progressStrokeWidth);
Expand Down Expand Up @@ -507,8 +533,7 @@ public void setProgressBackgroundStrokeWidthPx(@Dimension int strokeWidth) {
public void setTextColor(@ColorInt int color) {
textPaint.setColor(color);

Rect textRect = new Rect();
textPaint.getTextBounds(progressText, 0, progressText.length(), textRect);
Rect textRect = calculateTextBounds();

invalidate(textRect);
}
Expand All @@ -535,6 +560,14 @@ public void setTextSizePx(@Dimension int size) {
invalidate(textBounds);
}

public void setTextFont(Typeface font) {
textPaint.setTypeface(font);

Rect textRect = calculateTextBounds();

invalidate(textRect);
}

public void setShouldDrawDot(boolean shouldDrawDot) {
this.shouldDrawDot = shouldDrawDot;

Expand Down Expand Up @@ -607,6 +640,10 @@ public float getTextSize() {
return textPaint.getTextSize();
}

public Typeface getTextFont() {
return textPaint.getTypeface();
}


public boolean isDotEnabled() {
return shouldDrawDot;
Expand Down
1 change: 1 addition & 0 deletions circularprogressindicator/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
<attr name="textFont" format="string" />

<attr name="formattingPattern" format="string|reference" />

Expand Down
3 changes: 3 additions & 0 deletions example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package antonkozyriatskyi.circularprogressindicatorexample;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
Expand All @@ -17,6 +21,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import antonkozyriatskyi.circularprogressindicator.CircularProgressIndicator;

Expand Down Expand Up @@ -156,6 +161,44 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
public void onNothingSelected(AdapterView<?> parent) {
}
});

Spinner spinner = findViewById(R.id.sb_text_font);

final Context context = this;
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
circularProgress.setTextFont(Typeface.DEFAULT);
break;
case 1:
circularProgress.setTextFont(ResourcesCompat.getFont(context, R.font.architects_daughter));
break;
case 2:
circularProgress.setTextFont(ResourcesCompat.getFont(context, R.font.open_sans_light));
break;
case 3:
circularProgress.setTextFont(ResourcesCompat.getFont(context, R.font.vt323));
break;
}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

// Spinner Drop down elements
List<String> fonts = new ArrayList<String>();
fonts.add("Default");
fonts.add("Architects Daughter");
fonts.add("Open Sans Light");
fonts.add("VT323");

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, fonts);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}

@Override
Expand Down
8 changes: 8 additions & 0 deletions example/src/main/res/font/architects_daughter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
app:fontProviderAuthority="com.google.android.gms.fonts"
app:fontProviderPackage="com.google.android.gms"
app:fontProviderQuery="Architects Daughter"
app:fontProviderCerts="@array/com_google_android_gms_fonts_certs"
app:fontProviderFetchStrategy="blocking">
</font-family>
8 changes: 8 additions & 0 deletions example/src/main/res/font/open_sans_light.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
app:fontProviderAuthority="com.google.android.gms.fonts"
app:fontProviderPackage="com.google.android.gms"
app:fontProviderQuery="name=Open Sans&amp;weight=300"
app:fontProviderCerts="@array/com_google_android_gms_fonts_certs"
app:fontProviderFetchStrategy="blocking">
</font-family>
8 changes: 8 additions & 0 deletions example/src/main/res/font/vt323.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
app:fontProviderAuthority="com.google.android.gms.fonts"
app:fontProviderPackage="com.google.android.gms"
app:fontProviderQuery="VT323"
app:fontProviderCerts="@array/com_google_android_gms_fonts_certs"
app:fontProviderFetchStrategy="blocking">
</font-family>
11 changes: 11 additions & 0 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@
android:min="8"
android:progress="24" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Text font" />


<Spinner
android:id="@+id/sb_text_font"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_width="wrap_content"
Expand Down
17 changes: 17 additions & 0 deletions example/src/main/res/values/font_certs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="com_google_android_gms_fonts_certs">
<item>@array/com_google_android_gms_fonts_certs_dev</item>
<item>@array/com_google_android_gms_fonts_certs_prod</item>
</array>
<string-array name="com_google_android_gms_fonts_certs_dev">
<item>
MIIEqDCCA5CgAwIBAgIJANWFuGx90071MA0GCSqGSIb3DQEBBAUAMIGUMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEQMA4GA1UEChMHQW5kcm9pZDEQMA4GA1UECxMHQW5kcm9pZDEQMA4GA1UEAxMHQW5kcm9pZDEiMCAGCSqGSIb3DQEJARYTYW5kcm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YK6NeADwbSxDdJEqO4k//0zOHKrUiGYXtqw/A0LFFtqoZKFjnkCAQOjgdkwgdYwHQYDVR0OBBYEFMd9jMIhF1Ylmn/Tgt9r45jk14alMIGmBgNVHSMEgZ4wgZuAFMd9jMIhF1Ylmn/Tgt9r45jk14aloXikdjB0MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEUMBIGA1UEChMLR29vZ2xlIEluYy4xEDAOBgNVBAsTB0FuZHJvaWQxEDAOBgNVBAMTB0FuZHJvaWSCCQDC4IdGZEowjTAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBAUAA4IBAQBt0lLO74UwLDYKqs6Tm8/yzKkEu116FmH4rkaymUIE0P9KaMftGlMexFlaYjzmB2OxZyl6euNXEsQH8gjwyxCUKRJNexBiGcCEyj6z+a1fuHHvkiaai+KL8W1EyNmgjmyy8AW7P+LLlkR+ho5zEHatRbM/YAnqGcFh5iZBqpknHf1SKMXFh4dd239FJ1jWYfbMDMy3NS5CTMQ2XFI1MvcyUTdZPErjQfTbQe3aDQsQcafEQPD+nqActifKZ0Np0IS9L9kR/wbNvyz6ENwPiTrjV2KRkEjH78ZMcUQXg0L3BYHJ3lc69Vs5Ddf9uUGGMYldX3WfMBEmh/9iFBDAaTCK
</item>
</string-array>
</resources>
8 changes: 8 additions & 0 deletions example/src/main/res/values/preloaded_fonts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="preloaded_fonts" translatable="false">
<item>@font/architects_daughter</item>
<item>@font/open_sans_light</item>
<item>@font/vt323</item>
</array>
</resources>