Industrial Training




Kotlin Android Implicit Intent


Android Implicit Intent invokes the component of another app to handle the request. It does not specify the component name specifically.
For example, if we want to share data using Intent, it invokes the relevant component to fulfill the request.



intent = Intent(Intent.ACTION_VIEW)  
intent.setData(Uri.parse("https://www.javatpoint.com/"))  
startActivity(intent)  
  
intent= Intent(Intent.ACTION_VIEW, Uri.parse("https://www.javatpoint.com/"))  
startActivity(intent)  

Kotlin Android Implicit Intent Example Invoking URL


In this example, we will invoke the URL using Implicit Intent clicking on Button.


activity_main.xml


Add the following code in activity_main.xml file. In this activity, we use a Button to invoke Intent.


< ?xml version="1.0" encoding="utf-8"?>  
< android.support.constraint.ConstraintLayout 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"  
    tools:context="example.javatpoint.com.kotlinimplicitintent.MainActivity">  
  
    < TextView  
        android:id="@+id/textView"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginBottom="8dp"  
        android:layout_marginTop="8dp"  
        android:text="Your First Activity"  
        android:textSize="18sp"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintHorizontal_bias="0.501"  
        app:layout_constraintLeft_toLeftOf="parent"  
        app:layout_constraintRight_toRightOf="parent"  
        app:layout_constraintTop_toTopOf="parent"  
        app:layout_constraintVertical_bias="0.172" />  
  
    < Button  
        android:id="@+id/button"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_marginBottom="8dp"  
        android:layout_marginEnd="8dp"  
        android:layout_marginStart="8dp"  
        android:layout_marginTop="8dp"  
        android:text="click to invoke intent"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toBottomOf="@+id/textView"  
        app:layout_constraintVertical_bias="0.77" />  
  
  
< /android.support.constraint.ConstraintLayout>  

MainActivity.kt


Add the following code in the MainActivity.kt class. In this class, we are invoking the URL on clicking the button using Implicit Intent. To invoke this intent, we are passing the action type and URL. The startActivity() method is used to start the Intent.


package example.javatpoint.com.kotlinimplicitintent  
  
import android.content.Intent  
import android.net.Uri  
import android.support.v7.app.AppCompatActivity  
import android.os.Bundle  
import kotlinx.android.synthetic.main.activity_main.*  
  
class MainActivity : AppCompatActivity() {  
  
    override fun onCreate(savedInstanceState: Bundle?) {  
        super.onCreate(savedInstanceState)  
        setContentView(R.layout.activity_main)  
  
        button.setOnClickListener(){  
            intent = Intent(Intent.ACTION_VIEW)  
            intent.setData(Uri.parse("https://www.javatpoint.com/"))  
            startActivity(intent)  
            /*  intent= Intent(Intent.ACTION_VIEW, Uri.parse("https://www.javatpoint.com/")) 
            startActivity(intent)*/  
        }  
    }  
}  

Output:





Hi I am Pluto.