Search code examples
javaandroidtabsandroid-tablayout

How to lowercase all letter except first letter in TabLayout?


I want to lowercase all letters but want to keep the first letter in uppercase, I want to achieve this in tabLayout.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="23dp">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
            android:layout_height="match_parent"
            android:id="@+id/tabs"
            app:tabTextColor="@color/colorAccent"
            android:background="#fff">

        </android.support.design.widget.TabLayout>

    </android.support.design.widget.AppBarLayout>

</RelativeLayout>

Here is my code for tabLayout. I have included the following line but the problem is, its lowercasing all the letters.

app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"

I have also tried to do it programmatically i.e in my SectionsPagerAdapter class that I have for my fragments since I am viewPager in my activity. Here is my SectionsPagerAdapter Class,

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public SectionsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {
    return mFragments.get(i);
}

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

public void addFragment(Fragment fragment, String fragTitle) {

    mFragments.add(fragment);
    mFragmentTitleList.add(fragTitle.toLowerCase());

}

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

Also to call it in my working activity(or Fragment), I have used the following code,

public void setUpViewPager() {

    SectionsPagerAdapter adapter = new SectionsPagerAdapter(getChildFragmentManager());

    adapter.addFragment(new StudentsFragment(), "Students");

    adapter.addFragment(new ProfessorsFragment(), "Professors");

    adapter.addFragment(new AlumnusFragment(), "Alumnus");

    ViewPager viewPager = view.findViewById(R.id.container);

    viewPager.setAdapter(adapter);

    TabLayout tabLayout = view.findViewById(R.id.tabs);

    tabLayout.setupWithViewPager(viewPager);


}

Here I have given the names as "Student, Professors, Alumnus"(All letter are getting uppercased). I want it to be the same when I launch the app. But problem is when I include that one line in my tabLayout to lowercase the letters its also lowercasing the first letter. How can I achieve or overcome this issue?

enter image description here

Thanks in advance!


Solution

  • maybe this code snippet will help, try to apply it to your adapter class:

        public void addFragment(Fragment fragment, String fragTitle) {
    
        mFragments.add(fragment);
        fragTitle = fragTitle.toLowerCase()
        fragTitle = fragTitle.substring(0, 1).toUpperCase() + fragTitle.substring(1);
        mFragmentTitleList.add(fragTitle);
    
    }