Search code examples
androidandroid-studioandroid-typeface

Trying to use setTypeface in a ViewPager


 public class HelpSettingsFragment extends Fragment {

    /**
     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
     */
    private ViewPager mPager;

    /**
     * The pager adapter, which provides the pages to the view pager widget.
     */
    private PagerAdapter mPagerAdapter;

    Typeface champagneRegularFont;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tutorial, container, false);

        ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        SpannableString s = new SpannableString("Help");
        s.setSpan(new TypefaceSpan(getContext(), "Candy.ttf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        actionBar.setTitle(s);

        champagneRegularFont = Typeface.createFromAsset(getActivity().getAssets(),"fonts/Champagne & Limousines.ttf");

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) rootView.findViewById(R.id.vpPager);
        mPagerAdapter = new CustomPagerAdapter(getContext());
        mPager.setAdapter(mPagerAdapter);

        return rootView;
    }

    /**
     * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
     * sequence.
     */
    public class CustomPagerAdapter extends PagerAdapter {

        private Context mContext;

        public CustomPagerAdapter(Context context) {
            mContext = context;
        }

        @Override
        public Object instantiateItem(ViewGroup collection, int position) {
            ModelObject modelObject = ModelObject.values()[position];
            LayoutInflater inflater = LayoutInflater.from(mContext);
            ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);
            collection.addView(layout);

            return layout;
        }

        @Override
        public void destroyItem(ViewGroup collection, int position, Object view) {
            collection.removeView((View) view);
        }

        @Override
        public int getCount() {
            return ModelObject.values().length;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            ModelObject customPagerEnum = ModelObject.values()[position];

            return mContext.getString(customPagerEnum.getTitleResId());
        }

    }

    public enum ModelObject {

        FIRST(R.string.blue, R.layout.tutorial_1),
        SECOND(R.string.red, R.layout.tutorial_2),
        THIRD(R.string.green, R.layout.tutorial_3);

        private int mTitleResId;
        private int mLayoutResId;

        ModelObject(int titleResId, int layoutResId) {
            mTitleResId = titleResId;
            mLayoutResId = layoutResId;
        }

        public int getTitleResId() {
            return mTitleResId;
        }

        public int getLayoutResId() {
            return mLayoutResId;
        }

    }

}

I am using a ViewPager so the user can scroll through 3 different layouts. I am trying to set a custom typeface to some TextViews and Buttons. Does anyone know where I instantiate the objects and be able to set the custom typeface via setTypeface(champagneRegularFont)?

Edit: Also, would I add onClickListeners in the same place?

No longer need to know how to implement onClickListeners as I have replaced it with an onClick method in the xml file, and declared the method in my activity class.


Solution

  • You are inflating the layout as,

    ViewGroup layout = (ViewGroup)   
    inflater.inflate(modelObject.getLayoutResId(), collection, false);
    

    Now set the typeFace you initialized on your onCreateView() as follows,

    TextView textView=(TextView)layout.findViewById(R.id.textView);
    textView.setTypeface(champagneRegularFont);