38.8 Making the Activity Talk to the Text Fragment
As outlined in “An Introduction to Android Fragments”, an activity can communicate with a fragment by obtaining a reference to the fragment class instance and then calling public methods on the object. As such, within the TextFragment class we will now implement a public method named changeTextProperties() which takes as arguments an integer for the font size and a string for the new text to be displayed. The method will then use these values to modify the TextView object. Within the Android Studio editing panel, locate and modify the TextFragment.kt file to add this new method:
package com.ebookfrenzy.fragmentexample
.
.
class TextFragment : Fragment() {
.
.
fun changeTextProperties(fontsize: Int, text: String)
{
binding.textView2.textSize = fontsize.toFloat()
&...