Suppose I have got a seek bar in the activity to change the textview font size of the fragment. What method should I pass?
I have an activity. This includes a view pager. The view pager has a pager adapter. For each item in the pager adapter, we create new instance of fragment. When I drag the SeekBar, I want to pass the value onto the fragment. I have applied interface callback and also passing argument bundle. But, when it comes to implementation and testing, the font size des not change.
Would you please advise me the way to pass one value from a seek bar of an activity to a fragment within the pager adapter ?
Here is my working :
class ChapterActivity : AppCompatActivity() , ViewPager.OnPageChangeListener {
...
val listener = object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
val scaledSize = progress * 0.6 + minimumValue
println("scaledSize : $scaledSize" )
println("scaledSize : ${scaledSize.toFloat()}" )
//txt_chapter_content.setTextSize(TypedValue.COMPLEX_UNIT_DIP, scaledSize .toFloat() );
val prefs = getPreferences(Context.MODE_PRIVATE)
val ed = prefs.edit()
ed.putFloat("fontsize", scaledSize.toFloat())
ed.apply()
val myBundle = Bundle()
myBundle.putFloat("fontsize" , scaledSize.toFloat() )
mAboutDataListener!!.onDataReceived(scaledSize.toFloat())
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
}
}
chapterPagerAdapter = ChapterPagerAdapter(supportFragmentManager, chapters)
// Set the Adapter and the TabLayout forward the ViewPager
chapterViewPager.adapter = chapterPagerAdapter
chapterViewPager.addOnPageChangeListener(this);
Fragment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout forward this fragment
val view = inflater.inflate(com.books.learn.ddy.blinkist.R.layout.content_chapter, container, false)
val titleTextView = view.findViewById<TextView>(R.id.txt_chapter_title)
val contextTextView = view.findViewById<TextView>(R.id.txt_chapter_content)
contextTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, floatSize )
override fun onDataReceived(fontSize: Float) {
contextTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize );
}