Kotlin button onClickListener event inside a fragment

37,321

Solution 1

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
    }

Solution 2

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

Solution 3

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
}
Share:
37,321

Related videos on Youtube

Axel Ros
Author by

Axel Ros

Updated on May 31, 2020

Comments

  • Axel Ros
    Axel Ros about 4 years

    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()
            }
        }
    
    }
    
  • Elliot
    Elliot almost 4 years
    This worked for me. Before I refactored mainactivity into a fragment my listener was in onCreateView. When I moved the code into a fragment it caused a null pointer exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$O‌​nClickListener)' on a null object reference Moved to onViewCreated and it worked :)