Institute of Computer Science
  1. Courses
  2. 2019/20 fall
  3. Mobile Computing and Internet of Things (LTAT.06.009)
ET
Log in

Mobile Computing and Internet of Things 2019/20 fall

  • Main
  • Lectures
  • Labs
  • Homeworks & Home Assignments
  • Projects
    • Teams & Topics
    • Presentations & Report
    • Grading & Submission
  • Google Group
  • Task submission
  • Results
  • Other

Introduction

In this lab we will try our first steps with the Kotlin language and add click handling to our UI.

  • Make sure you have an understanding of these Kotlin concepts, discussed during the lecture:
    • Mutable and immutable variable declaration (var vs val)
    • Null-safety, including safe calls and the !! operator
    • Inferred types

Exercise 1: In-code GUI creation

  • in MainActivity.kt onCreate():
    • Create a vertical LinearLayout
    • Add 10 buttons to it using a loop
    • Make the Activity display the LinearLayout object instead of the default XML resource (using setContentView(..) ).

Exercise 2: ConstraintLayout practice

Change back your MainActivity so that it uses the XML resource activity definition.

  1. Divide View vertically into 2 (2/3 and 1/3, as shown on picture), using a guideline
  2. Top should have 1 TextView with long text
    • Vertically centered w.r.t. guideline
    • Horizontally constrained to edges of layout, with 32dp margin
    • Width set to match constraint
  3. Add 2 Buttons to the bottom
    • Vertically centered
    • Create a horizontal chain of them
      • Set chain to "spread inside"
    • Horizontally constrainted to edges of the parent

Exercise 3: Click behaviour

  • Set up a click handler for both buttons (see below for more details)
    • For the 1st button, use the programmatic approach and make the click update the button to show the number of times it has been clicked. Each time it gets clicked, it should also add a word to the TextView (hint).
    • For the 2nd button, use XML onClick and make it restart the counter. (Also try clearing the TextView)

Tips & Examples

Getting a reference to a UI element defined in XML

  • you can access UI View Objects directly using its ID myButton.text = "Hello"
    • have to import import kotlinx.android.synthetic.main.activity_main where activity_main correspond to the activity you are working with.
    • ( this works thanks to View Binding of the Kotlin Android Extension)
  • alternatively, using the findViewById(..) method, you can get a handle for referencing XML-declared resources & view objects in code:
    • val b:Button = findViewById(R.id.myButton)
    • this is the common approach in Java-based Android

Setting click listeners

  • To set a click listener from the code, you can use Button.setOnClickListener( .. )
    • button1.setOnClickListener { view: View? -> Log.i(TAG, "View: ${view?.id} was clicked!") }
  • Alternatively, you can specify android:onClick attribute of the XML object definition, setting its value to the name of the method to be called
    • The method has to be defined in the Activities' .kt file and accept a single argument of type View:
      • fun myHandler(view: View) { /** react to click */ }
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment