Activity screen transition using Android Kotlin custom dialog Intent

135 views Asked by At

I want to write code that moves the screen when a button is clicked through a custom dialog. startActivity not working in custom dialog class. I want to move other Activity when I click button on Custom Dialog How can I navigate to another activity screen?

custom_dialog.kt

    class CustomDialog(context: Context) {
    private val dialog = Dialog(context)

    fun myDig(page : Context ,result: String){

    dialog.setContentView(R.layout.custom_dialog_find)

    val text =dialog.findViewById<TextView>(R.id.result_textview)
    val btn_yes = dialog.findViewById<Button>(R.id.yesButton)
    val btn_no = dialog.findViewById<Button>(R.id.noButton)

    text.text = "${result} is me"

    btn_yes.setOnClickListener {
        Toast.makeText(page, "Move to A Activity", Toast.LENGTH_SHORT).show()
        /*val intent = Intent(page, LoginActivity::class.java)
        startActivity(intent)
        finish()*/
        dialog.dismiss()
    }
    btn_no.setOnClickListener {
        Toast.makeText(page, "Move to B Activity", Toast.LENGTH_SHORT).show()
        dialog.dismiss()
    }
    dialog.window!!.setLayout(WindowManager.LayoutParams.MATCH_PARENT, 
    WindowManager.LayoutParams.WRAP_CONTENT)
    dialog.setCanceledOnTouchOutside(true)
    dialog.setCancelable(true)
    dialog.show()
    }
   }

MainActivity.kt

 val custom_dialog = CustomDialog(this@MainActivity)
 custom_dialog.myDig(this@MainAcitivity, result)

custom_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:minWidth="300dp"
    android:orientation="vertical"
    android:padding="20dp"
    android:background="@drawable/attacth_btn">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ico_info"/>

<TextView
    android:id="@+id/result_textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:minHeight="50dp"
    android:textSize="20sp"
    android:padding="25dp"
    android:textStyle="bold"
    tools:text="hello result" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="horizontal">

    <Button
        android:id="@+id/noButton"
        android:background="@drawable/delete_btn"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_marginEnd="7dp"
        android:layout_weight="1"
        android:text="move to B activity"
        android:textSize="16sp"
        android:textStyle="bold"/>

    <Button
        android:id="@+id/yesButton"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:background="@drawable/selected_btn"
        android:text="move to A activity"
        android:textSize="16sp"
        android:textStyle="bold"/>
    </LinearLayout>
</LinearLayout>



 
1

There are 1 answers

1
Divyang Diwasaliwala On

Dialog does not contains startActivity() method. you need Activity context to call startActivity();

Start the activity using context from customdialog class

Try this:

btn_yes.setOnClickListener {
    Toast.makeText(page, "Move to A Activity", Toast.LENGTH_SHORT).show()
    val intent = Intent(page, LoginActivity::class.java)
    context.startActivity(intent)
    (context as Activity).finish()
    dialog.dismiss()
}