0

So basically, I'm trying to change the font on all the views of a xml layout. This can only be done through a non-xml approach. For some reason, setFont will not work with my inflated view group child. I think I'm going about the viewGroup wrong... How could I instantiate this better? The code works when I use a regular view like a button, but I dont want to define a million buttons and textviews so my solution was to make a viewgroup created from the layout and to iterate through all the views in it to change the font. Ug please help!

public static void applyFonts(final View v, Typeface fontToSet) {
    try {

        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;

            for (int i = 0; i < vg.getChildCount(); i++) {
                View child = vg.getChildAt(i);
                applyFonts(child, fontToSet);
            }
        } else if (v instanceof TextView) {
            ((TextView) v).setTypeface(fontToSet);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.main, null);


    Typeface rockwellFont = Typeface.createFromAsset(getAssets(), "Rockwell.ttf");
     LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.main, null);
     applyFonts(vg,rockwellFont);
     setContentView(vg);
Kohler Fryer
  • 89
  • 2
  • 7
  • check this.. http://stackoverflow.com/questions/7484471/loop-through-all-childs-of-groupview – Sankar V Feb 14 '13 at 06:10
  • don't you have adapter ?, with adapter you can do it very easiy – William Kinaan Feb 14 '13 at 06:14
  • Do you actually use the *vg* in your `Activity` as the content view or as part of the content view or you **just inflate it**? – user Feb 14 '13 at 06:14
  • What @Luksprog said is probably the problem - you're probably calling `setContentView(R.layout.main)` rather than `setContentView(vg)`. This inflates a new `ViewGroup` rather than using the one you have a reference to. – Adam S Feb 14 '13 at 06:51
  • Thanks a ton guys, I will try these out when i get home. Your right, I remember calling setContentView(R.layout.main) instead of setContentView(vg). That makes sense, I made a new instance of view and never used it. – Kohler Fryer Feb 14 '13 at 18:02
  • Did it now I'm getting a fatal exception main---java.lang.runtime error – Kohler Fryer Feb 15 '13 at 07:26
  • OH wait, I see a null pointer exception- what would be null in this code? – Kohler Fryer Feb 15 '13 at 07:33

1 Answers1

1

Its Better you extend your view which you want to have custom fonts Like this

package com.shahidawat.external;

    import com.shahidawat.R;
    import android.content.Context;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    import android.widget.TextView;

public class CustomTextView extends TextView {

        public CustomTextView(Context context) {
                super(context);
                init(context);

        }

        public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                init(context);
        }

        public CustomTextView(Context context, AttributeSet attrs) {
                super(context, attrs);
                init(context);
        }

        private void init(Context context) {
                Typeface type = Typeface.createFromAsset(context.getAssets(),
                                "fonts/MonotypeCorsiva.ttf");

                setTypeface(type);
                setTextColor(context.getResources().getColor(R.color.black));
        }

}

then in XML you can do like this

<com.shahidawat.external.CustomTextView
        android:id="@+id/txtHeader"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/separater"
        android:gravity="bottom|center_horizontal"
        android:text="Header"
        android:textSize="20sp"
        android:textStyle="bold" />
Rohit
  • 1,001
  • 1
  • 11
  • 20