Posts

Showing posts from November, 2018

Kotlin - Display Date picker dialog using EditText

Image
Here is how you can display a date picker dialog using Kotlin in Android. Result Code val c = Calendar.getInstance() val year = c.get(Calendar. YEAR ) val month = c.get(Calendar. MONTH ) val day = c.get(Calendar. DAY_OF_MONTH ) val datePicker = DatePickerDialog( this, DatePickerDialog.OnDateSetListener { view , year , monthOfYear , dayOfMonth -> // Display Selected date in textbox edittext_user_appointment_date .setText( "" + dayOfMonth + ", " + monthOfYear + ", " + year) } , year , month , day) datePicker .show()

How to write to a file in Kotlin

Image
Below is how to write a to a csv file in Kotlin. We have a class of clinitians with their assistants, contact, location e.t.c. We are going to print each clinitians data into a sorted list then write into a file then open in excel or numbers.                                                               SOURCE CODE Below is the code to write to csv file. import java.io.FileWriter import java.io.IOException import java.util.* fun main (args: Array<String>){ var allClinitiansList = arrayListOf <Clinitian>() //create clinitian var clinitian1 = Clinitian( "Yahya Borislav, NP" , "Arron Alfhard" , "237-8034" , "42172" , "Los Angeles East" ) var clinitian2 = Clinitian( "Uzoma Petra , MD" , "Gunter Viktoria" , "237-8802" , "4200" , "Miami west" ) var clinitian3 = Clinitian( "Slavica Kamal, NP" , "Edison Sladjana" , "237-8629&qu

Android send data from one activity to the next. Kotlin

Sending or passing data from one activity to the next is common in Android. Below is a sample code to send a piece of string data and receive it on the next activity using Kotlin. Send data to Activity2.kt var dataString = "Cabbage" val intent = Intent(this@MainActivity, Activity2::class.java) intent.putExtra("FOOD", dataString) startActivity(intent) Receive data on Activity2.kt val dataStringPassed = intent.getStringExtra("FOOD")

Kotlin Alertdialog Code

Image
Below is sample code for displaying an Alertdialog using Kotlin. It has 3 button click listeners. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setTitle("Kotlin Alert Dialog") showDialog("Kotlin Sample Code", "Alertdialog") } private fun showDialog(title: String, message: String){ // Late initialize an alert dialog object lateinit var dialog: AlertDialog // Initialize a new instance of alert dialog builder object val builder = AlertDialog.Builder(this) // Set a title for alert dialog builder.setTitle(title) // Set a message for alert dialog builder.setMessage(message) // On click listener for dialog buttons val dialogClickListener = DialogInterface.OnClickListener{ _, which -> when(which){