Skip to content

Instantly share code, notes, and snippets.

@ZahidRasheed
Forked from MizzleDK/Kotlin interview code test.kt
Last active July 21, 2022 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ZahidRasheed/2d225e42bf4785016c56da11727126c0 to your computer and use it in GitHub Desktop.
Save ZahidRasheed/2d225e42bf4785016c56da11727126c0 to your computer and use it in GitHub Desktop.
open class AwesomeFragment(var name: String): Fragment(), TextWatcher {
public var textToPrependOnTVAfterTextChanged = "Hello "
init {
// Make sure we don't crash if name is null
name = name ?: ""
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate our awesome layout
return View.inflate(activity!!, R.id.awesome_layout, null)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val textView = view.findViewById<TextView>(R.layout.textView1)
with (textView) {
// We want to prepend some awesome text after the initial use of setText()
addTextChangedListener(this@AwesomeFragment)
TV = this
}
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
// Set text
TV.apply {
setText("${name}")
}
}
override fun onPause() {
super.onResume()
// Clean up our references to the TextView field
TV.removeTextChangedListener(this)
TV = null
}
override fun afterTextChanged(s: Editable?) {
TV?.apply {
text = textToPrependOnTVAfterTextChanged + s.toString()
}
}
fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { /* Ignored */ }
fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { /* Ignored */ }
companion object {
lateinit var TV: TextView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment