How to use Roboto Font in TextView? - android

how to use roboto font type for my text views ?
I want to do it from xml and my app supports 4.1 above.
below is something which I tried:
<style name="BubbleNumber">
<item name="android:textStyle">normal</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#color/bubble_text_color</item>
</style>

Roboto is already the default font type (starting from Android 4.0)
see http://developer.android.com/design/style/typography.html
Otherwise you have to set the font programatically.
So I would recommend you to write a class:
public class StyledTextView extends TextView {
public StyledTextView(Context context) {
super(context);
style(context);
}
public StyledTextView(Context context, AttributeSet attrs) {
super(context, attrs);
style(context);
}
public StyledTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
style(context);
}
private void style(Context context) {
Typeface tf = Typeface.createFromAsset(context.getAssets(),
"fonts/roboto.ttf");
setTypeface(tf);
}
}
Then you can simply use it in your normal XML layout to replace the normal TextView
<LinearLayout
android:width="match_parent"
android:height="match_parent" >
<com.your.packakge.StyledTextView
android:width="match_parent"
android:height="match_parent" />
</LinearLayout>

For achieving roboto font type, You must add .ttf file of roboto in your asset folder. And set typeFace property to your text views. For example
TextView title=(TextView)findViewById(R.id.tv);
Typeface font = Typeface.createFromAsset(
activity.getAssets(),
"roboto.ttf");
title .setTypeface(font);
You can't set it in xml.

I have done like this coz in my app all I need is all fonts are Roboto.
if you need to control edittext or in button then add a view class and
extend it in custom text view or may be edittext then use it.
public class RobotoTextView extends TextView {
Context context;
public RobotoTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
public RobotoTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public RobotoTextView(Context context) {
super(context);
this.context = context;
}
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.NORMAL) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/RobotoNormal.ttf")/*
* ,
* -
* 1
*/);
} else if (style == Typeface.ITALIC) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/RobotoItalic.ttf")/*
* ,
* -
* 1
*/);
} else if (style == Typeface.BOLD) {
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/RobotoBold.ttf")/*
* ,
* -
* 1
*/);
}
}
}
now in your XML layout call it like this
<com.yourpakage.RobotoTextView
android:id="#+id/settings_cover_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textColor="#000000"
android:textSize="16sp"
android:text="Cover Photo"
android:layout_marginLeft="10dp"
/>

There is also a library Android-RobotoTextView that does that.

Related

Custom TextView with TextAppearance cannot have textColor overridden in XML

I'm trying to create a custom implementation of TextView that can be used to encapsulate all of the desired attributes of the text to be displayed.
What I've managed to do so far is:
Apply a custom font.
Apply a TextAppearance which includes things like letterSpacing and textColor.
This works as expected but when I try to change the colour of the text in XML the new text colour is completely ignored and the colour of the text remains the default that I've setup in the TextAppearance. It appears that the TextAppearance overrides the properties of the TextView provided in XML.
Question:
How do I create a custom TextView with default properties that can be modified in XML (mainly textColor)? Consider that this custom TextView exists in a separate module from the project it'll be used in.
Code:
CustomTextView
public class MyTextView extends AppCompatTextView {
public MyTextView(Context context) {
super(context);
init(context);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
setTextAppearance(context, R.style.MyText);
setTypeface(FontCache.futuraBookReg(getContext()));
}
}
Style
<style name="MyText" parent="TextAppearance.AppCompat">
<item name="android:textColor">#color/default_text_colour_selector</item>
<item name="android:textSize">28sp</item>
<item name="android:letterSpacing">0.04</item>
<item name="android:lineSpacingExtra">8sp</item>
</style>
FontCache (Included for completeness. Issue persists even without custom fonts)
class FontCache {
private static final String MY_FONT = "font/my_font.ttf";
private static Map<String, Typeface> fontMap = new HashMap<>();
static Typeface myFont(Context context) {
return getFont(context, MY_FONT);
}
private static Typeface getFont(Context context, String fontName) {
if (fontMap.containsKey(fontName)) {
return fontMap.get(fontName);
} else {
Typeface tf = Typeface.createFromAsset(context.getAssets(), fontName);
fontMap.put(fontName, tf);
return tf;
}
}
}
Attempt to colour TextView in XML
<com.example.text.MyText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/new_text_colour"
android:text="Hello" />

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);
}
}

How to chang List View Item Font Style TO MARATHI

I want to change the Font of List view items to marathi, I use Typeface to change the font of TextField, can anybody tell me how to do it with List View Items..
Create a custom textView Class like this
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();
}
#SuppressWarnings("null")
private void init() {
if (!isInEditMode()) {
// default style
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"helveticaneue_bold.ttf");
setTypeface(tf);
}
}
Place your font style (Marathi.ttf file, in this case i have used helveticaneue_bold.ttf ) in assets folder.
Now in your row, which you have inflated to ListView , instead of TextView use below
<yourpackagename.CustomTextView
android:id="#+id/tv_title"
style="#style/buttontext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/company_profile" >
</yourpackagename.CustomTextView>
You must implement CustomAdapter or BaseAdapter to achieve this. In getview method inflate other xml file which containing textview and settypeface to it

Categories

Resources