Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 922
Axel Ros Avatar asked Aug 03 '18 12:08

Axel Ros


People also ask

How many ways can you attach OnClickListener to button layout?

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.

Which are the ways to set OnClickListener for button in android?

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.

What is set OnClickListener?

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.


2 Answers

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")
    }
}
like image 177
Hoguwart Avatar answered Sep 20 '22 07:09

Hoguwart


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
    }
like image 37
Levi Moreira Avatar answered Sep 18 '22 07:09

Levi Moreira