I am trying to create a TextView with custom font-family. I have Created one which supports the normal font. But I am unable to implement bold/italic styling on the custom TextView.
This is my Custom TextView Class
public class KTextView extends TextView {
public KTextView(Context context) {
super(context);
TypefaceHelper.setTypeface(context, this);
}
public KTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypefaceHelper.setTypeface(context, this);
}
public KTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypefaceHelper.setTypeface(context, this);
}
}
This is the TypefaceHelper Class
public class TypefaceHelper {
private static final String FONT = "AvenirNext-Regular.otf";
private static Typeface typeface = null;
public static void setTypeface(Context context, TextView textView) {
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), FONT);
}
textView.setTypeface(typeface);
}
Can anyone suggest a way to implement Bold and Italic Styling.
try this:
textview.setTypeface(typeface, Typeface.BOLD);
you can use also ITALIC and BOLD_ITALIC
for custom textview use:
public class MyTextView extends TextView {
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(Typeface.createFromAsset(getContext().getAssets(), "fonts/fonts_name")/*, -1*/);
} else {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/fonts_name")/*, -1*/);
}
}
}
Related
I am trying to subclass TextInputLayout, in a CustomTextInputLayout. This is the code:
public class CustomTextInputLayout extends TextInputLayout {
public CustomTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomTextInputLayout(Context context) {
super(context);
}
public View getComponent(){
return this.getChildAt(0);
}
public void setHint(String hint){
super.setHint(hint);
}
}
I am using it to wrap another custom component, with a button and an EditText:
public class ComponentesSeleccion extends RelativeLayout {
private EditText editText;
private Button button;
public ComponentesSeleccion(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ComponentesSeleccion(Context context) {
super(context);
inicializar();
}
public ComponentesSeleccion(Context context, AttributeSet attrs) {
super(context, attrs);
inicializar();
}
private void inicializar() {
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li =
(LayoutInflater)getContext().getSystemService(infService);
li.inflate(R.layout.seleccionlayout, this, true);
editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button2);
}
public Button getButton() {
return button;
}
public EditText getEditText() {
return editText;
}
public void setInputType(int type){
editText.setInputType(type);
}
public void setImagen(Drawable drawable){
button.setBackgroundDrawable(drawable);
}
}
With this, I am not able to do in my code:
CustomTextInputLayout ctil=(CustomTextInputLayout)view.findViewById(R.id.ctil);
ctil.setHint(“a hint");
No exception, no errors, but the hint is not drawn. i can set the normal hint on the EditText, but I want the animated hint of the TextInputLayout.
How could I get it working?
Thank you.
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"
/>
I am currently implementing Roboto font within my project. For some fragments, there are a lot of TextView's. I am creating a custom View that exends TextView to implement custom fonts. Is there a better way to load the fonts without increasing the onCreate times?
Extends TextView
public class TextViewFont extends TextView {
public TextViewFont(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public TextViewFont(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public TextViewFont(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextViewFont);
String fontName = a.getString(R.styleable.TextViewFont_fontName);
if (fontName != null) {
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName);
setTypeface(myTypeface);
}
a.recycle();
}
}
}
XML
<com.eugene.fithealthmaingit.Custom.TextViewFont
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:text="#string/dinner"
android:textColor="#color/text_color"
android:textSize="16sp"
app:fontName="Roboto-Regular.ttf"/>
Example of how many TextView's
This is the library that saved my life Calligraphy. It's really nice and easy to use.
Typeface.createfromassets is a time taking process. You should declare typeface as static varaiable in class and just use it in constructor.
But here you are loading fonts in every textview's constructor.
If you having multiple fonts, have all tytypeface as static and use it appropriately.
UPDATE CODE:
public class TextViewFont extends TextView {
public static Typeface typeface1 = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontName1");
public static Typeface typeface2 = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontName2");
public static Typeface typeface3 = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontName3");
public static Typeface typeface4 = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontName4");
public static Typeface typeface5 = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontName5");
public TextViewFont(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public TextViewFont(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public TextViewFont(Context context) {
super(context);
init(null);
}
private void init(AttributeSet attrs) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextViewFont);
String fontName = a.getString(R.styleable.TextViewFont_fontName);
if (fontName != null) {
setTypeface(getTypeFace(fontName));
}
a.recycle();
}
}
public Typeface getTypeFace(String fontName){
if(fontName.equals("fontName1")){
return typeface1;
}else if(fontName.equals("fontName2")){
return typeface2;
}else if(fontName.equals("fontName3")){
return typeface3;
}else if(fontName.equals("fontName4")){
return typeface4;
}else if(fontName.equals("fontName5")){
return typeface5;
}
}
}
}
Try using an instance singleton or applicationsingleton. See if this works. So you can just call TextLover.get(context).getFont(id). It will create and cache it on the fly. This way your other views can also reuse the font cache. eg. buttons
class TextLover {
private static TextLover singleton;
private final Context context;
private final SparseArray<Typeface> faces = new SparseArray<Typeface>();
public TextLover get(Context context) {
if (singleton == null) {
singleton = new TextLover(context);
}
return singleton;
}
private TextLover(Context context) {
this.context = context;
}
private static final String[] fonts = {
"fonts/fontName1",
"fonts/fontName2",
"fonts/fontName3",
"fonts/fontName4",
"fonts/fontName5",
...
"fonts/fontName100"
}
// NOTE you need a mapping of ids to each asset font in fonts[]
public Typeface getFont(int id) {
Typeface font = faces.get(id);
if (font == null) {
font = Typeface.createFromAsset(context.getAssets(), fonts[id]);
faces.append(id, font);
}
return font;
}
}
I want to change some textview font-face to external font and i do something like this :
typeFace = Typeface.createFromAsset(getAssets(),"fonts/bkoodak.ttf");
tv1.setTypeface(typeFace);
tv2.setTypeface(typeFace);
tv3.setTypeface(typeFace);
...
But this form is't nice to me.
Is there some way to do this better?
Yes there is a better way.
But you have to create your own derived TextView that apply the TypeFace. And use it in your XML Layout.
Refer to this question for more details:
How to make a custom TextView?
You can create own TextView class:
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) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.MyTextView);
Typeface myTypeface = Typeface.createFromAsset(getContext()
.getAssets(), "fonts/bkoodak.ttf");
setTypeface(myTypeface);
a.recycle();
}
}
}
and use it in you layout:
<yourpackage.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
You can use create a class and use it everywhere.
For example:
FontChanger Class:
public class FontChanger
{
private Typeface typeface;
public FontChanger(Typeface typeface)
{
this.typeface = typeface;
}
public FontChanger(AssetManager assets, String assetsFontFileName)
{
typeface = Typeface.createFromAsset(assets, assetsFontFileName);
}
public void replaceFonts(ViewGroup viewTree)
{
View child;
for(int i = 0; i < viewTree.getChildCount(); ++i)
{
child = viewTree.getChildAt(i);
if(child instanceof ViewGroup)
{
// recursive call
replaceFonts((ViewGroup)child);
}
else if(child instanceof TextView)
{
// base case
((TextView) child).setTypeface(typeface);
}
}
}
}
onCreate of your activity :
FontChanger fontChanger = new FontChanger(getAssets(), "font.otf");
fontChanger.replaceFonts((ViewGroup)this.findViewById(android.R.id.content));
How can i set text color and text size in the class below
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);
}
}
In your init use setTextColor and setTextSize
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
setTypeface(tf ,1);
this.setTextColor(Color.RED);
this.setTextSize(20f);
}
If you are looking for custom attributes check this
Setting color of a Paint object in custom view