Search code examples
androidkotlintexttabsandroid-tablayout

TabLayout is not showing text


I want to build a TabLayout which add new tabs after an AlertDialog.

My problem: The text in the TabLayout is not showing. Screenshot

What I'm trying: - Changing the text of the tab after adding. This works with the newest tab. Other tabs loses its text - Setting the adapter for vp_participants every time if the user clicks on the positive button in my alert dialog

Can anybody help me?

In my Activity.kt:


    //Tab-Layout
    tabl_participants.setupWithViewPager(vp_participants)

    val adapter = TabPagerAdapter(supportFragmentManager)
    vp_participants.adapter = adapter


    img_bike_form.setOnClickListener {
        dialogNewParticipant("Bike", adapter)
    }


    ...


    fun dialogNewParticipant(bogen : String, adapter : TabPagerAdapter) {

        ...

        AlertDialog.Builder(this, if(getDarkMode(this)) { R.style.DarkAlertDialog } else { R.style.LightAlertDialog})
            .setTitle("Create new tab")
            .setMessage("Choose a number")
            .setView(view)
            .setPositiveButton("OK") { _, _ ->
                var existing = false

                ...

                if(!existing) {

                    when(numberPicker.value) {
                        1 -> adapter.addTab(Tab1Fragment(), "Tab 1")
                        2 -> adapter.addTab(Tab2Fragment(), "Tab 2")
                    }

                    adapter.notifyDataSetChanged()


                } 
            }
            .show()
    }
}

TabPagerAdapter.kt:


    class TabPagerAdapter (fm: FragmentManager) : FragmentPagerAdapter(fm) {

        val mFragmentList = ArrayList<Fragment>()
        val mFragmentTitleList = ArrayList<String>()

        override fun getItem(position: Int): Fragment {
            return mFragmentList.get(position)
        }

        override fun getCount(): Int {
            return mFragmentList.size
        }

        fun addTab(fragment : Fragment, title : String) {
            mFragmentList.add(fragment)
            mFragmentTitleList.add(title)
        }
    }

activity.xml:

    ...

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabl_participants"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@color/colorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:tabIconTint="@color/background_light"
            app:tabIndicatorColor="#FFFFFF"
            app:tabMode="scrollable"
            app:tabTextColor="#FFFFFF" />

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/vp_participants"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tabl_participants" />

    </androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • You have to override also the getPageTitle(int position) method:

    class TabPagerAdapter (fm: FragmentManager) : FragmentPagerAdapter(fm) {
    
        // ....  
    
        override fun getPageTitle(position: Int): CharSequence? {
                return mFragmentTitleList[position]
            }    
    
    }