Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

hanuor/android-kotlin-samples

Repository files navigation

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

...TextViewtextView = (TextView)findViewById(R.id.text_view);ButtonbuttonView = (Button)findViewById(R.id.button_view);...

In Kotlin we do:

...val textview= findViewById(R.id.text_view)asTextViewval buttonView= findViewById(R.id.button_view)asButton...

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(newOnClickListener() {publicvoidonClick(Viewv) {Toast.makeText(this,"Hey there!",Toast.LENGTH_LONG).show();    }});

Here is the Kotlin code:

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

Notice that insidesetOnClickListener method we are passing anobject of View->OnClickListener (Interface). So instead of usingnew, in Kotlin we doobject:
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:

Intentintent =newIntent(this,SecondActivity.class);startActivity(intent);

In Kotlin we do:

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

Heretrigger is the Intent object.The second parameterSecondActivity::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= intentval ssbat= intent.getStringExtra("yolo")

In Kotlin we don't use methods likegetIntent(). 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 theobject 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>(){overridefundoInBackground(varargparams:Void?):String {var sampleJson:String="https://gist.githubusercontent.com/hanuor/c3a94602155d23e46daac9c18903899d/raw/ae5313ad810308dcfbfda2dda75bcee73c8830d6/sampleJson"//Do some task heretry {//some task hereLog.d("SecondActivity",""+ jsonObject)               }catch (e:Exception){                   e.printStackTrace()               }return sampleJson           }overridefunonPostExecute(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 thedoInBackground function. Take a look if you're finding this line of code difficult to understand:Functions in KotlinNext we haveresult: 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


[8]ページ先頭

©2009-2025 Movatter.jp