0% found this document useful (0 votes)
26 views5 pages

Kotlin Topics Overview

This is related to Mobile application development. Topic is Kotlin overview

Uploaded by

hs6515700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Kotlin Topics Overview

This is related to Mobile application development. Topic is Kotlin overview

Uploaded by

hs6515700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Kotlin Topics Overview

1. Spinner in Kotlin
A Spinner in Android is a dropdown list that allows users to choose a single item from a set of

options.

Example:

```

val spinner: Spinner = findViewById(R.id.spinner)

val options = arrayOf("Option 1", "Option 2", "Option 3")

val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, options)

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)

spinner.adapter = adapter

spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {

val selectedItem = options[position]

override fun onNothingSelected(parent: AdapterView<*>) {}

```

2. ArrayAdapter in Kotlin
ArrayAdapter in Android is used to populate a view (such as ListView, GridView, or Spinner) with

data from an array or list.


Example:

```

val items = listOf("Item 1", "Item 2", "Item 3")

val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, items)

val listView: ListView = findViewById(R.id.listView)

listView.adapter = adapter

```

3. Uses of Interface in Kotlin


An interface in Kotlin defines a set of methods or properties that a class can implement, similar to

other object-oriented languages.

Example:

```

interface Clickable {

fun onClick()

class Button : Clickable {

override fun onClick() {

println("Button clicked!")

```

Uses of Interface:

- Provides abstraction.

- Helps achieve multiple inheritance.


- Ensures that classes implementing an interface follow a specific behavior.

4. Inner and Anonymous Classes in Kotlin


Inner Class: An inner class has access to the members of the outer class and can be created using

the `inner` keyword.

Example of Inner Class:

```

class Outer {

private val message = "Hello from Outer Class"

inner class Inner {

fun showMessage() = message

```

Anonymous Class: An anonymous class in Kotlin is an instance of an interface or abstract class

created on the spot without being formally named.

Example of Anonymous Class:

```

val clickListener = object : Clickable {

override fun onClick() {

println("Anonymous object clicked!")

```
5. RelativeLayout in Kotlin
RelativeLayout positions its children relative to each other or the parent layout.

Example:

```

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="TextView 1"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true" />

<TextView

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="TextView 2"

android:layout_below="@id/textView1"

android:layout_alignParentEnd="true" />

</RelativeLayout>

```
Properties:

- layout_alignParentTop, layout_alignParentBottom: Aligns the view to the top or bottom of the

parent.

- layout_centerHorizontal, layout_centerVertical: Centers the view horizontally or vertically within the

parent.

- layout_below, layout_above: Positions the view below or above another view.

Advantages:

- Flexible for positioning views relative to each other.

- Saves layout space and can reduce nested layouts.

Disadvantages:

- Can become complex and harder to manage with more views.

- Slower rendering compared to ConstraintLayout for complex layouts.

You might also like