Prabhakar Thota
6 min readJan 18, 2017

--

Kotlin example app from play store
Settings > Plugins > Browse Repositories > Search Kotlin and install
Select a Java file > Hit Ctrl+Shift+A > “convert to kotlin” Hit enter
verticalLayout { 
val name = editText()
button("Say Hello") {
onClick { toast("Hello, ${name.text}!")
}
}
}
final SUPPORT_VERSION = '23.3.0'
final ANKO_VERSION = '0.8.3'
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile "com.android.support:appcompat-v7:${SUPPORT_VERSION}"
compile "com.android.support:recyclerview-v7:${SUPPORT_VERSION}"
compile "org.jetbrains.anko:anko-sdk15:${ANKO_VERSION}"
compile "org.jetbrains.anko:anko-appcompat-v7:${ANKO_VERSION}"
compile "org.jetbrains.anko:anko-recyclerview-v7:${ANKO_VERSION}"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

Example

Splash screen
Main screen
Menu
Web view using google custom tabs

SplashScreenActivity.kt

SplashScreenActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) // Start main activity
startActivity(Intent(this, MainActivity::class.java))
// close this activity
finish()
}
}
Variable declaration. int/string/boolean
var EMAIL_ID = “contact@myinnos.in
To open Web URL
browse(GIT_HUB_URL)
setText function
tvHeader.text = HEADER_TEXT
Hint to Edit Text
etName.hint = EDIT_TEXT_NAME_HINT
Toast
toast(“Activity restarted!”)

Email Intent
email(EMAIL_ID, “subject”)

Share Intent
share(“text”)

Function declaration, function to get text length of edit text
fun checkTextLength(editText: EditText): Boolean {
var length = editText.length()
if (length > 0)
return true
else
return false
}
onClick funtion
btDone.onClick {
hideKeyboard()
if (!checkTextLength(etName) || !checkTextLength(etMobile))
toast(“Fields cannot be empty!”)
else
onButtonClicks()
}

Dialog Aleart Box
fun openAlertDialog(name: String, phoneNumber: String) {
val countries = listOf(“Russia”, “India”, “USA”, “Japan”, “China”)
selector(“Where are you from?”, countries) {
i -> alert(“One more thing! You have entered this number “ +
phoneNumber, name + “! So you’re living in ${countries[i]},
right?”) {
customView {
verticalLayout {
positiveButton(“AWESOME!”) {
longToast(“Thank you!”)
}
}
}
}.show()
}
Initializing menu options
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
getMenuInflater().inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
val id = item!!.getItemId()
//noinspection SimplifiableIfStatement
if (id == R.id.action_rate) {
// opining browser intent
browse(PLAY_STORE_URL)
return true
}
return super.onOptionsItemSelected(item)
}
class MainActivity : AppCompatActivity() {var HEADER_TEXT = "You can try awesome example!"
var EDIT_TEXT_NAME_HINT = "enter name"
var EDIT_TEXT_NUMBER_HINT = "enter number"
var EMAIL_ID = "contact@myinnos.in"
var GIT_HUB_URL = "https://github.com/myinnos/Kotlin-Example"
var GIT_HUB_WEB_URL = "https://myinnos.github.io/Kotlin-Example/";
var PLAY_STORE_URL = "market://details?id=" + BuildConfig.APPLICATION_ID
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// setting header text
tvHeader.text = HEADER_TEXT
// setting hint for edit text
etName.hint = EDIT_TEXT_NAME_HINT
etMobile.hint = EDIT_TEXT_NUMBER_HINT
// setting drawable image to image view
imageView.resources.getDrawable(R.mipmap.ic_launcher)
// onclick event for image view to restart
imageView.onClick {
startActivity<SplashScreenActivity>()
finish()
toast("Activity restarted!")
}
// onclick event for button
btDone.onClick {
hideKeyboard()
if (!checkTextLength(etName) ||
!checkTextLength(etMobile))
toast("Fields cannot be empty!")
else
onButtonClicks()
}
btGitHubLink.onClick {
// opining browser intent
browse(GIT_HUB_URL)
}
btTutorial.onClick {
// google custom tabs
val builder = CustomTabsIntent.Builder()
builder.setToolbarColor(ContextCompat.getColor(this,
R.color.colorPrimary))
val customTabsIntent = builder.build()
customTabsIntent.launchUrl(this,
Uri.parse(GIT_HUB_WEB_URL))
}
}
// function to get text from edit text
fun EditText.textValue(): String {
return text.toString()
}
// function to get text length of edit text
fun checkTextLength(editText: EditText): Boolean {
var length = editText.length()
if (length > 0)
return true
else
return false
}
// function to hide keyboard
fun hideKeyboard() {
try {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE)
as InputMethodManager
imm.hideSoftInputFromWindow(currentFocus!!.windowToken,
0)
} catch (e: Exception) {
// TODO: handle exception
}
}fun onButtonClicks() {
//using function
val phoneNumber = etMobile.textValue()
// direct access
val name = etName.text.toString()
// calling alert dialog
openAlertDialog(name, phoneNumber)
}
fun openAlertDialog(name: String, phoneNumber: String) {
val countries = listOf(“Russia”, “India”, “USA”, “Japan”, “China”)
selector(“Where are you from?”, countries) {
i -> alert(“One more thing! You have entered this number “ +
phoneNumber, name + “! So you’re living in ${countries[i]},
right?”) {
customView {
verticalLayout {
positiveButton(“AWESOME!”) {
longToast(“Thank you!”)
}
}
}
}.show()
}
// Initializing menu options
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
getMenuInflater().inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
val id = item!!.getItemId()
//noinspection SimplifiableIfStatement
if (id == R.id.action_share) {
// sharing intent
share(getString(R.string.share_text) +
BuildConfig.APPLICATION_ID,
getString(R.string.app_name))
return true
} else if (id == R.id.action_feedback) {
// email intent
email(EMAIL_ID, getString(R.string.app_name))
return true
} else if (id == R.id.action_rate) {
// opining browser intent
browse(PLAY_STORE_URL)
return true
}
return super.onOptionsItemSelected(item)
}
}

--

--

Prabhakar Thota

Mobile Engineer, UI/UX. I believe in the quote, “Creativity is thinking up new things. Innovation is doing new things.” Happy Coding :)