Skip to content
Permalink
Browse files

Terrible workaround applied.

  • Loading branch information...
GyulaJuhasz committed May 20, 2017
1 parent d215429 commit 3a647bb916295a6530935fe4f1bbd2736f1ced51
@@ -5,12 +5,15 @@
import android.graphics.Typeface;
import android.support.v7.widget.SwitchCompat;
import android.util.AttributeSet;
import android.util.Log;

/**
* Switch that supports using custom fonts.
*/
public class CustomSwitch extends SwitchCompat {

private static final String TAG = "CUSTOMSWITCH";

public CustomSwitch(Context context) {
this(context, null);
}
@@ -35,6 +38,12 @@ public CustomSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
@Override
public void setTypeface(Typeface typeface) {
super.setTypeface(typeface);
setSwitchTypeface(typeface);
try {
setSwitchTypeface(typeface);
Log.v(TAG, "OK: " + toString(), new Exception("Just for stacktrace"));
} catch (final NullPointerException npe) {
// Unfortunately, this cannot be prevented because TextView's constructor calls setTypeface :(
Log.w(TAG, "NOT OK: " + toString(), npe);
}
}
}
@@ -6,18 +6,35 @@
android:layout_height="match_parent">

<com.gyulajuhasz.example.overridefail.CustomSwitch
android:id="@+id/switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/switch_text"
android:textOff="@string/off"
android:textOn="@string/on"
android:thumb="@drawable/custom_switch_thumb"
app:fontAssetPath="font.ttf"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/switch_2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:showText="true"
app:switchPadding="20dp" />

<com.gyulajuhasz.example.overridefail.CustomSwitch
android:id="@+id/switch_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/switch_text"
android:textOff="@string/off"
android:textOn="@string/on"
android:thumb="@drawable/custom_switch_thumb"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/switch_1"
app:showText="true"
app:switchPadding="20dp" />


</android.support.constraint.ConstraintLayout>

0 comments on commit 3a647bb

Please sign in to comment.
You can’t perform that action at this time.