0

I've no java/kotlin/android background and been a week tryin to learn. I stuck at Settings Activity Listeners

I created a Settings Activity using New > Activity > Settings Activity and added a SeekBar

But whatever I tried, I couldn't make println("triggered") parts work

What am I doing wrong? Please do not forget that I'm a newbie while explaining

Thanks

PS: API level is 21

SettingsActivity.kt

import android.os.Bundle
import android.text.InputType
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.SeekBar
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.EditTextPreference
import androidx.preference.PreferenceFragmentCompat


class SettingsActivity2 : AppCompatActivity(), SeekBar.OnSeekBarChangeListener, EditTextPreference.OnBindEditTextListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.settings_activity)
        if (savedInstanceState == null) {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.settings, SettingsFragment())
                .commit()
        }
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }

    class SettingsFragment : PreferenceFragmentCompat() {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey)
        }
    }

    override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
        println("triggered1")
    }

    override fun onStartTrackingTouch(seekBar: SeekBar?) {
        println("triggered2")
    }

    override fun onStopTrackingTouch(seekBar: SeekBar?) {
        println("triggered3")
    }

    override fun onBindEditText(editText: EditText) {
        println("triggered4")
    }
}

root_preferences.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory app:title="@string/messages_header">

        <EditTextPreference
            app:key="signature"
            app:title="@string/signature_title"
            app:useSimpleSummaryProvider="true" />

        <ListPreference
            app:defaultValue="reply"
            app:entries="@array/reply_entries"
            app:entryValues="@array/reply_values"
            app:key="reply"
            app:title="@string/reply_title"
            app:useSimpleSummaryProvider="true" />
        <SeekBarPreference
            android:key="seekbar_preference"
            android:title="Brightness"
            android:summary="@string/value_percentage"
            android:progress="0"
            app:showSeekBarValue="true" />

    </PreferenceCategory>

    <PreferenceCategory app:title="@string/sync_header">

        <SwitchPreferenceCompat
            app:key="sync"
            app:title="@string/sync_title" />

        <SwitchPreferenceCompat
            app:dependency="sync"
            app:key="attachment"
            app:summaryOff="@string/attachment_summary_off"
            app:summaryOn="@string/attachment_summary_on"
            app:title="@string/attachment_title" />

    </PreferenceCategory>

</PreferenceScreen>
Ergec
  • 11,608
  • 7
  • 52
  • 62
  • And since it is the `SettingsFragment` that has `Seekbar`, the `SettingsFragment` is the one to implement `SeekBar.OnSeekBarChangeListener` and have `seekbar.setOnSeekBarChangeListener(this)`, not `SettingsActivity2`. – Onik Nov 06 '20 at 21:49
  • @Onik sorry for late response. In `root_preferences` you can't add `id` so I can't use `findViewById` to get `seekbar`. Is there some other way to access objects in preferences? Seems I'm missing something – Ergec Nov 09 '20 at 14:06
  • Haven't work with Preferences screen layout customisation but according to [How to add a button to PreferenceScreen](https://stackoverflow.com/q/2697233/3290339) it seems to be possible. – Onik Nov 09 '20 at 14:22

1 Answers1

0

You must use OnPreferenceChangeListener

Example

val seekbarPreference: SeekBarPreference? = findPreference("seekbar_preference")
seekbarPreference!!.onPreferenceChangeListener =
    Preference.OnPreferenceChangeListener { preference, newValue ->
         println("triggered1")
        true
    }