android - Custom font is only applied on first textview and button -


custom font applied on first textview , button, not on rest.i had used findviewbyid views. had used fontfam id textview , btfontfam button

xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margintop="100dp" >   <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_gravity="center"     android:textsize="24sp"     android:text="@string/l1" />  <button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_gravity="center"     android:text="@string/lbtn"     android:textsize="24sp" />  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_gravity="center"     android:text="@string/l2"     android:textsize="24sp" />  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_gravity="center"     android:text="@string/l3"     android:textsize="24sp" />  <textview     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_gravity="center"     android:text="@string/l4"     android:textsize="24sp" />  <button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_gravity="center"     android:text="@string/sbtn"     android:textsize="24sp" /> 

public class mainactivity extends activity {  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      typeface tf = typeface.createfromasset(getassets(),             "fonts/roboto-medium.ttf");      textview tv = (textview) findviewbyid(r.id.fontfam);     tv.settypeface(tf);      button bt = (button) findviewbyid(r.id.btfontfam);     bt.settypeface(tf);  } } 

you should extends textview, button,... class using custom fonts.

first, add new property "values/attrs.xml" file:

<?xml version="1.0" encoding="utf-8"?> <resources>     <declare-styleable name="textviewplus">         <attr name="customfont" format="string"/>     </declare-styleable> </resources> 

second, rewrite textview class:

package com.ex.ui;  import android.content.context; import android.content.res.typedarray; import android.graphics.typeface; import android.util.attributeset; import android.widget.textview;  public class textviewplus extends 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 = 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;     } } 

then, using custom font:

<com.ex.ui.textviewplus xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res/com.ex"   android:id="@android:id/text1"     android:layout_width="match_parent" android:layout_height="match_parent" app:customfont="roboto-light.ttf"/> 

note:

"xmlns:app="http://schemas.android.com/apk/res/com.ex" => "com.ex" main package, mean can access resource via com.ex.r


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -