2

I'm using the tab view in a fragment.
I want to change the font while I'm using calligraphy And here is the link: Calligraphy
I could change the font all over the app except the tab views.
The class's code is here, I really appreciate it if you can solve the problem,
I'm stucked here.
This is my code:

   public class AsliFragment extends Fragment {


    @SuppressLint("StaticFieldLeak")
    public static ViewPager viewPager;
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    public AsliFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            getArguments().getString(ARG_PARAM1);
            getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_asli, container, false);

        //init tablayout & viewPager
        initViewPager(view);

    return view;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
        } else {
           // Toast.makeText(context,"Remove Fregment Attached", Toast.LENGTH_SHORT).show();
        }
    }



    @Override
    public void onDetach() {
        super.onDetach();
    }


    interface OnFragmentInteractionListener {
    }

    private void initViewPager(View view){
        viewPager = view.findViewById(R.id.viewPager);
        TabLayout tabLayout = view.findViewById(R.id.tabLayout);
        //create adapter


        adabterViewPager adapter = new adabterViewPager(getChildFragmentManager());
        //get string tab name
        String tab_name_1 = getResources().getString(R.string.tab_name_1);
        String tab_name_2 = getResources().getString(R.string.tab_name_2);
        //add fragment to adapter
        adapter.addFragment(new FragmentOneAsli(), tab_name_1);
        adapter.addFragment(new FragmentTwoAsli(), tab_name_2);
        //set adapter to viewpager
        viewPager.setAdapter(adapter);
        //set tablayout with viewpager
        tabLayout.setupWithViewPager(viewPager);
    }
}
Anton Kazakov
  • 2,740
  • 3
  • 23
  • 34
kali
  • 337
  • 1
  • 3
  • 14

3 Answers3

6

Add this in style.xml

 <style name="CustomViewAllTab" parent="@android:style/TextAppearance.Widget.TabWidget">
        <item name="android:textSize">20sp</item>
        <item name="android:fontFamily">@string/fontHelveticaMed</item>
    </style>

For your custom font

 <item name="android:fontFamily">fontname</item>

Apply the style on TabLayout

 <android.support.design.widget.TabLayout
        android:id="@+id/tabOrderType"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorViewAllTab"
        app:tabBackground="@color/colorViewAllTab"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabMaxWidth="0dp"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/colorSelectedText"
        app:tabTextAppearance="@style/CustomViewAllTab"
        app:tabTextColor="@color/colorUnselectedTabColor" />

This property is used for changing text style in TabLayout

app:tabTextAppearance="@style/CustomViewAllTab"

Lokesh Desai
  • 2,607
  • 16
  • 28
1

Do this-:

private void changeTabsFont() {

        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.getInstance().getTypeFace(), Typeface.NORMAL);
                }
            }
        }
    } 
Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18
  • Thanks Shivam for your answer. How should I personalize this code depending on my classes? – kali Jan 16 '18 at 07:42
0

Try this with style

<style name="TabWidget" parent="TextAppearance.AppCompat.Medium">
        <item name="android:textSize">16sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:typeface">monospace</item>
</style>

Use this in xml in tablayout :

app:tabTextAppearance="@style/TabWidget"

Example

<android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorWhite"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/colorBlack"
        app:tabTextAppearance="@style/TabWidget"
        app:tabTextColor="@android:color/darker_gray" />
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31