I'm trying to catch the onClick event from a button inside a fragment but it's not working.
Any tip?
I have this main activity and I call the fragment throught a bottomNavigation. MainActivity.kt:
class MainActivity : FragmentActivity() {
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
showFragmentSetup()
return@OnNavigationItemSelectedListener true
}
}
false
}
fun showFragmentSetup(){
val setupFragment = SetupFragment()
val manager = supportFragmentManager
val transaction = manager.beginTransaction()
transaction.replace(R.id.setupFragment, setupFragment)
transaction.addToBackStack(null)
transaction.commit()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
}
}
The activity_main.xml is the container of the linearLayout which will cointain the fragment.
activity_main.xml
<LinearLayout
android:id="@+id/setupFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
My fragment is simple it just have a button and i want to catch the onClickEvent from this button
class SetupFragment : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_setup, container, false)
val view: View = inflater!!.inflate(R.layout.fragment_setup, container, false)
btnSetup.setOnClickListener { view ->
Log.d("btnSetup", "Selected")
}
// Return the fragment view/layout
return view
}
companion object {
fun newInstance(): SetupFragment {
return SetupFragment()
}
}
}
Using an OnClickListener There are two ways to do this event handler programmatically : Implementing View. OnClickListener in your Activity or fragment. Creating new anonymous View.
To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.
I think you should use "onViewCreated" function in your "SetupFragment"
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btnSetup.setOnClickListener { view ->
Log.d("btnSetup", "Selected")
}
}
You're returning before you can setup the listener here:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_setup, container, false)
val view: View = inflater!!.inflate(R.layout.fragment_setup, container, false)
btnSetup.setOnClickListener { view ->
Log.d("btnSetup", "Selected")
}
// Return the fragment view/layout
return view
}
Try like this:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view: View = inflater!!.inflate(R.layout.fragment_setup, container, false)
view.btnSetup.setOnClickListener { view ->
Log.d("btnSetup", "Selected")
}
// Return the fragment view/layout
return view
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With