Kotlin is a language and used for avoid null pointer exception
kotlin has defaultly uses lambadas for concise event handling code
Kotlin is a safer and more concise code for writing
Kotlin is a modern and it reduces the number of lines code in mobile application development
Here we can see the how the activity
MainActivity[MainActivity.kt]
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import com.androidfeeders.kotlinsampleapplication.R.id.bottonToast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//First Button Onclick Listener
bottonToast.setOnClickListener(this)
//Second Button Onclick Listener
val btn_Second_click_me = findViewById(R.id.onCreateToast) as Button
btn_Second_click_me.setOnClickListener{
Toast.makeText(this,"My Sample kotlin Programme",Toast.LENGTH_SHORT).show();
}
}
override fun onClick(p0: View?) {
Toast.makeText(this,"My Sample Kotlin Programme",Toast.LENGTH_SHORT).show();
}
}
Layout Resource[activity_main.xml]
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:fontFamily="cursive"
android:text="@string/app_name"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:id="@+id/bottonToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Invoke Toast"
android:layout_marginLeft="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
app:layout_constraintTop_toTopOf="@+id/textView" />
<Button
android:id="@+id/onCreateToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Invoke Toast"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
app:layout_constraintTop_toTopOf="@+id/textView"
app:layout_constraintLeft_toRightOf="@+id/bottonToast"
android:layout_marginLeft="80dp" />
</android.support.constraint.ConstraintLayout>
Gradle Scripts~build.gradle[Module:app]
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.androidfeeders.kotlinsampleapplication"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Gradle Scripts~build.gradle[Project:KotlinSampleApplication]
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.41'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Resource[strings.xml]
<resources>
<string name="app_name">Welcome To Kotlin</string>
</resources>
manifests
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidfeeders.kotlinsampleapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
See what's new in Android & IOS & Hybrid development … explore and learn in site