Step 1. Create a class MainActivity.kt
class MainActivity : AppCompatActivity() {Step 2. create a layout activity_main.xml
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Set a click listener for button widget
button.setOnClickListener{
val builder = AlertDialog.Builder(this@MainActivity)
builder.setTitle("App background color")
builder.setMessage("Are you want to set the app background color to RED?")
builder.setPositiveButton("YES"){dialog, which ->
Toast.makeText(applicationContext,"Ok, we change the app background.",Toast.LENGTH_SHORT).show()
root_layout.setBackgroundColor(Color.RED)
}
builder.setNegativeButton("No"){dialog,which ->
Toast.makeText(applicationContext,"You are not agree.",Toast.LENGTH_SHORT).show()
}
builder.setNeutralButton("Cancel"){_,_ ->
Toast.makeText(applicationContext,"You cancelled the dialog.",Toast.LENGTH_SHORT).show()
}
val dialog: AlertDialog = builder.create()
dialog.show()
}
}
}
<?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:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Me Alert Dialog"
/>
</LinearLayout>