Skip to content

Instantly share code, notes, and snippets.

@hassansw
Created October 4, 2017 14:22
Show Gist options
  • Save hassansw/3d68627c75ea89e10e6ee46ddde19328 to your computer and use it in GitHub Desktop.
Save hassansw/3d68627c75ea89e10e6ee46ddde19328 to your computer and use it in GitHub Desktop.
Creating a Fragment with Kotlin
class FragmentHome : Fragment() {
//Any other widgets such as Button, Layouts and etc be declared also like this
var tvHomeLabel:TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
//Best practice in kotlin is to leave this section as it is
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater!!.inflate(R.layout.fragment_home, container, false)
}
//Best to manipulate Widgest in this method
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Find and attach the view from the specified layout
tvHomeLabel = view!!.findViewById<TextView>(R.id.tvHomeLabel) as TextView
//Now you can set the label as you wish -> enjoy
tvHomeLabel.setText("Hello From Fragment")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment