Android | Implicit and Explicit Intents with Examples
Pre-requisites:
- Android App Development Fundamentals for Beginners
- Guide to Install and Set up Android Studio
- Android | Starting with first app/android project
- Android | Running your first Android app
This article aims to tell about the Implicit and Explicit intents and how to use them in an android app.
What is intent in Android?
Intent is an messaging object which passes between components like services, content providers, activities etc. Normally startActivity() method is used for invoking any activity.
Some of the general functions of intent are:
- Start service
- Launch activity
- Display web page
- Display contact list
- Message broadcasting
Methods and its Description
| Methods | Description |
|---|---|
| Context.startActivity() | This is to launch a new activity or get an existing activity to be action. |
| Context.startService() | This is to start a new service or deliver instructions for an existence service. |
| Context.sendBroadcast() | This is to deliver the message to broadcast receivers. |
Intent Classification
There are two types of intents
1. Implicit Intent 2. Explicit Intent
- Implicit Intent: Using implicit Intent, component can’t be specifying. An action to be performed is declared by implicit intent. Then android operating system will filter out component which will response to the action.
For Example
In the above example, no component is specified, instead an action is performed i.e. a webpage is going to be opened. As you type the name of your desired webpage and click on ‘CLICK’ button. Your webpage is opened.
How to create an Android App to open a webpage using implicit intent(with Example)
- Step 1: Create XML file and Java File. Please refer the pre-requisites to learn more about this step.

- Step 2: Open “activity_main.xml” file and add following widgets in a Constraint Layout.
1. An EditText to input text.
2. A Button to open webpage.
Also, Assign IDs for each component as shown in the image and the code below. The assigned ID for a component helps in the identification of component and can be easily use in the Java files.
Syntax:android:id="@+id/id_name"
Here the given IDs: editText1, button1
This will make the UI of the Application.
- Step 3: Now, after the UI, this step will create the Backend of the App. For this, Open “MainActivity.java” file and instantiate the component (Button) created in the XML file using findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.
General Syntax:ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);
Syntax for used components (EditText, Button):
EditText editText = (EditText)findViewById(R.id.editText1);
Button button = (Button)findViewById(R.id.button1); - Step 4: This step involves setting up the operations to display the Toast Message. These operations are as follows:
a). First Add the listener on button and this button will open webpage.
button.setOnClickListener(new View.OnClickListener() {});b). Create String type variable to store the value of ‘EditText’. Value is accepted and converted to string.
String url = editText1.getText().toString();
c). Create an Intent object MainActivity.java class to open the webpage.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
d). The startActivity() method starts to call a webpage for opening specified by the intent.
startActivity(intent);
Therefore the code snippet for Implicit Intent:
String url = editText1.getText().toString(); Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent);
Complete code of implicit Intent
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><android.support.constraint.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><!-- add an edittext to input text --><EditTextandroid:id="@+id/editText1"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="136dp"android:ems="10"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/><!-- add a button for click --><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/editText1"android:layout_centerHorizontal="true"android:layout_marginTop="49dp"android:layout_marginEnd="132dp"android:text="Click"app:layout_constraintEnd_toEndOf="@+id/editText1"app:layout_constraintTop_toBottomOf="@+id/editText1"/></android.support.constraint.ConstraintLayout>chevron_rightfilter_noneMainActivity.java
packageorg.geeksforgeeks.implicitIntent_example;importandroid.app.Activity;importandroid.content.Intent;importandroid.net.Uri;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;publicclassMainActivityextendsActivity {@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Bind the components to their respective objects// by assigning their IDs// with the help of findViewById() methodfinalEditText editText1 = (EditText)findViewById(R.id.editText1);Button button = (Button)findViewById(R.id.button1);// implementation of onClick event for Implicit Intentbutton.setOnClickListener(newView.OnClickListener() {@OverridepublicvoidonClick(View v){// performing webpage open actionString url = editText1.getText().toString();Intent intent =newIntent(Intent.ACTION_VIEW, Uri.parse(url));startActivity(intent);}});}}chevron_rightfilter_noneOutput:
- Step 1: Create XML file and Java File. Please refer the pre-requisites to learn more about this step.
- Explicit Intent: Using explicit intent any other component can be specified. In other words, the targeted component is specified by explicit intent. So only the specified target component will be invoked.
For Example:

Explicit Intent Example
In the above example, There are two activities (FirstActivity, SecondActivity). When you click on ‘GO TO OTHER ACTIVITY’ button in the first activity, then you move to second activity. When you click on ‘GO TO HOME ACTIVITY’ button in the second activity, then you move to the first activity. This is getting done through Explicit Intent.
How to create an Android App to move to next activity using Explicit Intent(with Example)
- Step 1: Create XML file and Java File. Please refer the pre-requisites to learn more about this step.

- Step 2: Open “activity_main.xml” file and add following widgets in a Constraint Layout.
1. A Button for moving to second activity.
2. A TextView for writing some text.
Also, Assign IDs for each component (Button, TextView) as shown in the image and the code below. The assigned ID for a component helps in the identification of component and can be easily use in the Java files.
Syntax:android:id="@+id/id_name"
Here the given IDs: Button01, TextView01.
This will make the UI of the Application.
- Step 3: Now, after the UI, this step will create the Backend of the App. For this, Open “MainActivity.java” file and instantiate the component (Button, TextView) created in the XML file using findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID.
General Syntax:
ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);
Syntax for used components (Button, TextView):
1. Button button = (Button)findViewById(R.id.Button01);
2. TextView textView = (TextView)findViewById(R.id.TextView01); - Step 4: This step involves setting up the operations to create explicit intent. These operations are as follows:
a. First Add the listener on button and using this button you will move to other activity.
button1.setOnClickListener(new View.OnClickListener() {});
b. Now, Create an Intent.
Intent i = new Intent(getApplicationContext(), SecondActivityName.class);
Parameters: This is having two parameters:
- getApplicationContext(): Returns the context for the entire application.
- OtherActivityName.class: You should use your activity name here.
Therefore the code to make an explicit intent is:
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
c. Now Start the Targeted Activity.
startActivity(i):This starts target activity.
The code snippet for explicit intent:Intent i = new Intent(getApplicationContext(), ActivityTwo.class); startActivity(i);
- Step 5: Now we have to create a second activity as a destination activity.
The steps to create the second activity is as follows:android project > File > new > Activity > Empty Activity
- Step 6: Now open your second xml file.
Add Button and TextView for moving back to home activity and to write some text on activity respectively. Assign ID to Button and Textview. Second Activity is shown below:
- Step 7: Now, open up the your second activity java file and perform the following operation.
a. First Add the listener on button and using this button you will move to home activity.
button1.setOnClickListener(new View.OnClickListener() {});
b. Now, Create an Intent.
Intent i = new Intent(getApplicationContext(), OtherActivityName.class);
Therefore the code to make an explicit intent is:
Intent i = new Intent(getApplicationContext(), MainActivity.class);c. Now Start the Targeted Activity.
startActivity(i):This starts target activity.
The code to show the Toast message:
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i); - Step 8: Now Run the app and operate as follows:
When the app is opened, it displays a “GO TO OTHER ACTIVITY” button.
Click the “GO TO OTHER ACTIVITY” button.
Then you will move to second activity.
Also, you will get a “GO TO HOME ACTIVITY” button on the second activity, When you will click this, you will move to home activity.
Complete code of Explicit Intent
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><android.support.constraint.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context="org.geeksforgeeks.explicit_intent.MainActivity"><!-- add a button to move to next activity --><Buttonandroid:id="@+id/Button01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_below="@+id/TextView01"android:layout_marginTop="209dp"android:onClick="onClick"android:text="Go To Other Activity"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"tools:ignore="OnClick"/><!-- add a TextView to write some text on activity --><TextViewandroid:id="@+id/TextView01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/Button01"android:layout_alignParentTop="true"android:layout_marginTop="44dp"android:minHeight="60dip"android:text="This is First Activity"android:textSize="20sp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/></android.support.constraint.ConstraintLayout>chevron_rightfilter_noneMainActivity.java
packageorg.geeksforgeeks.explicit_intent;importandroid.os.Bundle;importandroid.app.Activity;importandroid.content.Intent;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;publicclassMainActivityextendsActivity {// Defining the object for buttonButton button1;@OverridepublicvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Bind the components to their respective objects// by assigning their IDs// with the help of findViewById() methodbutton1 = (Button)findViewById(R.id.Button01);button1.setOnClickListener(newOnClickListener() {publicvoidonClick(View view){// Creating explicit intentIntent i =newIntent(getApplicationContext(),ActivityTwo.class);startActivity(i);}});}}chevron_rightfilter_noneactivity_two.xml
<?xmlversion="1.0"encoding="utf-8"?><android.support.constraint.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context="org.geeksforgeeks.explicit_intent.ActivityTwo"><!-- add a button for moving to home activity --><Buttonandroid: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"/><!-- add a TextView to write some text on activity --><TextViewandroid: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"/></android.support.constraint.ConstraintLayout>chevron_rightfilter_noneActivityTwo.java
packageorg.geeksforgeeks.explicit_intent;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;publicclassActivityTwoextendsActivity {// Defining the object for buttonButton button1;@OverridepublicvoidonCreate(Bundle bundle){super.onCreate(bundle);setContentView(R.layout.activity_two);// Bind the components to their respective objects// by assigning their IDs// with the help of findViewById() methodbutton1 = (Button)findViewById(R.id.Button01);button1.setOnClickListener(newOnClickListener() {publicvoidonClick(View view){// Creating explicit intentIntent i =newIntent(getApplicationContext(),MainActivity.class);startActivity(i);}});}}chevron_rightfilter_noneOutput:

- Step 1: Create XML file and Java File. Please refer the pre-requisites to learn more about this step.
Recommended Posts:
- Android | What is Toast and How to use it with Examples
- Shared Preferences in Android with Examples
- Firebase RealTime Database with Operations in Android with Examples
- Android | AdMob Banner Ads for Android Studio
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to add Radio Buttons in an Android Application?
- Android | How to Request permissions in Android Application?
- Android | Android Application File Structure
- JSP | Implicit Objects - request and response
- Android | Starting with first app/android project
- Android | Running your first Android app
- Android | 8.0 Oreo
- How to use WebView in Android
- How to use CheckBox in Android
- Android | App to Add Two Numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



