Quick Contact


    Android Intent

    The message delivered between components such as activities, content providers, broadcast receivers, and services is known as Android Intent.

    It’s commonly used with the startActivity() method to start activities, broadcast receivers, and other things.

    The definition of intent in the dictionary is “intention or purpose.” As a result, it may be defined as the desire to do action.

    Android.content has a subclass called LabeledIntent.

    Intent is a type of class.

    Types of Android Intents

    In Android, there are two sorts of intents: implicit and explicit.

    1. Implicit Intent
    2. The component isn’t specified via Implicit Intent. In this example, intent gives information about the components that are accessible from the system that is to be executed.

      To see the webpage, for example, write the following code.

      Intent intent=new Intent(Intent.ACTION_VIEW);

      intent.setData(Uri.parse(“http://www.Tutorials.ducatindia.com”));

      startActivity(intent);

    3. Explicit Intent
    4. The component is specified via Explicit Intent. In this example, intent specifies the external class that should be used.

      Intent i = new Intent(getApplicationContext(), ActivitySampleTwo.class);

      startActivity(i);

    Android Implicit Intent Example

    Let’s look at a basic example of implicit purpose using a web page.

    File – activity_main.xml

    < ?xml version="1.0" encoding="utf-11"?>

    < android.support.constraint.ConstraintLayoutxmlns: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.Tutorials.ducatindia.com.implicitintent.MainActivity”>

    < Button

    android:id=”@+id/button”

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_marginRight=”8dp”

    android:layout_marginLeft=”156dp”

    android:layout_marginTop=”172dp”

    android:text=”Visit”

    app:layout_constraintEnd_toEndOf=”parent”

    app:layout_constraintHorizontal_bias=”0.0″

    app:layout_constraintStart_toStartOf=”parent”

    app:layout_constraintTop_toBottomOf=”@+id/editText” />

    < Area

    android:id=”@+id/button”

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_marginRight=”8dp”

    android:layout_marginLeft=”156dp”

    android:layout_marginTop=”172dp”

    android:text=”Visit”

    app:layout_constraintEnd_toEndOf=”parent”

    app:layout_constraintHorizontal_bias=”0.0″

    app:layout_constraintStart_toStartOf=”parent”

    app:layout_constraintTop_toBottomOf=”@+id/editText” />

    < EditText

    android:id=”@+id/editText”

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_marginEnd=”8dp”

    android:layout_marginStart=”8dp”

    android:layout_marginTop=”60dp”

    android:ems=”10″

    app:layout_constraintEnd_toEndOf=”parent”

    app:layout_constraintHorizontal_bias=”0.575″

    app:layout_constraintStart_toStartOf=”parent”

    app:layout_constraintTop_toTopOf=”parent” />

    < ContextView

    android:id=”@+id/editText”

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:layout_marginEnd=”4dp”

    android:layout_marginStart=”8dp”

    android:layout_marginTop=”41dp”

    android:ems=”10″

    app:layout_constraintEnd_toEndOf=”parent”

    app:layout_constraintHorizontal_bias=”0.575″

    app:layout_constraintStart_toStartOf=”parent”

    app:layout_constraintTop_toTopOf=”parent” />

    < /android.support.constraint.ConstraintLayout>

    Activity class

    MainActivity.java

    package tutorials.ducatindia.com.implicitintent;

    import android.content.Intent;

    import android.net.Uri;

    import android.support.v7.app.AppCompatActivity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Button;

    import android.widget.EditText;

    public class MainActivity extends AppCompatActivity {

    	    Button button;  
    	    Area;
    	ContextView;
    	EditTexteditText;  
    
    	    @Override  
    	    protected void onCreate(Bundle savedInstanceState) {  
    	super.onCreate(savedInstanceState);  
    	setContentView(R.layout.activity_main);  
    
    	        button = findViewById(R.id.button);  
    	editText=  findViewById(R.id.editText);  
    
    	button.setOnClickListener(new View.OnClickListener() {  
    	            @Override  
    	            public void onClick(View view) {  
    	                String url=editText.getText().toString();  
    	                Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));  
    	startActivity(intent);  
    	protected void onCreate(Bundle savedInstanceState) {  
    	super.onCreate(savedInstanceState);  
    	setContentView(R.layout.activity_main);  
    
    	        button = findViewById(R.id.button1);  
    	editText=  findViewById(R.id.editText); 
    	protected void onCreate(Bundle savedInstanceState) {  
    	super.onCreate(savedInstanceState);  
    	setContentView(R.layout.activity_main);  
    
    	        button = findViewById(R.id.button2);  
    	editText=  findViewById(R.id.editText); 
    	protected void onCreate(Bundle savedInstanceState) {  
    	super.onCreate(savedInstanceState);  
    	setContentView(R.layout.activity_main);  
    
    	        button = findViewById(R.id.button);  
    	editText=  findViewById(R.id.editText); 
    	            }  
    	        });  
    	    }  
    	}
    	Activity_SampleTwo.xml
    	activity_two.xml< ?xml version="1.0" encoding="utf-11"?>
    	< 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="Tutorials.ducatindia.explicit_intent.ActivityTwo">
    
    	
    	< Button
    	android:id="@+id/Button01"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_below="@+id/TextView01"
    	android:layout_alignParentLeft="true"
    	android:layout_marginBottom="220dp"
    	android:onClick="onClick"
    	android:text="Go to Home Activity"
    	app:layout_constraintBottom_toBottomOf="parent"
    	app:layout_constraintEnd_toEndOf="parent"
    	app:layout_constraintHorizontal_bias="0.533"
    	app:layout_constraintStart_toStartOf="parent"
    	tools:ignore="OnClick" />
    
    	
    	< TextView
    	android:id="@+id/TextView01"
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_alignLeft="@+id/Button01"
    	android:layout_alignParentTop="true"
    	android:layout_margin="71dp"
    	android:layout_marginTop="144dp"
    	android:layout_marginEnd="88dp"
    	android:layout_marginRight="88dp"
    	android:minHeight="60dip"
    	android:text="This is Second Activity"
    	android:textSize="20sp"
    	app:layout_constraintEnd_toEndOf="parent"
    	app:layout_constraintTop_toTopOf="parent" />
    	< Context
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_alignLeft="@+id/Button01"
    	android:layout_alignParentTop="true"
    	android:layout_margin="71dp"
    	android:layout_marginTop="144dp"
    	android:layout_marginEnd="88dp"
    	android:layout_marginRight="88dp"
    	android:minHeight="60dip"
    	android:text="This is Second Activity"
    	android:textSize="20sp"
    	app:layout_constraintEnd_toEndOf="parent"
    	app:layout_constraintTop_toTopOf="parent" />
    	< Area
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:layout_alignLeft="@+id/Button01"
    	android:layout_alignParentTop="true"
    	android:layout_margin="71dp"
    	android:layout_marginTop="144dp"
    	android:layout_marginEnd="88dp"
    	android:layout_marginRight="88dp"
    	android:minHeight="60dip"
    	android:text="This is Second Activity"
    	android:textSize="20sp"
    	app:layout_constraintEnd_toEndOf="parent"
    	app:layout_constraintTop_toTopOf="parent" />
    
    	< /android.support.constraint.ConstraintLayout>
    	

    Enroll Yourself in Live Classes For Android Tutorial.

    There are two actions in the example above. You move to the second activity when you click the ‘GO TO OTHER ACTIVITY’ button in the first activity. You move to the first activity when you click the ‘GO TO HOME ACTIVITY’ button in the second activity. Explicit Intent is used to accomplish this.

    ActivitySampleTwo.java

    package tutorials.ducatindia.explicit_intent;

    import android.app.Activity;

    import android.content.Intent;

    import android.os.Bundle;

    import android.view.View;

    import android.view.View.OnClickListener;

    import android.widget.Button;

    public class ActivityTwo extends Activity {

    	    // Defining the object for button
    	    Button button1;
    	    @Override
    	    public void onCreate(Bundle bundle)
    	    {
    	super.onCreate(bundle);
    	setContentView(R.layout.activity_one);
    
    	        // Bind the components to their respective objects
    	        // by assigning their IDs
    	        // with the help of findViewById() method
    	        button1 = (Button)findViewById(R.id.Button01);
    
    	        button1.setOnClickListener(new OnClickListener() {
    	            public void onClick(View view)
    	            {
    
    	                // Creating explicit intent
    	                Intent i = new Intent(getApplicationContext(),
    	MainActivity.class);
    	startActivity(i);
    
    	      // Defining the object for button
    	    Button button1;
    	    @Override
    	    public void onCreate(Bundle bundle)
    	    {
    	super.onCreate(bundle);
    	setContentView(R.layout.activity_sampletwo);
    
    	        // with the help of findViewById() method
    	        button1 = (Button)findViewById(R.id.Button01);
    
    	        button1.setOnClickListener(new OnClickListener() {
    	            public void onClick(View view)
    	            {
    
    	                // Creating explicit intent
    	                Intent i = new Intent(getApplicationContext(),
    	MainActivity.class);
    	startActivity(i);
    	            }
    	        });
    	    }
    	}
    	
    Output:-
    Android Intent

    Enroll Yourself in Live Classes For Android training in delhi.

    Copyright 1999- Ducat Creative, All rights reserved.