Axel Ros
Axel Ros

Reputation: 775

Kotlin button onClickListener event inside a fragment

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()
        }
    }

}

Upvotes: 18

Views: 48557

Answers (4)

Seotsa Abram Makaota
Seotsa Abram Makaota

Reputation: 11

This is how to go about in viewBinding after setting your View.Oclicklisterner interface in your fragment

override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View {

    _binding = FragmentMenuBinding.inflate(inflater, container, false)
    val root: View = binding.root

    _binding?.tvPlattersChip?.setOnClickListener(this)
    _binding?.tvPizzaChip?.setOnClickListener(this)
   
    return root

}

Upvotes: 1

Rahul ShaRma
Rahul ShaRma

Reputation: 99

You're not providing that button a view

lateinit var mView: View
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    mView=inflater.inflate(R.layout.product_list,container,false)

    mView.addProduct.setOnClickListener {

        val intent=Intent(activity,ProductAddActivity::class.java)
        startActivity(intent)
    }
    return mView
}

Upvotes: 3

Hoguwart
Hoguwart

Reputation: 141

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")
    }
}

Upvotes: 14

Levi Moreira
Levi Moreira

Reputation: 12005

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
    }

Upvotes: 19

Related Questions