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
Related
Given a Button created at runtime:
Button button = Button(context)
The way to set a custom typeface is:
button.setTypeface(myTypeface)
However I find it only works before I add it to a ViewGroup and not after.
I've also tried:
button.setTypeface(myTypeface, myStyle)
but it didn't work either. I need to change my Button font dynamically. I've tried invalidate() and requestLayout() but the font never changes.
Solution:- you can subclass the Button class with your custom font and use it instead of button.
public class MyButton extends AppCompatButton {
public MyButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyButton(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font.ttf");
setTypeface(tf);
}
}
}
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>
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'm trying to decorate my application fonts. I get to do it from code. but do not know how to do it in the settings. in the code I do so
public class Fonts {
public static Typeface getHeaderFont(Context context){
return Typeface.createFromAsset(context.getAssets(), "header_levelI.ttf");
}
public static Typeface getSubHeaderFont(Context context){
return Typeface.createFromAsset(context.getAssets(), "header_levelII.ttf");
}
}
and
Typeface type= Fonts.getHeaderFont(getActivity());
TextView header = (TextView) v.findViewById(R.id.cassaName);
header.setTypeface(type);
settings android studio only standard fonts. How do I specify fonts in the file not to write extra code?
Create Custom TextView with your custom font.
Note: It's important that you place required font files in the assets/fonts directory.
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
setFont();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setFont();
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFont();
}
private void setFont() {
Typeface font = Typeface.createFromAsset(getContext().getAssets(),"fonts/ss-symbolicons-line.ttf");
setTypeface(font, Typeface.NORMAL);
}
}
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.
}
}