Padding strings in Kotlin for calendar weeks
val calendarWeek = 3 val calendarWeekString = calendarWeek.toString().padStart(2, ‘0’)
val calendarWeek = 3 val calendarWeekString = calendarWeek.toString().padStart(2, ‘0’)
Sending data private void openVideoActivity(String video) { Intent newActivity = new Intent(this, PlayerActivity.class); newActivity.putExtra(“videoId”, video); startActivity(newActivity); } Retrieving data final String videoID = getIntent().getExtras().getString(“videoID”); In Kotlin it looks like this: val intent = Intent(this, NewActivity::class.java).apply { putExtra(“EXTRA_MESSAGE”, “Test 123”) } startActivity(intent) Retrieving val message = intent.getStringExtra(“EXTRA_MESSAGE”) Further Reading Passing data between fragments using SafeArgs…
Code class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) Log.d(“LifecycleTest”, “onCreate”) } override fun onStart() { super.onStart() Log.d(“LifecycleTest”, “onStart”) } override fun onResume() { super.onResume() Log.d(“LifecycleTest”, “onResume”) } override fun onPause() { super.onPause() Log.d(“LifecycleTest”, “onPause”) } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) Log.d(“LifecycleTest”, “onSaveInstanceState”) } override fun onRestoreInstanceState(savedInstanceState: Bundle) { Log.d(“LifecycleTest”,…
Toast Toasts are simple message dialogs val text = “Hello toast!” val duration = Toast.LENGTH_SHORT val toast = Toast.makeText(applicationContext, text, duration) toast.show() Toast.makeText(this@MainActivity, “Hello Toast!”, Toast.LENGTH_SHORT).show()
In the last article we looked at functions in Kotlin. But there is more to functions: Multiple return values All you Pythonistas already know the concept of multiple return values. Kotlin as a strongly typed language uses the Pair class to enable functions to return two values or Triple to return three. fun nextTwo(num: Int)…
This time we look at functions. Let’s take a simple function from Java: public int mult(int num1, int num2){ return num1 * num2; } In Kotlin it would look like this: fun mult(num1: Int, num2: Int): Int{ return num1 * num2 } A function definition always starts with the keyword fun. That’s fun isn’t it?…
This time we deal with loops Loops Kotlin has two types of loops: for and while. For-loop for(x in 1..10){ println(“Loop: $x”) } We can count backwards as well for (x in 10 downTo 1) { println(“Loop: $x”) } While-loop var i = 10 while(i > 0) { println(“$i”) i– } Break & Continue for(x…
Conditionals This is a pretty quick one: Kotlin has the standard Java if / else if / else statements val age = 12 if (age < 5) { println(“Go to kindergarten”) } else if (age == 5) { println(“Go to pre-school”) } else if (age > 5 && age <= 17) { println(“Go to grade…
After Learning Kotlin – Part 1 and Learning Kotlin – Part 2 we will now dive into the ranges. One nice little feature is the .. syntax: you can generate an IntRange object just by specifying the start and the end element: val oneTo10 = 1..10 You can even use values for the start and…
In Learning Kotlin – Part 1 we looked at some basic concepts like type inference and string handling. In this part we deal with arrays: You can easily create an array with the arrayOf function var myArray = arrayOf(1, 1.23, “Jörn”) Element access You can access arrays directly with the [] operator println(myArray[2]) myArray[1] =…