In view of the fact that today we celebrate Programmer Day, I decided to write a simple app for this event in Android Platform using Kotlin.
I will skip basics of creating an Android Project in IntelliJ IDEA. For this purpose I refer you to official Getting started with Android and Kotlin.
Just remember to make sure that in build.gradle file you have same version as in the plugin. If it is not same you will receive error: “Some Kotlin libraries attached to this project have unsupported format.Please update the libraries or the plugin”.
Here is a MainActivity:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.pawegio.kotlinsample.app import android.support.v7.app.ActionBarActivity import android.os.Bundle import java.util.Calendar import java.util.Date import android.widget.TextView public class MainActivity() : ActionBarActivity() { /** * type your name below */ val YOUR_NAME = "Paweł" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val textView = findViewById(R.id.textView) as TextView val todayView = findViewById(R.id.todayView) as TextView val cal = Calendar.getInstance() as Calendar val currDate = cal.getTime() as Date; val progDate = getProgrammerDayDate() if (isProgrammerDayToday(currDate, progDate)) { textView.setText("Happy Programmers Day $YOUR_NAME !!") } else { when (getDiff(currDate, progDate)) { in -100..-8 -> textView.setText("Programmers Day arrives soon.") in -7..-1 -> textView.setText("Programmers Day is so close...") else -> textView.setText("There is a long time till the next Programmers Day.") } } todayView.setText(getTodayText(cal)) } fun getProgrammerDayDate(): Date { var cal = Calendar.getInstance() as Calendar cal.set(Calendar.DAY_OF_MONTH, 13) cal.set(Calendar.MONTH, Calendar.SEPTEMBER) return cal.getTime() as Date } fun isProgrammerDayToday(currDate: Date, progDate: Date) = currDate.compareTo(progDate) == 0 fun getDiff(date: Date, progDate: Date) = date.compareTo(progDate) fun getTodayText(cal: Calendar): String { val dayOfYear = cal.get(Calendar.DAY_OF_YEAR) - 1 val binaryDay = Integer.toBinaryString(dayOfYear) return "Today is:\n$binaryDay" } } |
and layout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.pawegio.kotlinsample.app.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:textSize="30sp" /> <TextView android:id="@+id/todayView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:textStyle="bold" android:textSize="30sp" /> </LinearLayout> |
Hope you enjoy your day! 🙂
I attach a screenshot: