How can i use custom fonts on ViewPager?

Go To StackoverFlow.com

0

i have an Android application which is using ViewPager and it works correctly, but now i want to add custom fonts to my view, when i slide across pages i change the layout.It is my code:

import com.viewpagerindicator.TitlePageIndicator;
import com.viewpagerindicator.TitleProvider;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

/** *************************************************************************************************
/** Clase:   public class LCMeter extends Activity
/** Notas:   esta Activity usa desplazamiento por paginas horizontal
/** Funcion: obtiene de puerto USB los datos de frecuencia, inductancia, capacitancia y los muestra
/**          con multiples funciones como creacion de graficos, logs, etc
****************************************************************************************************/
public class LCMeter extends Activity {

    // titulos de las paginas
    private static String[] titulosPaginas = { "APP 1", "APP 2", "APP 3" };
    private Context cxt;
    private ViewPager columnas;
    private ColumnasAdapter myAdapter;
    TextView txt;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  //layout con PagerViewer
        cxt = this;

        myAdapter = new ColumnasAdapter();
        columnas = (ViewPager) findViewById(R.id.columnas);
        columnas.setAdapter(myAdapter);

        // Agrega los titulos
        TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.titulos);    //layout XML
        titleIndicator.setViewPager(columnas);
    }

/** *************************************************************************************************
/** Clase:   private class ColumnasAdapter extends Pager Adapter implements TitleProvider
/** Notas:   extends PagerAdapter hace uso de la libreria y al usar implements TitleProvider permite
/**          colocar titulos a las paginas por las que nos movemos
/** Funcion: crea paginas por las cuales deslizarse hotizontalmente cambiando los Layout
****************************************************************************************************/
    private class ColumnasAdapter extends PagerAdapter implements TitleProvider {

        /// Obtiene numero de paginas en este caso el numero de elementos en el String
        @Override
        public int getCount() {
            return titulosPaginas.length;
        }

        // Obtiene el titulo para ponerle a la pagina
        @Override
        public String getTitle(int position) {
            // TODO Auto-generated method stub
            return titulosPaginas[position];    // titulo de la pagina
        }

        //Cambia los layout de acuerdo a las paginas
        @Override
        public Object instantiateItem(View collection, int position) {
            LinearLayout v = (LinearLayout) LayoutInflater.from(cxt).inflate(R.layout.lcmeter, null);

            // Ejecuto una Activity de acuerdo a la pagina en la que este
            switch (position) {
            case 0:
                v = (LinearLayout) LayoutInflater.from(cxt).inflate(R.layout.lcmeter, null);
                break;
            }

            ((ViewPager) collection).addView(v, 0);

            return v;
        }

        //Destruye las paginas cuando dejan de usarse
        @Override
        public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((LinearLayout)view);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((LinearLayout) object);
        }

        @Override
        public void finishUpdate(View arg0) {}

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {}

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {}

    }
}

I want to add custom fonts on the layout called "lcmeter" but when i put it:

TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");  
txt.setTypeface(font);

I get FC when running application but when i put the same code on a simple Activity whitout ViewPager it works perfectly i think the problem is on:

Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");

Maybe the context is not the right but i am new at this so i dont know too much. Thanks you :)

2012-04-04 23:11
by Andres Torti
Still gives me FC :/ its strange. Here my code: package com.multi.andres; import com.viewpagerindicator.TitlePageIndicator; import com.viewpagerindicator.TitleProvider; import android.app.Activity; import android.content.Context; import android.graphics.Typeface; import android.os.Bundle; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; /* ******************Andres Torti 2012-04-05 07:27


1

Try

TextView txt = (TextView) v.findViewById(R.id.custom_font);  

and then change your font

Something like this:

// Ejecuto una Activity de acuerdo a la pagina en la que este
switch (position) {
     case 0:
            v = (LinearLayout) LayoutInflater.from(cxt).inflate(R.layout.lcmeter, null);
            TextView txt = (TextView) v.findViewById(R.id.custom_font); 
            Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");  
            txt.setTypeface(font); 
            break;
     }
2012-04-04 23:15
by pleerock
still gives me FC :/ is strange : - Andres Torti 2012-04-05 00:12
put your logcat errors here; check if R.id.custom_font is really exists in R.layout.lcmeter layou - pleerock 2012-04-05 00:20
I have the log here but i am new so i have to wait 6 hours until i can attach it. I put it here: http://pastebin.com/PWFtmRs - Andres Torti 2012-04-05 00:30
hmmm does your Chantelli_Antiqua.ttf font really exists in the assets folder - pleerock 2012-04-05 00:33
Yes it is on assets folder, it is strange, maybe a solution would be using fragments instead of layouts - Andres Torti 2012-04-05 15:23
Ads