De­vel­op­ing a Sim­ple An­droid Ap­pli­ca­tion Us­ing Kotlin

Kotlin is a stat­i­cally typed pro­gram­ming lan­guage spe­cially de­signed for mod­ern multi-plat­form ap­pli­ca­tions. Ex­per­i­ment and use it to be­come the proud cre­ator of your own An­droid app by fol­low­ing the sim­ple in­struc­tions in this ar­ti­cle.

OpenSource For You - - Contents -

An­droid, which was de­clared open source from the year 2007, works on Java and C++ as its of­fi­cial lan­guages. In May 2017, at the Google I/O, it was an­nounced that Kotlin, a stat­i­cally typed pro­gram­ming lan­guage that is 100 per cent in­ter­op­er­a­ble with Java, was also an of­fi­cial lan­guage of An­droid.

For cur­rent An­droid devel­op­ers, Kotlin is a chance to use an An­droid-cen­tric lan­guage to solve the usual prob­lems, such as run­time ex­cep­tions and Java’s lengthy cod­ing. Google as­sures con­tin­ued sup­port for Java, which means that your ex­ist­ing cod­ing pat­terns are pre­served. Kotlin, on the other hand, is an easy so­lu­tion for a be­gin­ner to get started with An­droid’s own pro­gram­ming lan­guage.

Con­fig­ur­ing Kotlin

An­droid Stu­dio 3.0 comes with pre-in­stalled Kotlin. For all other ver­sions below 3.0, the user needs to in­stall Kotlin man­u­ally as fol­lows:

Open Set­tings (Pref­er­ences in Mac).

Se­lect Plu­g­ins and search for Kotlin (Fig­ure 1).

Hit In­stall to add Kotlin to your An­droid Stu­dio.

Now, if the An­droid Stu­dio ver­sion is not 3.0, we need to add the fol­low­ing code into the build.gra­dle (Project: MyAp­pli­ca­tion) file (lo­cated at the bot­tom of the left side of the Project win­dow, in­side Gra­dle Scripts), un­der the de­pen­den­cies sec­tion.

class­path “org.jet­brains.kotlin:kotlin-gradle­plu­gin:$kotlin_ver­sion” class­path “org.jet­brains.kotlin:kotlin-an­droidex­ten­sions:$kotlin_ver­sion”

Also add the Kotlin ver­sion into the build­script sec­tion, as fol­lows:

ext.kotlin_ver­sion = ‘1.1.x’

Now, add the fol­low­ing line to the build.gra­dle (Mod­ule: app) file, as the sec­ond line, fol­lowed by the com.an­droid. ap­pli­ca­tion plugin:

ap­ply plugin: ‘kotlin-an­droid’

Creat­ing a new project with Kotlin

An­droid Stu­dio 3.0 users can di­rectly se­lect Kotlin as their de­fault lan­guage while creat­ing a new project; as usual,

se­lect File > New > New Project. The slide ask­ing for the project’s name will have an op­tion un­der­neath, ‘In­clude Kotlin Sup­port’. Check­ing the box will en­able Kotlin, and this is the start­ing ac­tiv­ity — the MainAc­tiv­ity (de­fault name) named MainAc­tiv­ity.kt. For other ver­sions, the user needs to con­vert the ex­ist­ing Java class to Kotlin, by open­ing the MainAc­tiv­ity.java file, and then se­lect­ing Code

> Con­vert Java file to Kotlin file (Fig­ure 2).

Fig­ure 3 shows the dif­fer­ences be­tween Java class and Kotlin class. The fol­low­ing dif­fer­ences can be noted:

1. The ex­ten­sion has changed from .Java to .kt.

2. ex­tends Ap­pCom­patAc­tiv­ity is changed to: Ap­pCom­patAc­tiv­ity(). In Kotlin, the dec­la­ra­tion of a vari­able name is fol­lowed by a ‘:’, af­ter which the datatype is spec­i­fied.

3. ‘over­ride’ be­comes part of the onCreate func­tion and the func­tion syn­tax is changed to ‘fun onCreate’ in Kotlin. Un­like

Java, Kotlin re­quires ex­plicit an­no­ta­tions for over­rid­able mem­bers for over­rides.

We can see a con­sid­er­able dif­fer­ence in the Java and Kotlin syn­tax. Kotlin is a sim­ple and easy-to-code lan­guage, al­low­ing users to skip the headache of ver­bosity.

Cod­ing with Kotlin: The lay­out de­sign

Let’s cre­ate our sim­ple ap­pli­ca­tion, which will print ‘Wel­come to OSFY’ on the screen. Here we need a Tex­tView, which we will re­fer to and set a text to it. Let’s now open the lay­out file, which is lo­cated at app > res > lay­out > ac­tiv­i­ty_­main.xml (the de­fault name). Fig­ure 4 shows the lo­ca­tion.

Here, we can see that a Tex­tView has al­ready been added to the lay­out file. Switch to the ‘De­sign’ sec­tion from the Tex­tView (this can be changed at the bot­tom part of the dis­played win­dow, con­tain­ing code). Next, se­lect the Tex­tView from the Com­po­nent Tree win­dow (Fig­ure 5).

In Fig­ure 5, on the left, we have the Pal­ette win­dow, where we can se­lect and add more wid­gets to the ap­pli­ca­tion screen. To the right, we have the Prop­er­ties win­dow, where we can make prop­erty changes to the se­lected Tex­tView.

The most im­por­tant prop­erty is the ID, so pro­vide a suit­able name, or rather, set the ID value as ‘tex­tview’ it­self for easy iden­ti­fi­ca­tion. This ID is used to uniquely iden­tify the par­tic­u­lar el­e­ment in our Java/Kotlin code. We can

also view many other prop­er­ties, such as set­ting size, giv­ing colour, etc, by se­lect­ing the ‘View all prop­er­ties’ link at the bot­tom of the Prop­er­ties win­dow.

Cod­ing with Kotlin: The Java/Kotlin part

In nor­mal Java cod­ing, we need the fol­low­ing code to re­fer to a Tex­tView, and for set­ting a text to the above added Tex­tView:

Tex­tView t; t=(Tex­tView) find­ViewById(R.id.tex­tview); t.setText(“Wel­come to OSFY”);

The above code is used to set a text to the Tex­tView el­e­ment, with the ID ‘tex­tview’ added to the lay­out file, in Java cod­ing.

In Kotlin, the fol­low­ing code is used:

val t: Tex­tView = find­ViewById(R.id.tex­tview) as Tex­tView; t.setText(“Wel­come to OSFY”);

Here we can see the changes in the cod­ing, while us­ing Java and Kotlin. Also note that if there is any ob­ject we de­clare in the code, which has a null value as de­fault, it will not com­pile in Kotlin. That is:

//Java String t; t=null;

//Kotlin val t: String = null

The above code will ex­e­cute in Java, but will not com­pile in Kotlin. This is be­cause Kotlin pro­vides ‘null safety’ or, in other words, Kotlin marks an end to the long-time prob­lem of Nul­lPoin­t­erEx­cep­tion.

Run­ning your first project us­ing Kotlin

In or­der to run the ap­pli­ca­tion, we need to click the Run but­ton lo­cated on the top panel of An­droid Stu­dio. A win­dow to choose the de­vice will ap­pear (Fig­ure 6). We can either use an An­droid vir­tual de­vice, or an An­droid de­vice con­nected to the com­puter.

If you are us­ing an An­droid de­vice, then you must en­able de­vel­oper op­tions in it, be­fore en­abling An­droid De­bug­ging in the menu op­tions.

Kotlin and Oreo

An­droid re­cently an­nounced its next An­droid ver­sion, An­droid Oreo, along with sparkling new de­vel­op­ment fea­tures. These in­clude down­load­able fonts, font sup­port in XML, emoji com­pat­i­bil­ity, auto siz­ing Tex­tView, and other style and theme op­tions.

What we have just learnt about the fea­tures of Kotlin, which is an An­droid-cen­tric pro­gram­ming lan­guage, will help us to work with the new An­droid fea­tures.

For ex­am­ple, let’s look at auto-siz­ing Tex­tView. When we code for older ver­sions, prior to Oreo, we need to de­sign sep­a­rate de­sign files for vary­ing screen sizes, which fit the screen fea­tures, like height, width, etc. But in Oreo, we have an auto-siz­ing op­tion, which will auto-re­size the text ac­cord­ing to the tar­get de­vice screen. For this, the fol­low­ing code is used:

<?xml ver­sion="1.0" en­cod­ing="utf-8"?> <Tex­tView an­droid:lay­out_width="match_­par­ent" an­droid:au­toSizeTex­tType="uni­form" />

A new prop­erty called au­toSizeTex­tType has been in­tro­duced in Oreo, which will con­trol the auto siz­ing of the Tex­tView. The value can either be uni­form or none, based on the re­quire­ment. We have al­ready learned that con­nect­ing el­e­ments is eas­ier us­ing Kotlin. This is ev­i­dent when us­ing the auto-sized Tex­tView pro­gram­mat­i­cally with Kotlin, as fol­lows:

val t: Tex­tView = find­ViewById(R.id.tex­tview) as Tex­tView; t.setText(“Wel­come to OSFY”); Tex­tViewCom­pat.setAu­toSizeTex­tTypeWithDe­faults(t,Tex­tViewComp at.AUTO_SIZE_TEXT_TYPE_UNIFORM);

Tex­tViewCom­pat.setAu­toSizeTex­tTypeWithDe­faults is used for the pur­pose, where the ar­gu­ments are Tex­tView in­stant and auto size text type, which can be either Tex­tViewCom­pat. AUTO_SIZE_TEXT_TYPE_NONE or Tex­tViewCom­pat. AUTO_SIZE_TEXT_TYPE_UNIFORM.

By: Jis Joe Mathew

The au­thor is as­sis­tant pro­fes­sor of com­puter sci­ence and en­gi­neer­ing at Amal Jy­oti Col­lege, Kani­ra­pally, Ker­ala. He can be con­tacted at jisjoe­mathew@gmail.com.

Fig­ure 1: Adding Kotlin to An­droid Stu­dio

Fig­ure 2: Con­vert­ing Java to Kotlin

Fig­ure 5: Se­lect­ing the Tex­tView el­e­ment from the Com­po­nent Tree win­dow

Fig­ure 3: Dif­fer­ences be­tween the Java and the Kotlin class

Fig­ure 4: Lo­ca­tion of the lay­out file

Fig­ure 6: Se­lect­ing the de­vice to run the ap­pli­ca­tion

Fig­ure 7: The out­put of the pro­gram on the de­vice screen

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.