Skip to content

hanuor/android-kotlin-samples

Repository files navigation

Android + Java Kotlin

kotlin_250x250.png
Android Kotlin samples
Because converting code is too mainstream.


Get started with the new official language for Android app development - Kotlin.

Code samples, instructions and much more


Contents

Getting started

Android+Studio+3.0.png
As of now Kotlin based IDE hasn't officially launched yet. But we are developers eh! Follow this link to download Android studio 3.0 (Canary) :

Download

Android studio 3.0 preview
If you are working on a stable version then don't freak out. This version won't affect your existing work/settings/environment etc.

findViewById

Till now we were using

...
TextView textView = (TextView) findViewById(R.id.text_view);    
Button buttonView = (Button) findViewById(R.id.button_view);
... 

In Kotlin we do:

...
val textview = findViewById(R.id.text_view) as TextView   
val buttonView = findViewById(R.id.button_view) as Button
...

Method accessability remains to be same:

textview.setText("Hello World. This is Kotlin!")

Oh and did I mention you don't have to worry about semicolons? No? Well now you know.

onClickListeners

In Java we did

button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Toast.makeText(this, "Hey there!",
                Toast.LENGTH_LONG).show();
    }
});

Here is the Kotlin code:

button.setOnClickListener(object: View.OnClickListener{
            override fun onClick(v: View?) {
                Toast.makeText(this@MainActivity, "Hey there!", Toast.LENGTH_SHORT).show()
         }
 });

Notice that inside setOnClickListener method we are passing an object of View->OnClickListener (Interface). So instead of using new, in Kotlin we do object:
This is a short example but here Kotlin bags all the points when it comes to code readability.

Starting a new Activity

In Java we started a new Activity through Intents like:

Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);

In Kotlin we do:

 val trigger = Intent(this@MainActivity, SecondActivity::class.java)
 startActivity(trigger)

Here trigger is the Intent object. The second parameter SecondActivity::class.java looks fishy right? Well this is how we pass a reference to a class (in this case our activity) in Kotlin. See this link for more information: Class references in Kotlin

getIntent()

  val intent_get_data = intent
       val ssbat = intent.getStringExtra("yolo")

In Kotlin we don't use methods like getIntent(). Instead we have 'intent' which acts as a wrapper fpr these functions. Next we see that we don't have to specify the data type to store value from the received intent, just like in Python. This makes our code a hella more short,and enhances readability too.

AsyncTasks

To create an object of a class in Kotlin we used the object keyword. Similarly here also we'll use this to make object of our AyncTask class. Notice that you can copy and paste your Java code and Studio will automatically convert it to Kotlin. Kotlin code:

object: AsyncTask<Void, Void, String>(){
           override fun doInBackground(vararg params: Void?): String {
               var sampleJson: String =  "https://gist.githubusercontent.com/hanuor/c3a94602155d23e46daac9c18903899d/raw/ae5313ad810308dcfbfda2dda75bcee73c8830d6/sampleJson"
               //Do some task here
               try {
                   //some task here
                   Log.d("SecondActivity", " " + jsonObject)
               }catch (e : Exception){
                   e.printStackTrace()
               }
            return sampleJson
           }

           override fun onPostExecute(result: String?) {
               super.onPostExecute(result)
               Toast.makeText(this@SecondActivity, "HelloWorld" + result, Toast.LENGTH_SHORT).show()
           }
       }.execute()
object: AsyncTask<Void, Void, String>()

We are creating an object of AsyncTask task here. This takes three types of parameters <params, progress, result>. Next comes the overriding of the doInBackground function. Take a look if you're finding this line of code difficult to understand: Functions in Kotlin Next we have result: String? . So this means that we have a variable 'result' of String datatype. '?' is written here to allow 'result' to be null. See this for more reference. Null safety in Kotlin


About

Get started with the new official language to build apps on Android - Kotlin! (Android kotlin samples)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages