MainActivity.kt
package com.cfsuman.kotlinexamples
import android.content.Context
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Configure action bar
setSupportActionBar(toolbar)
val actionBar = supportActionBar
actionBar?.title = "Hello Toolbar"
// Initialize the action bar drawer toggle instance
val drawerToggle:ActionBarDrawerToggle = object : ActionBarDrawerToggle(
this,
drawer_layout,
toolbar,
R.string.drawer_open,
R.string.drawer_close
){
override fun onDrawerClosed(view:View){
super.onDrawerClosed(view)
//toast("Drawer closed")
}
override fun onDrawerOpened(drawerView: View){
super.onDrawerOpened(drawerView)
//toast("Drawer opened")
}
}
// Configure the drawer layout to add listener and show icon on toolbar
drawerToggle.isDrawerIndicatorEnabled = true
drawer_layout.addDrawerListener(drawerToggle)
drawerToggle.syncState()
// Set navigation view navigation item selected listener
navigation_view.setNavigationItemSelectedListener{
when (it.itemId){
R.id.action_cut -> toast("Cut clicked")
R.id.action_copy -> toast("Copy clicked")
R.id.action_paste -> toast("Paste clicked")
R.id.action_new ->{
// Multiline action
toast("New clicked")
drawer_layout.setBackgroundColor(Color.RED)
}
}
// Close the drawer
drawer_layout.closeDrawer(GravityCompat.START)
true
}
}
// Extension function to show toast message easily
private fun Context.toast(message:String){
Toast.makeText(applicationContext,message,Toast.LENGTH_SHORT).show()
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:fitsSystemWindows="true"
/>
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello Drawer Navigation Example!"
android:layout_gravity="center"
android:textSize="30sp"
android:layout_margin="25dp"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/drawer_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
android:background="#ebe5ff"
>
<TextView
android:id="@+id/drawer_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Drawer Header"
android:padding="50dp"
android:textSize="25sp"
android:textStyle="bold"
android:background="#dcfffe"
/>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/drawer_menu"
/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
res/menu/drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@+id/action_cut"
android:title="Cut"
android:icon="@drawable/ic_action_cut"
/>
<item
android:id="@+id/action_copy"
android:title="Copy"
android:icon="@drawable/ic_action_copy"
/>
<item
android:id="@+id/action_paste"
android:title="Paste"
android:icon="@drawable/ic_action_paste"
/>
<item
android:id="@+id/action_new"
android:title="New"
android:icon="@drawable/ic_action_new"
/>
</menu>
res/values/strings.xml
<resources>
<string name="app_name">Kotlin - Navigation Drawer Example</string>
<string name="drawer_open">Open</string>
<string name="drawer_close">Close</string>
</resources>
res/values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>



- kotlin - Extension function example
- kotlin - NumberPicker example
- kotlin - GridView with BaseAdapter example
- kotlin - ListView ViewHolder example
- kotlin - Resize a bitmap
- kotlin - Resize bitmap keep aspect ratio
- kotlin - Rotate a bitmap
- kotlin - Enum example
- kotlin - HashMap example
- kotlin - File provider image share internal storage