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>
Related
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
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
I created a custom Button, TextView, and ImageView. None of these appear properly in the Graphical Layout of any XML. Instead of showing a button or text with a custom font, it instead shows a huge grey box with the name of the custom class I'm calling. How do I get these to show in the preview?
public class FontTextView extends TextView {
public static Typeface FONT_NAME;
public FontTextView(Context context) {
super(context);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
this.setTypeface(FONT_NAME);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
this.setTypeface(FONT_NAME);
}
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
this.setTypeface(FONT_NAME);
}
and
<com.example.gesturetest.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text placeholder" />
You can do this in your Custom View:
if(!isInEditMode()){
// Your custom code that is not letting the Visual Editor draw properly
// i.e. thread spawning or other things in the constructor
}
Reference
do this in the constructor of your customView
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode()) {
createTypeface(context, attrs); //whatever added functionality you are trying to add to Widget, call that inside this condition.
}
}
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
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.