how to jump one activity to another activity in kotlin android
In this article, today learn how to jump one activity to
another activity in kotlin android basically using an Intent[] from the
following code.
val intent
= Intent(this@MainActivity, SecondActivity::class.java)
startActivity(intent)
follow the example
learn how to jump one activity to another activity in kotlin android
1.MainActivity.kt :
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.View
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
class MainActivity:AppCompatActivity() {
protected fun
onCreate(savedInstanceState:Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar =
findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
val button =
findViewById(R.id.button) as Button
button.setOnClickListener(object:View.OnClickListener() {
fun
onClick(view:View) {
val intent
= Intent(this@MainActivity, SecondActivity::class.java)
startActivity(intent)
}
})
}
}
2.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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="akraj.snow.test.MainActivity"
tools:showIn="@layout/activity_main">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
3.SecondActivity.kt:
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
class SecondActivity:AppCompatActivity() {
protected fun
onCreate(savedInstanceState:Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val toolbar =
findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
}
}
4.activity_second :
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="akraj.snow.test.SecondActivity"
tools:showIn="@layout/activity_second">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"
android:id="@+id/textView"
/>
</RelativeLayout>
5. manifest.xml :
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.intent.example"
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity
android:name=". SecondActivity "
android:label="@string/title_activity_main" >
</application>
</manifest>
No comments:
Post a Comment