Skip to content

Instantly share code, notes, and snippets.

@ahmed-shehataa
Created November 13, 2020 19:17
Show Gist options
  • Save ahmed-shehataa/ebff2a176c940000ac701f945ed88e2a to your computer and use it in GitHub Desktop.
Save ahmed-shehataa/ebff2a176c940000ac701f945ed88e2a to your computer and use it in GitHub Desktop.
import android.os.Bundle
import android.view.View
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.ashehata.linky.R
import com.ashehata.base.externals.hideIcon
import com.ashehata.linky.main.MainActivity
import com.ashehata.base.externals.restartActivity
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.fragment_settings, null)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initUi()
handleChangeLang()
handleChangeTheme()
}
private fun handleChangeTheme() {
findPreference<Preference>(getString(R.string.key_dark_theme))?.setOnPreferenceChangeListener { _, _ ->
// Restart activity
activity?.recreate()
return@setOnPreferenceChangeListener true
}
}
private fun handleChangeLang() {
findPreference<Preference>(getString(R.string.key_language))?.setOnPreferenceChangeListener { _, _ ->
// Restart application
activity?.restartActivity(MainActivity::class.java)
return@setOnPreferenceChangeListener true
}
}
private fun initUi() {
//findPreference<Preference>("user")?.hideIcon()
findPreference<Preference>("app")?.hideIcon()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment