0

I want to implement fontawesome in tabs. This are the main things that matter.

MainActivity in OnCreate:

    viewPager = (ViewPager) findViewById(R.id.pager);
    setupViewPager(viewPager);    //Kreiramo viewpager oz. klic funkcije
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    if (tabLayout != null) {
        tabLayout.setupWithViewPager(viewPager);  //Poveže tab_layout z viewpagerjem
    }

A method that is in MainActivity for viewpager:

private void setupViewPager(ViewPager viewPager) {
    AdapterViewPager adapter = new AdapterViewPager(getSupportFragmentManager());
    //Kreiraš fragment, ki bo v ViewPagerju
    adapter.addFragment(new Fragment1(), "HOME");
    adapter.addFragment(new Fragment2(), "HEART");
    adapter.addFragment(new Fragment3(), "CRITERIA");
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(2);
}

Custom AdapterViewPager:

public class AdapterViewPager extends FragmentStatePagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();


    public AdapterViewPager(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}

How can accomplish this? Any ideas?

WinterChilly
  • 1,549
  • 3
  • 21
  • 34

1 Answers1

1

I used font Awesome in TabLayout but it's a bit nasty.

1- Put font Icon in your asset/font folder.

2- Use following code to apply your font:

private void changeTabsFont() {

        Typeface font = Typeface.createFromAsset(getAssets(),"fonts/font-awsome.ttf"); 
        ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
        int tabsCount = vg.getChildCount();
        for (int j = 0; j < tabsCount; j++) {
            ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
            int tabChildsCount = vgTab.getChildCount();
            for (int i = 0; i < tabChildsCount; i++) {
                View tabViewChild = vgTab.getChildAt(i);
                if (tabViewChild instanceof TextView) {
                    ((TextView) tabViewChild).setTypeface(font);
                }
            }
        }
    } 

3- Now just copy your font-awesome code in your string resource and use them, in your TabLayout.

<string name="icon_home">&#xf015;</string>

4- (alternative of 3) Override getTitle of your adapter:

 @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case POSITION_HOME:
                    return mContext.getResources().getString(R.string.icon_home);
             //  ... other title

            }
        }

Because Tablayout doesn't have setTypeface method you have to use such code. if you use PagerSlidingTabStrip it have such method and you can easily apply your font.

tabs.setTypeface(typeFace , 0);
Amir
  • 16,067
  • 10
  • 80
  • 119
  • Thanks for the help but, now it looks like this: http://prntscr.com/cc7xij Any way to increase it's size and change the color like it is for indicator? – WinterChilly Aug 30 '16 at 19:20
  • off course it's possible, there are lot's of thread out there about increase font size in tabLayout or selector for TabLayout – Amir Aug 30 '16 at 19:22
  • if you didn't find anythings let me help you :) – Amir Aug 30 '16 at 19:24
  • 1
    I found this: http://stackoverflow.com/questions/31471177/text-size-of-android-design-tablayout-tabs and now it's like this http://prntscr.com/cc8256 I'll try to fix the color – WinterChilly Aug 30 '16 at 19:27
  • http://prntscr.com/cc8aa3 do you have any idea how to increase the size of the house... it is not the same size as the other two :/ – WinterChilly Aug 30 '16 at 19:49
  • The home size is well But if you want to change it In for loop. – Amir Aug 30 '16 at 19:53
  • I tried and didn't work but oh well.. the fontawesome works i guess... Thanks again for the help – WinterChilly Aug 30 '16 at 20:00