Skip to content

Instantly share code, notes, and snippets.

@abdul-rehman-2050
Created November 1, 2017 12:16
Show Gist options
  • Save abdul-rehman-2050/1495783b44e5f8cea341253819ff25a9 to your computer and use it in GitHub Desktop.
Save abdul-rehman-2050/1495783b44e5f8cea341253819ff25a9 to your computer and use it in GitHub Desktop.
package com.a2050.rehman.abdul.dialogtest
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import android.content.DialogInterface
import android.R.array
class DialogSimpleActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val builder = AlertDialog.Builder(this,R.style.Base_Theme_AppCompat_Dialog_Alert)
builder.setTitle("Hello Dialog")
builder.setPositiveButton("OK",null)
builder.setMessage("This is hello message")
builder.setNegativeButton("Cancel",null)
builder.setNeutralButton("Maybe",null)
btn1.setOnClickListener {
Toast.makeText(applicationContext,"btn1 Clicked",Toast.LENGTH_LONG).show()
builder.show()
}
btn2.setOnClickListener{
showDialog()
}
}
fun showDialog() {
val builder = AlertDialog.Builder(this@MainActivity)
builder.setTitle("Select Font")
//list of items
val items = resources.getStringArray(R.array.Font_Names)
builder.setSingleChoiceItems(items, 0
) { dialog, which ->
// item selected logic
}
val positiveText = getString(android.R.string.ok)
builder.setPositiveButton(positiveText,
object:DialogInterface.OnClickListener {
override fun onClick(dialog:DialogInterface, which:Int) {
// positive button logic
}
})
val negativeText = getString(android.R.string.cancel)
builder.setNegativeButton(negativeText,
object:DialogInterface.OnClickListener {
override fun onClick(dialog:DialogInterface, which:Int) {
// negative button logic
}
})
val dialog = builder.create()
// display dialog
dialog.show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment