Переход на другую активити kotlin

How to switch between different Activities in Android using Kotlin?

This example demonstrates how to switch between different Activities in Android using Kotlin.

Step 1 − Create a new project in Android Studio, go to File? New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

Example

Step 3 − Add the following code to src/MainActivity.kt

import android.content.Intent import android.os.Bundle import android.widget.Button import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" val button:Button = findViewById(R.id.btnOpenAct2) button.setOnClickListener < val intent = Intent(this@MainActivity, NewActivity::class.java) startActivity(intent) >> >

Step 4 − Create a new empty Activity and add the following code −

NewActivity.main

import android.content.Intent import android.os.Bundle import android.widget.Button import androidx.appcompat.app.AppCompatActivity class NewActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_new) title = "KotlinApp" val button: Button = findViewById(R.id.btnOpenMain) button.setOnClickListener < val i = Intent(this@NewActivity, MainActivity::class.java) startActivity(i) >> >

activity_new.xml

Step 5 − Add the following code to androidManifest.xml

Let’s try to run your application. I assume you have connected your actual Android Mobile device withyour computer. To run the app from android studio, open one of your project’s activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen

Источник

Android Navigation — How to navigate between activities in Android Studio by using Kotlin.

In this android example, we will see how to navigate one screen to another screen with Android Intents in Android Studio by Kotlin code.

Implementation:

Create a new Project in android studio.

Go to File > New > New Project > Empty Activity > Next > Enter Name > Select Language Kotlin > Finish

Go to activity_first.xml file and add the following code

xmlns:tools=»http://schemas.android.com/tools»
android:layout_width=»match_parent»
android:layout_height=»match_parent»
android:orientation=»vertical»
android:gravity=»center»
android:background=»#E8EFA3″
tools:context=».FirstActivity»>

android:id=»@+id/btn1″
android:layout_width=»300dp»
android:layout_height=»wrap_content»
android:text=»Go to Second Screen»>

Go to FirstActivity.kt file and add the following code

val btn1 = findViewById(R.id.btn1)

btn1.setOnClickListener val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
>

Create a new empty activity

app > java > package name > right-click > New > Activity /Empty Activity > enter name(SecondActivity.kt) > Finish.

Add following code in the activity_second.xml file

xmlns:tools=»http://schemas.android.com/tools»
android:layout_width=»match_parent»
android:layout_height=»match_parent»
android:orientation=»vertical»
android:gravity=»center»
android:background=»#A8B80A»
tools:context=».AndroidNavigation.SecondActivity»>

android:id=»@+id/btn2″
android:layout_width=»300dp»
android:layout_height=»wrap_content»
android:text=»Go to First Screen»>

Open SecondActivity.kt file and add the following code

val btn2 = findViewById(R.id.btn2)

btn2.setOnClickListener val intent = Intent(this, FirstActivity::class.java)
startActivity(intent)
>

By using Intent we wil navigate the activity from first to second screen.
Run the app in your emulator or real device, you will see the following output.

Android Navigation Example

Fig. First Screen UI

Android Navigation Example

Fig. Second Screen UI

Complete Source code of Android Navigation Example:

activity_first.xml file

xmlns:tools=»http://schemas.android.com/tools»
android:layout_width=»match_parent»
android:layout_height=»match_parent»
android:orientation=»vertical»
android:gravity=»center»
android:background=»#E8EFA3″
tools:context=».FirstActivity»>

android:id=»@+id/btn1″
android:layout_width=»300dp»
android:layout_height=»wrap_content»
android:text=»Go to Second Screen»>

FirstActivity.kt file

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.nishajain.kotinexamples.R

class FirstActivity : AppCompatActivity() override fun onCreate(savedInstanceState: Bundle?) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_first)
val btn1 = findViewById(R.id.btn1)

btn1.setOnClickListener val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
>
>
>

activity_second.xml file

xmlns:tools=»http://schemas.android.com/tools»
android:layout_width=»match_parent»
android:layout_height=»match_parent»
android:orientation=»vertical»
android:gravity=»center»
android:background=»#A8B80A»
tools:context=».SecondActivity»>

android:id=»@+id/btn2″
android:layout_width=»300dp»
android:layout_height=»wrap_content»
android:text=»Go to First Screen»>

SecondActivity.kt file

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.nishajain.kotinexamples.R

class SecondActivity : AppCompatActivity() override fun onCreate(savedInstanceState: Bundle?) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val btn2 = findViewById(R.id.btn2)

btn2.setOnClickListener val intent = Intent(this, FirstActivity::class.java)
startActivity(intent)
>
>
>

Conclusion: We have covered how to navigate between activities using intent in Android with kotlin language.

Источник

How to move From one Activity to another Activity in Android Studio Kotlin

In this tutorial we will learn how to open new Activity when a Button is clicked using Android Studio in Kotlin language. When button in an activity(Main Activity) is clicked the new activity(New Activity) will be opened.

Step 1: Create a new Project or open new project

Step 2: Create a new Activity(e.g. Empty Activity)

How to move From one Activity to another Activity in Android Studio (Kotlin)

Step 3: Code

activity_main.xml

MainActivity.kt

package com.jigopost.myapplication import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.content.Intent import android.view.View import android.widget.Button class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //initialize val mButton = findViewById(R.id.button) as Button //handle onClick mButton.setOnClickListener < //intent to start NewActivity startActivity(Intent(this@MainActivity, NewActivity::class.java)) >> >
package com.jigopost.myapplication 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) >>

Источник

How to start another activity on button click using Kotlin

On the activity_main.xml add a textView and a button. Here is is what you can use

Create a new second activity

app>>java>>new>>activity>>Empty activity From the second activity you can add an imageView and textView You now have two activities, the main activity and the second activity. Now all we have to do is to add functionality to the button in the (MainActivity.kt)

override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val button = findViewById(R.id.button) button.setOnClickListener < val intent = Intent(this, second::class.java) startActivity(intent) >> 

Now run your app on your emulator or hardware device You will get something like this Check out the Source Code on GitHub That’s a wrap for this post. We have learnt how to start another activity on button click for our app’s first time users. My ‘images’ don’t quite match with the ones you see on the Drive app 😄. But I’m sure you can come up with something better! Drop ’em in the comments below.

Источник

Читайте также:  Java найти корень числа
Оцените статью