Reputation: 301
I Have an activity fragment whereby i have it call out values from a separate data class and also a .xml file
I want the button in the .xml file to be accessible in the Kotlin CLASS.however, an error keeps showing that the declared function is unreachable. What i have done:
the kotlin class page:
class HomeFragment : Fragment() {
private val personcolletionref =Firebase.firestore.collection( "users")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
submitbutton.setOnClickListener {
}
}
private fun savePerson (person: Person) = CoroutineScope(Dispatchers.IO).launch {
try {
personcolletionref.add(person).await()
withContext(Dispatchers.Main) {
Toast.makeText(activity, "Successful", Toast.LENGTH_LONG).show()
}
}catch (e: Exception) {
withContext(Dispatchers.Main) {
Toast.makeText(activity, e.message, Toast.LENGTH_LONG).show()
}
}
}
}
the button in the xml file to be called:
<Button
android:id="@+id/submitbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="268dp"
android:text="Submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/edittextage"
app:layout_constraintStart_toStartOf="@+id/edittextage" />
Upvotes: 1
Views: 118
Reputation: 391
In the onCreateView method, you just have to return the view. Override the following method and write your button on click listener init
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
submitbutton.setOnClickListener {
}
}
Upvotes: 2