FontFamily doesn't apply after upgrading support library - android

I have a TextView:
<TextView
android:id="#+id/digits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:translationY="5dp"
android:text=""
android:textSize="128sp"
android:textColor="#color/white"
android:fontFamily="fonts/Roboto-Thin.ttf"
/>
In my gradle file I used to have
dependencies {
compile 'com.android.support:support-v4:19.1.0'
}
Everything was great, TextView had proper font and typeface.
But, when I changed support library version (so I can use new SwipeToRefreshLayout) to:
dependencies {
compile 'com.android.support:support-v4:21.0.2'
}
Then my TextView is not applying Robot Thin font, text is bolded.
How come does it work this way? I think problem is only with Roboto-Thin, because other TextViews that have Roboto-Regular are working properly. Any ideas guys?

Create a Custom TextView class
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomTextView(Context context) {
super(context);
init();
}
public void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Thin.ttf");
setTypeface(tf, 1);
}
}
public void setNewText(CharSequence text) {
// code to check text for null omitted
super.setText(text, BufferType.SPANNABLE);
}
}
After this, change your
<TextView
to
<CustomTextView
Is the better way-out to this error

Related

AppCompatEditText Custom Typeface doesn't work properly

I am trying to create a new and Custom AppCompatEditText while extending from AppCompatEditText, when I'm changing the typeface in my class and use that class in my XML while making the view, the font will remain the same as default, can anyone please tell me what is the problem here?
public class MAppEditText extends AppCompatEditText {
public MAppEditText(#NonNull #NotNull Context context) {
super(context);
init();
}
public MAppEditText(#NonNull #NotNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public MAppEditText(#NonNull #NotNull Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
Typeface tf;
if (getTypeface().isBold()) {
tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/yekan_bold.ttf");
} else {
tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/yekan_regular.ttf");
}
setTypeface(tf);
}
}
I also made a custom AppCompatTextView with this method and it works just fine, but with AppCompatEditText is different.
Are you sure you used the same object in the xml too?
<MAppEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"/>

How to add svg icon to TextView with default text font?

here is my custom text view:
public class CustomTextView extends TextView {
public CustomTextView (Context context) {
super(context);
}
public CustomTextView (Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public CustomTextView (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
setCustomFont(ctx);
}
public boolean setCustomFont(Context ctx) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), "icons/svg_icons.ttf");
} catch (Exception e) {
return false;
}
setTypeface(tf);
return true;
}
}
my icon code is :
<string name="svg_warning" translatable="false"></string>
I'm adding svg icon to TextView with text by this:
mytextview.setText(String.format("Please Select a Country %s",getResources().getString(R.string.svg_warning)));
But font of "Please Select a Country" is not the same like default Android font. How to add svg icon to TextView with default Android's text font?
Result:
holy shit i never knew textviews supported svg like this!
As I can see from your code your customTextView will have the Typeface you just set in the setCustomFont method. What you can do is have 2 textviews, one your customTextView for the svg and left to it enclosed in a horizontal LinearLayout a normal TextView. You can't have one font to some text in a textview and another font for the rest of the text, in web can you have 2 different fonts for a p tag?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

create custom textview and set drawable to right of it

Hi I create custom textview which we have set font to it
this is my custom textview class
public class mTextView extends TextView {
private int CircleColor=0;
// Default constructor when inflating from XML file
public mTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init(attrs);
}
private void init(AttributeSet attrs) {
//setIncludeFontPadding(false);
if(attrs!=null){
TypedArray a=getContext().obtainStyledAttributes(attrs, R.styleable.mTextView);
String font=a.getString(R.styleable.mTextView_mt_font);
if(font!=null) {
Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), font);
setTypeface(typeface);
}
}
}
// Default constructor override
public mTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
init(attrs);
setGravity(getGravity() | Gravity.CENTER_VERTICAL); //make sure that the gravity is set to the top
}
}
and it works fine but when I set drawable to the right of textview my textview do not show my drawable
<tools.mTextView
android:textColor="#color/md_white_1000"
app:mt_font="fonts/irterafik.ttf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="20dp"
android:drawableRight="#drawable/registerdata"
android:gravity="right"
android:text="registerdata"
android:textAppearance="?android:attr/textAppearanceMedium" />
so can anyone help about this?
It seems It's because you have the gravity set to right. Try to remove it and see if the problem solved

Change color Text from CustomTextView

By default to change textColor programatically is :
textView.setTextColor(Color.RED);
I need to have a custom Textview to change typeface and color by default, How can change textcolor from CustomTextView class, here is my code.
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextView(Context context) {
super(context);
}
public void setTypeface(Typeface tf, int style) {
if(!isInEditMode()) {
if (style == Typeface.BOLD) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Bold.ttf"));
} else if(style == Typeface.ITALIC){ // constant used to set Lato-Light.
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Light.ttf"));
}else {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Regular.ttf"));
}
}
}
The below code is the way to set your default text color and typeface.
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
init(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Lato-Light.ttf"));
setTextColor(Color.RED);
}
}
The init() method gets called every time the text view gets created, and will then set the typeface and color in that. You can manipulate any other variables you want to in there.
Use setTextColor(Color.RED); after each super in each constructor.
Step 1
In the /assets directory (not the /resource directory), create a folder called /fonts. Copy your custom font here. You can use both TTF and OTF fonts.
Step 2
In the /res/values folder, create a new file called attrs.xml. This is how the Android SDK lets you name custom properties for your widgets.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTextView">
<attr name="fontName" format="string" />
</declare-styleable>
</resources>
Step 3
In /res/layouts, you will need to include your to-be-created custom text view in the activity_main.xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:customfontdemo="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:padding="12dp"
android:text="Standard Android Font" />
<com.authorwjf.customfontdemo.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:padding="12dp"
customfontdemo:fontName="pipe_dream.ttf"
android:text="Custom Android Font" />
</LinearLayout>
Step 4
In the /src folder, you will want to create your MyTextView class. It extends the standard text view, plucks the font name from the custom attribute, and applies the type face.
package com.authorwjf.customfontdemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public MyTextView(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
setTextColor(Color.RED);
if (attrs!=null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyTextView);
String fontName = a.getString(R.styleable.MyTextView_fontName);
if (fontName!=null) {
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontName);
setTypeface(myTypeface);
}
a.recycle();
}
}
}
Step 5
Because the text view is now self-contained, you aren't required to make any modifications to our /src/MainAcitivity.java file.
package com.authorwjf.customfontdemo;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Custom Fonts and Custom Textview on Android

From an application I need to develop, I've received a specific font that has many files like FontName-Regular, FontName-Bold, FontName-it. I need to use it in all the textviews in the application. First I thought it was an easy task. Look over SO and found a very nice thread:here
So first I did like:
public static void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof TextView) {
((TextView)v).setTypeface(FONT_REGULAR);
}
} catch (Exception e) {
e.printStackTrace();
// ignore
}
}
And called this method during onCreate in my activity. Every textView in my app was showing that font and boy, was I happy for getting away so easy. Until I got to a screen where some textviews required Bold as Style (android:textStyle="bold"). Then I realized that this solution does not provide me with possibility to load the Font-Bold.ttf from assets.
Than looked further and saw a nice custom TextView implementation, in the same SO question:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
setTypeface(tf ,1);
}
}
This looks even better. My question is: how can I detect on init() if my control has Style set to Bold or not so I can assign the requested TypeFace ?
Thank you for your time.
LE. Following the example below, I've updated my class as:
public class MyTextView extends TextView {
Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_REGULAR);
Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_BOLD);
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context) {
super(context);
}
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.BOLD) {
super.setTypeface(boldTypeface/*, -1*/);
} else {
super.setTypeface(normalTypeface/*, -1*/);
}
}
}
Well If I debug, the app goes in setTypeFace and it seems to apply the bold one, but on my layout I can't see any change, not bold. No matter what font I use, no changes are done in my TextView and is displayed with the default android font. I wonder why ?
I have summed everything on a blog post here on my blog maybe it will help someone.
The constructor of TextView calls setTypeface(Typeface tf, int style) with the style parameter retrieved from the XML attribute android:textStyle. So, if you want to intercept this call to force your own typeface you can override this method as follow:
public void setTypeface(Typeface tf, int style) {
Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/your_normal_font.ttf");
Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/your_bold_font.ttf");
if (style == Typeface.BOLD) {
super.setTypeface(boldTypeface/*, -1*/);
} else {
super.setTypeface(normalTypeface/*, -1*/);
}
}
You can use my CustomTextView which allows you to specify a font file name in your assets folder:
https://github.com/mafshin/CustomTextView
and the usage is really simple:
<com.my.app.CustomTextView
xmlns:custom="http://schemas.android.com/apk/res/com.my.app"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test text"
android:id="#+id/testcustomview"
custom:fontAssetName="Politica XT.otf"
/>
I think it's better to create your own package for custom fonts and import them in your project so that you can use them later in future
package com.codeslips.utilities;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomTextView extends TextView {
public CustomTextView(Context context)
{ super(context); setFont(); }
public CustomTextView(Context context,AttributeSet set)
{ super(context,set); setFont(); }
public CustomTextView(Context context,AttributeSet set,int defaultStyle)
{ super(context,set,defaultStyle); setFont(); }
private void setFont() {
Typeface typeface=Typeface.createFromAsset(getContext().getAssets(),"fonts/your-font.ttf");
setTypeface(typeface); //function used to set font
}
}
Now use the above class in your XML file to have your custom font
<com.codeslips.utilities.CustomTextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Upload Image"
android:paddingTop="10sp"
android:textSize="14sp"
android:layout_weight="0.7"
android:textColor="#android:color/white"/>

Categories

Resources