Starting Activity with (Kotlin) - Android Studio

How to start an Activity on Click in Kotlin using Android Studio?

In this tutorial we will learn how to start a new activity when a button is clicked(you can also implement this on item click or something else).

Step 1: Create a new Project with Kotlin Support OR open a new Project

Step 2: Create a new Activity(when button is click we will go to that activity)

Step 3: Code

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start New Activity"/>

</LinearLayout>

MainActivity.kt
package com.blogspot.devofandroid.kotlinpractice

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //to change title of activity(optional)
        val actionBar = supportActionBar
        actionBar!!.title = "Main Activity"

        //button
        val mButton = findViewById(R.id.button) as Button
        //button click
        mButton.setOnClickListener {
            val intent = Intent(this, NewActivity::class.java)
            startActivity(intent)

        }

    }
}

activity_new.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".NewActivity">

    <TextView
        android:text="New Activity"
        android:textStyle="bold"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

NewActivity.kt
package com.blogspot.devofandroid.kotlinpractice

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class NewActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new)

        //to change title of activity(optional)
        val actionBar = supportActionBar
        actionBar!!.title = "New Activity"
    }
}

Note: You can also use:
startActivity(Intent(this, NewActivity::class.java));

Step 4: Output

Comments

Popular posts from this blog

Manage External Storage Permission | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Java

Add a Back Button to Action Bar Android Studio (Kotlin)