btn start activity
// 1. Import necessary packages
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 2. Set click listener for the button
btn_start.setOnClickListener {
// 3. Define the intent to start the activity
val intent = Intent(this, YourActivityName::class.java)
// 4. Start the activity using the intent
startActivity(intent)
}
}
}
Explanation:
1. Import necessary packages: Import required classes from Android and Kotlin libraries.
2. Set click listener for the button: Assign a click listener to the btn_start
button, typically defined in the XML layout file.
3. Define the intent to start the activity: Create an Intent
object to specify the transition from the current activity to another (YourActivityName
is a placeholder for the name of the activity you want to start).
4. Start the activity using the intent: Use the startActivity()
method with the created Intent
object to initiate the transition to the specified activity.