Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make buttons clickable in the bottom sheet fragment?

**The flow: ** There are two xml files:

activity_main.xml
activity_main.xml

bottomsheet_fragment.xml
bottomsheet_fragment.xml

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to click on "click here" button (xml id: btn_show) and show the bottom sheet fragment [This is happening correctly) .. corresponding files: activity_main.xml, MainActivity.kt

Now I want to click on the button in the bottom sheet(xml id: btn_button1), and show the text you pressed button 1 via toast(or anything else is fine too, I just want to show something when I click the button) [This part is not happenning correctly) .. corresponding files: bottomsheet_fragment.xml, BottomSheetFragment.kt

I am attaching codes for the files below:

bottomsheet_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="Sample button 1"
        />

</LinearLayout>

BottomSheetFragment.kt

package android.example.naruto

import android.example.naruto.databinding.ActivityMainBinding
import android.example.naruto.databinding.BottomsheetFragmentBinding
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class BottomSheetFragment: BottomSheetDialogFragment() {

    private lateinit var binding2: BottomsheetFragmentBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding2 = BottomsheetFragmentBinding.inflate(layoutInflater)

        binding2.btnButton1.setOnClickListener{
            Toast.makeText(context, "You pressed on button 1!", Toast.LENGTH_SHORT).show()
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

main_activity.kt

package android.example.naruto

import android.example.naruto.databinding.ActivityMainBinding
import android.example.naruto.databinding.BottomsheetFragmentBinding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    private lateinit var binding1: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding1 = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding1.root)

        val bottomSheetFragment= BottomSheetFragment()

        binding1.btnShow.setOnClickListener {
           bottomSheetFragment.show(supportFragmentManager, "BottomSheetDialog")
        }
    }
}

Thanks in advance for the help! Please let me know if you need any more details or clarifications.

I referred to these tutorials: https://www.youtube.com/watch?v=yqnVPiWAw0o&t=21s, https://www.youtube.com/watch?v=4GXflIdrlus

>Solution :

I am not able to see onCreateView method in BottomSheetFragment and probably that’s why it is not working

You can change your code like this

From

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding2 = BottomsheetFragmentBinding.inflate(layoutInflater)

        binding2.btnButton1.setOnClickListener{
            Toast.makeText(context, "You pressed on button 1!", Toast.LENGTH_SHORT).show()
        }
    }

To

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding2 = BottomSheetFamilyTreeBinding.inflate(inflater, container, false)
        return binding2.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding2.btnButton1.setOnClickListener{
            Toast.makeText(context, "You pressed on button 1!", Toast.LENGTH_SHORT).show()
        }
        
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading