Error in setting custom font - Android - android

For an Android widget I've been developing, I'm trying to use a custom font. I've looked up some tutorials on the internet, and I found several ways to do it. I chose one and tried to implement it, but I get an error and I can't figure out why. The technique uses a seperate class (of which I've posted the code below), to set the font. In this code I get an error at the customFont line in setCustomFont. It says that customFont cannot be resolved to a variable. Can anybody help me out as to why this is happening?
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class TypefacedTextView extends TextView {
private static final String TAG = "TextView";
public TypefacedTextView(Context context) {
super(context);
}
public TypefacedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public TypefacedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView);
String customFontl = a.getString(R.styleable.TypefacedTextView_typeface);
setCustomFont(ctx, customFont); // get an error here
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: " + e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}

String customFontl = a.getString(R.styleable.TypefacedTextView_typeface);
setCustomFont(ctx, customFont); // get an error here
Your variable is declared as customFontl (note final l), and you are trying to use it as customFont. I would guess the error is that customFont is not defined or something alike, as it is the case.

i have try this may it help you in this code my font is in asset/fonts folder
if it is only in asseet than replace in code fonts/
public class MyTextView extends TextView{
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
rotate();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
rotate();
}
public MyTextView(Context context) {
super(context);
init();
rotate();
}
private void rotate() {
// TODO Auto-generated method stub
setSelected(true);
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/TrajanPro-Regular.otf");
setTypeface(tf);
}
}
}

Related

Font awesome text is not solid in Android

I am trying to show hand pointer icon which is https://fontawesome.com/icons/hand-pointer?style=regular I want to show it solidly(white background) like this one https://fontawesome.com/icons/hand-pointer?style=solid .
But here I have a problem that both of them has the same unicode which is f25a.
//This is the FontAwesomeTextView I Created
FontAwesomeTextView fingerPointer = new FontAwesomeTextView(MyApplication.getStaticContext());
fingerPointer.setText(MyApplication.getStaticContext().getString(R.string.finger_pointer));
fingerPointer.setTextColor(ContextCompat.getColor(MyApplication.getStaticContext(), R.color.colorWhite));
fingerPointer.setShadowLayer(2,2,2,R.color.colorOrange);
fingerPointer.setTextSize(30f);
fingerPointer.setVisibility(View.GONE);
this.fingerPointer = fingerPointer;
//This is the hand pointer text that I am using
<string name="finger_pointer" translatable="false"></string>
As you can see icon i send in link has the same unicode in string.xml.
This is my FontAwesomeTextView which is pretty simple class that loads fontawesome typeface and set it.
public class FontAwesomeTextView extends AppCompatTextView {
public FontAwesomeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public FontAwesomeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FontAwesomeTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface typeface = FontCache.get(FontCache.FONT_AWESOME,getContext());
setTypeface(typeface);
}
}

Edittext cursor not visible in android

I am using custom Edittext like this
public class DosisEdittextFont extends TextView {
public DosisEdittextFont(Context context) {
super(context);
init(context);
}
public DosisEdittextFont(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public DosisEdittextFont(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
Typeface t = Typeface.createFromAsset(context.getAssets(),
"fonts/Dosis-Regular.otf");
this.setTypeface(t);
}
}
But because of that cursor is not showing in EditText. how to set cursor in custom EditText?
You may extends EditText instead of TextView
Try This
public class CustomEditText extends EditText {
public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if(!isInEditMode())
init(attrs);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
if(!isInEditMode())
init(attrs);
}
public CustomEditText(Context context) {
super(context);
if(!isInEditMode())
init(null);
}
private void init(AttributeSet attrs)
{
if (attrs!=null)
{
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView);
String fontName = a.getString(R.styleable.CustomTextView_fontFamily);
if (fontName!=null)
{
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontName);
setTypeface(myTypeface);
}
a.recycle();
}
}
}
To Use In Your Layout
<CustomEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/etEmailId"
android:hint="#string/email"
android:textColor="#color/input_text_color"
android:textColorHint="#color/input_text_color"
customfont:fontFamily="YOUR FONT NAME.ttf"
/>
Reason for not appearance of cursor in edittext are many like your theme appearance
i.e. theme style or edittext background and one is that you extending TextView,
so try extending EditText in place of TextView as:
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;
public class CustomEditText extends EditText {
private Context context;
private AttributeSet attrs;
private int defStyle;
public CustomEditText(Context context) {
super(context);
this.context = context;
init();
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
this.attrs = attrs;
init();
}
public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
this.attrs = attrs;
this.defStyle = defStyle;
init();
}
private void init() {
Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/montserrat_bold.ttf");
this.setTypeface(font);
}
#Override
public void setTypeface(Typeface tf, int style) {
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/montserrat_bold.ttf");
super.setTypeface(tf, style);
}
#Override
public void setTypeface(Typeface tf) {
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/montserrat_bold.ttf");
super.setTypeface(tf);
}
}
and in you layout xml
<com.example.CustomEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/edittext"
android:hint="text"
android:textColor="#000000"
android:textColorHint="#000000"/>
So, try replacing textview with edittext , if still not resolve, this
occurs due to your activity theme or your edittext background

Stroke in textview text android?

I need to style the text for textview as per the below image
The font being used is CARTER ONE font
This is my textview class
public class CustomTextViewCarter extends TextView {
private static Typeface mTypeface;
public CustomTextViewCarter(final Context context) {
this(context, null);
}
public CustomTextViewCarter(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextViewCarter(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
if (!isInEditMode()) {
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getResources().getAssets(), "carter_one.ttf");
}
setTypeface(mTypeface);
}
}
}
But the the same kind of text doesn't appear. How can it be done?
Use this code for show custom TextView
Step-1:
CustomTextView.java
public class CustomTextView extends TextView {
private static final String TAG = "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 defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs,R.styleable.CustomTV);
String customFont = a.getString(R.styleable.CustomTV_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
Step-2:
After that write below code in your styles.xml file
<declare-styleable name="CustomTV">
<attr name="customFont" format="string"></attr>
</declare-styleable>
Step-3:
Use CustomTextView in your xml file
Here com.app.demo is my package name.. use your package name instead of that,
<com.app.demo.CustomTextView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello"
android:textColor="#color/text"
android:textStyle="bold"
app:customFont="carter_one.ttf" />
Try this.
You can set ttf by making your own textview implementation.
It will works above API 15 (Ice cream sandwich).
https://stackoverflow.com/a/5185587/850347
<com.lht.ui.MyTextView
android:text="Hello friends"
lht:ttf_name="ITCBLKAD.TTF"
/>

Android expandable list view items font style

I have followed the tutorial given in below link:
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
How can I give custom font(typeface) to textviews in list_group.xml and list_item.xml.
I have copied Arial.ttf file into assets folder.
Please help me.
You can create a custom textview as I have done here by overriding the TextView Class and then instead of using TextView in your xml you use CustomTextView:
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CustomTextView(Context context) {
super(context);
}
private void init(Context context, AttributeSet attrs) {
}
#Override
public void setTypeface(Typeface tf, int style) {
if (!isInEditMode()) {
Typeface normalTypeFace = Typeface.createFromAsset(getContext()
.getAssets(), "Esans.ttf");
Typeface boldTypeFace = Typeface.createFromAsset(getContext()
.getAssets(), "EsansBol.ttf");
Typeface lightTypeFace = Typeface.createFromAsset(getContext()
.getAssets(), "EsansLig.ttf");
Typeface mediumTypeFace = Typeface.createFromAsset(getContext()
.getAssets(), "EsansMed.ttf");
//Modifying styles to support the different styles of custom font
switch (style) {
case Typeface.NORMAL:
super.setTypeface(normalTypeFace);
break;
case Typeface.BOLD:
super.setTypeface(boldTypeFace);
break;
case Typeface.ITALIC:
super.setTypeface(lightTypeFace);
break;
case Typeface.BOLD_ITALIC:
super.setTypeface(mediumTypeFace);
break;
default:
super.setTypeface(normalTypeFace);
break;
}
}
}
}
You can try this to add typeface in xml
public class TextViewPlus extends TextView {
private static final String TAG = "TextView";
public TextViewPlus(Context context) {
super(context);
}
public TextViewPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: "+e.getMessage());
return false;
}
setTypeface(tf);
return true;
}
}
in your xml
<com.icl.examples.TextViewPlus
android:id="#+id/textViewPlus1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="#string/info_text"
android:textColor="#color/White"
android:textStyle="bold"
android:textSize="50sp"
foo:customFont="Bamini.ttf">
</com.icl.examples.TextViewPlus>
add this to resources, res->values-attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextViewPlus">
<attr name="customFont" format="string"/>
</declare-styleable>
</resources>

Where to Set Custom Typeface in Custom View Class (extends Button)?

I have a custom View class that extends Button. I'd like to set a custom Typeface to all instances of this class, but since some methods get called multiple times in a view, I was wondering what is the best overwriting method to place the following code in?
final Typeface face = Typeface.createFromAsset(getContext().getAssets(),
"myfont.ttf");
this.setTypeface(face);
Currently, I have it in onMeasure(), but I noticed it gets called multiple times, which I assume won't be good for performance.
Most correct is to add your code to constructor.
public class ButtonPlus extends Button {
public ButtonPlus(Context context) {
super(context);
}
public ButtonPlus(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs,
R.styleable.TextViewPlus);
String customFont = a.getString(R.styleable.TextViewPlus_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
return false;
}
setTypeface(tf);
return true;
}
}
here customFont - is my custom attribute. i specify font from layout using this attr. I just created following custom attr in values/attrs.xml

Categories

Resources