Question

One of my app activity include many textview i want to set multiple custom font for that textviews,

I tried the below code but it doesn't work to customize textview ,

any help will be appreciated

public class Text extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv2=(TextView)findViewById(R.id.text2);
        tv2.setText(Html.fromHtml(getString(R.string.text_2)));

        TextView tv3=(TextView)findViewById(R.id.text3);
        tv3.setText(Html.fromHtml(getString(R.string.text_3)));

        TextView tv4=(TextView)findViewById(R.id.text4);
        tv4.setText(Html.fromHtml(getString(R.string.text_4)));

        TextView tv5=(TextView)findViewById(R.id.text5);
        tv5.setText(Html.fromHtml(getString(R.string.text_5)));
    }


    class MyTextView extends TextView {
        private String TextView;

        public MyTextView(Context context,int  string) {
            super(context );
        }

        public  MyTextView(Context context) {
            super(context);
        }

        public void setTypeface(Typeface tf, int string) {
            if (TextView == "tv2,tv4") {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(),
                    "BFantezy.ttf"));
            }
            else if (TextView == "tv3,tv5") {
                super.setTypeface(Typeface.createFromAsset(getContext().getAssets(),
                    "RoseNewB.ttf"));
            }
        }
    }
}

main.xml:

  <?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">

      <TextView
          android:id="@+id/text2"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"   
          android:textSize="25sp"/>

      <View
        android:layout_width="fill_parent"
        android:layout_height="2dp"/>


      <TextView
          android:id="@+id/text3"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="25sp"/>

      <View
        android:layout_width="fill_parent"
        android:layout_height="2dp"/>

       <TextView
          android:id="@+id/text4"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"              
          android:textSize="25sp"/>

      <View
        android:layout_width="fill_parent"
        android:layout_height="2dp"/>

      <TextView
          android:id="@+id/text5"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"              
          android:textSize="25sp"/>

    </LinearLayout>
 </ScrollView>
Was it helpful?

Solution

You are comparing it in a wrong way.. Do this instead

if (getId() == R.id.text2)                                    
           super.setTypeface(Typeface.createFromAsset(getContext().getAssets(),
                 "BFantezy.ttf"));  

and create your textView using MyTextView.

And like this compare all.

OTHER TIPS

You are creating the same typeface every time you set it.

super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "BFantezy.ttf")); 

Just create it once and reuse it over and over. This call is heavy and doing it 40 times will surely increase the load time.

public class Text extends Activity {    
    /** Called when the activity is first created. */
    private Static Typeface bf = Typeface.createFromAsset(getContext().getAssets(),
             "BFantezy.ttf");
    private Static Typeface rn = Typeface.createFromAsset(getContext().getAssets(), 
             "RoseNewB.ttf")

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 

        TextView tv2=(TextView)findViewById(R.id.text2);
        tv2.setText(Html.fromHtml(getString(R.string.text_2)));
        tv2.setTypeface(bf);

        TextView tv3=(TextView)findViewById(R.id.text3);
        tv3.setText(Html.fromHtml(getString(R.string.text_3)));
        tv3.setTypeface(rn);

        TextView tv4=(TextView)findViewById(R.id.text4);
        tv4.setText(Html.fromHtml(getString(R.string.text_4)));
        tv4.setTypeface(bf);

        TextView tv5=(TextView)findViewById(R.id.text5);
        tv5.setText(Html.fromHtml(getString(R.string.text_5)));}
        tv5.setTypeface(rn);
    ...

Custom TextView is a overkill for this purpose. You can remove the code below.

class MyTextView extends TextView {     
    private String TextView;
    private Static Typeface bf = Typeface.createFromAsset(getContext().getAssets(),
             "BFantezy.ttf");
    private Static Typeface rn = Typeface.createFromAsset(getContext().getAssets(), 
             "RoseNewB.ttf")

    public MyTextView(Context context,int string) {
        super(context);
        updateTypeface();
    } 

    public  MyTextView(Context context) {       
        super(context);      
        updateTypeface();
    }

    private void updateTypeface() {
        if (getId() == R.id.text2 || getId() == R.id.text4) {                                         
            super.setTypeface(bf);     
        } 
        else if (getId() == R.id.text3 || getId() == R.id.text5) { 
            super.setTypeface(rn);   
        }  
    }

}

EDIT: Updated code. Updating typeface automatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top