Home > Enterprise >  How to setOnSeekBarChangeListener in a Kotlin Fragment
How to setOnSeekBarChangeListener in a Kotlin Fragment

Time:03-30

I am currently working on a mobile application in my uni classes that utilizes a seekBar to let the user decide to use a timer for a quiz. This application uses main to host all of the fragments. currently I just want the textbox to display where the user scrolled the seek bar to but am struggling to find a solution. any advice would be greatly appreciated. this is the code I have within the TitleFragment.kt:

package com.example.android.guesstheword.screens.title

import android.os.Bundle
import android.view.DragEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.Switch
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil
import androidx.databinding.adapters.SeekBarBindingAdapter
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.android.guesstheword.R
import com.example.android.guesstheword.databinding.TitleFragmentBinding
import kotlinx.android.synthetic.main.title_fragment.*

/**
 * Fragment for the starting or title screen of the app
 */
class TitleFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View {

        // Inflate the layout for this fragment
        val binding: TitleFragmentBinding = DataBindingUtil.inflate(
                inflater, R.layout.title_fragment, container, false)

        binding.timerSwitch.setOnClickListener {

            if(binding.timerSwitch.isChecked){
                binding.timerBar.visibility = View.VISIBLE
            }
            else{
                binding.timerBar.visibility = View.INVISIBLE
            }
        }

        val seekBar = binding.timerBar
        seekBar.setOnSeekBarChangeListener(binding)
        binding.playGameButton.setOnClickListener {

            findNavController().navigate(TitleFragmentDirections.actionTitleToGame())
        }
        return binding.root
    }
}

private fun SeekBar.setOnSeekBarChangeListener(binding: TitleFragmentBinding) {
    val seconds = binding.timerSeconds
    seconds.visibility = View.VISIBLE
    seconds.text = binding.timerBar.progress.toString()
}

title_fragment.xml:

<?xml version="1.0" encoding="utf-8"?><!--
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/title_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".screens.title.TitleFragment">

        <TextView
            android:id="@ id/get_ready_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:fontFamily="sans-serif"
            android:text="@string/get_ready"
            android:textColor="@color/black_text_color"
            android:textSize="14sp"
            android:textStyle="normal"
            app:layout_constraintBottom_toTopOf="@ id/title_text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed" />

        <TextView
            android:id="@ id/title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:fontFamily="sans-serif"
            android:text="@string/title_text"
            android:textColor="@color/black_text_color"
            android:textSize="34sp"
            android:textStyle="normal"
            app:layout_constraintBottom_toTopOf="@ id/play_game_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/get_ready_text"
            app:layout_constraintVertical_chainStyle="packed" />

        <Button
            android:id="@ id/play_game_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="32dp"
            android:text="@string/play_button"
            android:theme="@style/GoButton"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <Switch
            android:id="@ id/timer_switch"
            android:layout_width="95dp"
            android:layout_height="52dp"
            android:text="Timer"
            android:min="5"
            android:max="360"
            app:layout_constraintBottom_toTopOf="@ id/play_game_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/title_text"
            app:layout_constraintVertical_bias="0.064"/>

        <SeekBar
            android:id="@ id/timerBar"
            android:layout_width="132dp"
            android:layout_height="85dp"
            android:clickable="false"
            android:visibility="gone"
            app:layout_constraintBottom_toTopOf="@ id/play_game_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/timer_switch"
            app:layout_constraintVertical_bias="0.144" />

        <TextView
            android:id="@ id/timer_seconds"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:layout_constraintBottom_toTopOf="@ id/play_game_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/timerBar"
            app:layout_constraintVertical_bias="0.17" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

CodePudding user response:

You created an extension function called setOnSeekBarChangeListener which does not actually set it.

In order to set the listener, you need to do something like this:

binding.timerBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
    override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
        // here, you react to the value being set in seekBar
    }

    override fun onStartTrackingTouch(seekBar: SeekBar) {
        // you can probably leave this empty
    }

    override fun onStopTrackingTouch(seekBar: SeekBar) {
        // you can probably leave this empty
    }
})

But keep in mind that this is NOT the extension function you created.

  • Related