SmartTabLayout icon indicating copy to clipboard operation
SmartTabLayout copied to clipboard

how to change font of tabs ?

Open salehmosleh opened this issue 8 years ago • 8 comments

how to change font of tabs ? by add typeface

salehmosleh avatar Aug 17 '16 16:08 salehmosleh

Hey Brother, i hope your doing great as we live in iran, so why not using some iran_sans or yekan,..

you can add custom fonts to SmartTabLayout in many ways, but one of them is:

SmartTabLayout tabs = (SmartTabLayout) findViewById(R.id.tabs);
        tabs.setCustomTabView(new SmartTabLayout.TabProvider() {
            @Override
            public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
                LayoutInflater inflater = LayoutInflater.from(container.getContext());
                Resources res = container.getContext().getResources();
                View tab = inflater.inflate(R.layout.custom_tab, container, false);
                TextView customText = (TextView)  tab.findViewById(R.id.custom_text);
                switch (position) {
                    case 0:
                        customText.setTypeface(Util.GetSelfTypeFace(mainInstance(), 5));
                        customText.setText(adapter.getPageTitle(position));
                        break;
                    case 1:
                        customText.setTypeface(Util.GetSelfTypeFace(mainInstance(), 4));
                        customText.setText(adapter.getPageTitle(position));
                        break;
                    case 2:
                        customText.setTypeface(Util.GetSelfTypeFace(mainInstance(), 3));
                        customText.setText(adapter.getPageTitle(position));
                        break;
                    default:
                        throw new IllegalStateException("Invalid position: " + position);
                }
                return tab;
            }
});

you just need to create a new SmartTabLayout.TabProvider() even if you add your custom view in xml, you can set your own typeface in your MainActivity for whole views and... there is many options but i've done it this way.

for the the typeface loader i made my own lib that you can download form here: AndroidIranSans or you can just easily:

Typeface type = Typeface.createFromAsset(getAssets(),"fonts/yekan.ttf");
yourTextView.setTypeface(type);

if you need any more details just let me know.

hello guys maybe this can be helpful for you too @saeedsh92 @linsea @yangxianzhu

truthofmatthew avatar Sep 06 '16 09:09 truthofmatthew

Thank you so much my brother @mtkarimi .

salehmosleh avatar Sep 06 '16 12:09 salehmosleh

@salehmosleh :bowtie: welcome bro, just let me know if its works fine for you :punch: and sure when you released your apk on bazar gimme the link to put some :star::star::star::star::star: for you.

truthofmatthew avatar Sep 06 '16 12:09 truthofmatthew

@mtkarimi Thank you,and i found a easy way to solve this problem by using "stl_customTabTextLayoutId" and "stl_customTabTextViewId"

yangxianzhu avatar Sep 18 '16 06:09 yangxianzhu

@yangxianzhu yes sure its works for you, but in out language (Persian) we need to set the typeface and other stuff to make sure everything looks the way we need. thanks for mentioning those, i used them before but can't get what we need with them, or we need to use calligraphy and other ways of setting typefaces...

truthofmatthew avatar Sep 18 '16 06:09 truthofmatthew

@mtkarimi yeah,you're right and your solution is more useful,it can set different font to different position

yangxianzhu avatar Sep 18 '16 06:09 yangxianzhu

Hi brother

what is R.id.custom_text and where I can find it

SmartTabLayout tabs = (SmartTabLayout) findViewById(R.id.tabs); tabs.setCustomTabView(new SmartTabLayout.TabProvider() { @Override public View createTabView(ViewGroup container, int position, PagerAdapter adapter) { LayoutInflater inflater = LayoutInflater.from(container.getContext()); Resources res = container.getContext().getResources(); View tab = inflater.inflate(R.layout.custom_tab, container, false); TextView customText = (TextView) tab.findViewById(R.id.custom_text); switch (position) { case 0: customText.setTypeface(Util.GetSelfTypeFace(mainInstance(), 5)); customText.setText(adapter.getPageTitle(position)); break; case 1: customText.setTypeface(Util.GetSelfTypeFace(mainInstance(), 4)); customText.setText(adapter.getPageTitle(position)); break; case 2: customText.setTypeface(Util.GetSelfTypeFace(mainInstance(), 3)); customText.setText(adapter.getPageTitle(position)); break; default: throw new IllegalStateException("Invalid position: " + position); } return tab; } });

devyaghi avatar Mar 29 '17 21:03 devyaghi

@mtkarimi hi bro thx for good solution . i have a question that i want active tab has same color with indicator !! how can i set custom color to active tab and disactive tab with this custom tab ? please answer me quick !! I'm doing project and must to solve this problem fast . tank you again .

alirezanazari avatar Oct 12 '17 10:10 alirezanazari