How to open a new activity with a button click -Android Kotlin Example
This example shows how to open a new activity with a button click.
To create a new activity by clicking a button, follow these steps.
- Create a button with id in activity_main.xml
- Create new_activity.xml
- Add findViewById, setOnClickListener, intent in MainActivity.kt
- Create NewActivity.kt
- Add NewActivity in AndroidManifest.xml
This example will help you answer the following questions.
- What is findViewById?
- What is setOnClickListener?
- What is intent?
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:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/button_click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click" />
</LinearLayout>
new_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Activity" />
</LinearLayout>
MainActivity.kt
package com.simple.example1
import android.content.Intent
import androidx.appcompat.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)
val buttonClick = findViewById<Button>(R.id.button_click)
buttonClick.setOnClickListener {
val intent = Intent(this, NewActivity::class.java)
startActivity(intent)
}
}
}
ㆍfindViewById finds the View with the corresponding ID. Here is the button created in acitivity_main.xml.
ㆍsetOnClickListener attaches a click listener to the Button which calls the new activity by using intent.
ㆍintent is to request another activity to achieve a task.
NewActivity.kt
package com.simple.example1
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class NewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.new_activity)
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simple.example1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Example1">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NewActivity"></activity>
</application>
</manifest>
Feel free to ask any questions. I’m also learning Kotlin like you. Your question will help us all. Of course welcome to point out any mistakes. Please let me know if you have better ideas.