Search code examples
javaandroidkotlinandroid-bottomsheetdialog

How to navigate from bottomsheet to activity


I have a bottomsheet implemented in my app. I used inflater to show the bottomsheet dialog, here is my code in NewSignUpActivity to show the bottomsheet dialog.

class NewSignUpActivity : AppCompatActivity() {

    private lateinit var btnBottomSheet: RelativeLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_rider_or_bike)

        btnBottomSheet = findViewById(R.id.riderBtn)
        val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback(){
            override fun onStateChanged(bottomSheet: View, newState: Int) {}
            override fun onSlide(bottomSheet: View, slideOffset: Float) {}
        }
        val bottomSheetView = layoutInflater.inflate(R.layout.layout_bottom_sheet, null)
        val bottomSheetDialog = BottomSheetDialog(this)
        bottomSheetDialog.setContentView(bottomSheetView)
        val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView.parent as View)
        bottomSheetBehavior.setBottomSheetCallback(bottomSheetCallback)

        btnBottomSheet.setOnClickListener {
            bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
            bottomSheetDialog.show()
        }


    }
}

Here's how it looks like in view.

enter image description here

As you can see, there's button on the bottomsheet I want to navigate to another layout activity once I clicked the YES button, how can I do that? I am very new to android development using Kotlin.

EDITED: Added my XML file for bottomsheet dialog please see below._ name of file is layout_bottom_sheet.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layoutBtmSheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/rectangle"
   >


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.AppCompatImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="@dimen/_20sdp"
            android:layout_marginRight="@dimen/_15sdp"
            android:layout_marginBottom="@dimen/_30sdp"
            android:src="@drawable/close_black" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="52dp"
        android:background="@drawable/bgwhite"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/ll_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/_30sdp"
            android:layout_marginTop="@dimen/_20sdp"
            android:layout_marginRight="@dimen/_10sdp"
            android:orientation="vertical"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.AppCompatImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:src="@drawable/outline_sports_motorsports_black_48"
                tools:layout_editor_absoluteX="3dp"
                tools:layout_editor_absoluteY="3dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_10sdp"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/ll_one"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/hello_there_partner_rider"
                android:textSize="18sp"
                android:textStyle="bold" />

            <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="@dimen/_5sdp"
                android:text="@string/lorem_ipsum" />

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout">

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_100sdp"
        android:layout_marginBottom="@dimen/_10sdp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.49"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout3"
        app:layout_constraintVertical_bias="0.678">

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/noBtn"
            android:layout_width="169dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/bgbtn1"
            android:radius="@dimen/_50sdp"
            android:text="No" />

        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/yellowBtn"
            android:layout_width="169dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:textColor="@color/white"
            android:background="@drawable/bgbtno"
            android:text="@string/yes" />
    </LinearLayout>

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"
       android:textColor="@color/reply_black_800"
        android:text="@string/are_you_using_your_own_motorcycle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        app:layout_constraintVertical_bias="0.074" />


</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Already the BottomSheetView layout is inflated with:

    val bottomSheetView = layoutInflater.inflate(R.layout.layout_bottom_sheet, null)
    

    And this layout hosts the YES button, so you can just use findViewById(). Assuming that the id of the YES button is btn_yes:

    val btnYes = bottomSheetView.findViewById<Button>(R.id.btn_yes)
    
    btnYes.setOnClickListener {
        startActivity(myIntent) // Add your activity intent
    }
    

    UPDATE:

    As you provided the id of the YES button which is yellowBtn, then you can use it:

    val btnYes = bottomSheetView.findViewById<Button>(R.id.yellowBtn)
    btnYes.setOnClickListener {
        startActivity(myIntent) // Add your activity intent
    }