Showing posts with label Kotlin Android. Show all posts

Kotlin Android Extensions is an important Kotlin plugin that is similar to Butterknife. It will allow to recover views in android. I...

Kotlin Android Extensions Kotlin Android Extensions

A blog about android developement

Kotlin Android


Kotlin Android Extensions is an important Kotlin plugin that is similar to Butterknife. It will allow to recover views in android.

Implementation

Add the following plugins in your app level build.gradle file
apply plugin:'com.android.application'
apply plugin:'kotlin-android'
apply plugin:'kotlin-android-extensions'

Coding Part

Create a new layout file in res folder, name it as activity_main.xml and paste the following codes
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.androidmads.kotlinsample_helloworld.MainActivity">

    <TextView
        android:id="@+id/helloTxtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <TextView
        android:id="@+id/helloAndroidExtension"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Hello World!" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:srcCompat="@mipmap/ic_launcher" />

</LinearLayout>
Create a new Kotlin file and name it as MainActivity.kt Import the following line, which is used to recover the views in android.
import kotlinx.android.synthetic.main.activity_main.*;
The following code refers that how to use Kotlin Extension. Here helloAndroidExtension is the Id of the textview and click listener for this textview is done as in below code
// Hello Program for Kotlin Android Extension 
// helloAndroidExtension is the id of the TextView
helloAndroidExtension.text = "Example for Kotlin Android Extension";
We can apply the same Approach with imageview by assigning ImageSource from Drawable folder and see the code below.
imageView.setImageResource(R.mipmap.ic_launcher_round)

Full Code

The following shows, how to use Kotlin Android extensions
package com.androidmads.kotlinsample_helloworld

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*;

class MainActivity : AppCompatActivity() {

    // Declaration
    var helloTextView: TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Initialization
        helloTextView = findViewById(R.id.helloTxtView) as TextView
        // Assigning data to TextView in runtime
        helloTextView!!.text = "Hello Kotlin!";

        // Click Listener in Kotlin
        helloTextView!!.setOnClickListener({
            Toast.makeText(applicationContext, "Hello TextView Clicked",
                    Toast.LENGTH_LONG).show();
        })

        // Hello Program for Kotlin Android Extension
        helloAndroidExtension.text = "Example for Kotlin Android Extension";

        imageView.setImageResource(R.mipmap.ic_launcher_round)
    }
}

Download Code:

You can download the full source for this tutorial from the following Github link. If you Like this tutorial, Please star it in Github.
    
Download From Github

Google officially announced Kotlin as a first class language for Android development at Google I/O 2017. From Android Studio 3.0, Kotl...

Hello World - Kotlin Hello World - Kotlin

A blog about android developement

Kotlin Android

Google officially announced Kotlin as a first class language for Android development at Google I/O 2017. From Android Studio 3.0, Kotlin is included as Support for Android Studio. In this post, we will start the development of Android Application with Kotlin.

To Getting Started, you have to download Android Studio 3.0 Canary 1 or Add the Kotlin plugin in your existing Android Studio.

Create Android Project with Kotlin

Create new project in Android studio check the Kotlin support and start as usual with Android Studio 3.0. 
Android Studio 3.0
But for Android Studio with version less than 3.0, we have install plugin form Plugins menu manually by Selecting File-->Settings-->Plugins-->Browse Repositories--> Search and Select Kotlin. Then Click Install

Now inside activity_main.xml create the following UI
<?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="com.androidmads.kotlinsample_helloworld.MainActivity">

    <TextView
        android:id="@+id/helloTxtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
In Android Studio 3.0, by default you have Kotin Activity. For Android Studio with Version less than 3.0, can convert their Java activity into Kotlin Activity.
Install Kotlin Plugin
  1. Select Tools --> Kotlin --> Configure Kotlin --> Click Sync Now. 
  2. Open Quick Search or Click Ctrl + Shift + A for Windows Users. 
  3. Search and Select convert java to kotlin or simply Selct Ctrl + Shift + Alt + K for Windows Users. 
Now your MainActivity.kt looks like in the following.
package com.androidmads.kotlinsample_helloworld

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    // Declaration
    var helloTextView: TextView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Initialization
        helloTextView = findViewById(R.id.helloTxtView) as TextView
        // Assigning data to TextView in runtime
        helloTextView!!.text = "Hello Kotlin!";

        // Click Listener in Kotlin
        helloTextView!!.setOnClickListener({
            Toast.makeText(applicationContext, "Hello TextView Clicked", Toast.LENGTH_LONG).show();
        })
    }
}
Kotlin is a new Programming Language for Android Development. I hope Java is not deprecated for Android Development. Please tell what do you think about this tutorial as well as the future of Java in Android development in Comment Section.