What is implicit intent.

Android Implicit Intent is that in which you can specify the action that can invoke any app in your device able to perform action.


Implicit Intent:.

In this section i am going to show the use of implicit intent to open the new Activity with practical example.

First open the android studio and create a project and give the name of your project like OpenActivityDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop one Button from pallet. Open the activity_main.xml file and code look like this


<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"
    tools:context="com.example.niitpc.myapplication.MainActivity">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:gravity="center"
        android:layout_marginLeft="150dp"
        android:onClick="onClickButton"
        android:text="Next" />
</RelativeLayout>


     

Take a new activity for this you right click on the app folder and a pop menu display click new->Activity->Empty Activity. A dialog box will open you can change the Activity Name but directly click on finish.

This will create the Empty Activity and simple drag and drop TextView from pallet and set the property android:Text="Welcome to this page".

Open the AndroidManifest.xml file and code look like this.


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.niitpc.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Main2Activity">

            <intent-filter>
                <action android:name="com.example.niitpc.myapplication.Main2Activity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>


        </activity>
    </application>

</manifest>

     

You can do two things in AndroidManifest.xml file.

Copy the intent filter tag from the MainActivity an paste it inside the Main2Activity.

Open the acitvity_main2.xml file and copy the tools:context property value that is "com.example.niitpc.myapplication.Main2Activity" and paste inside the android:name as value.This value may differ in your computer ok.

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    public void onClickButton(View v)
    {

           Intent intent=new Intent("com.example.niitpc.myapplication.Main2Activity");
            startActivity(intent);


    }


}



       

Explanation:

Intent intent=new Intent("com.example.niitpc.myapplication.Main2Activity"):In this statement i am going to create the object of Intent and Intent constructor take android:name value that is "com.example.niitpc.myapplication.Main2Activity" .

startActivity(intent):The startActivity() take the Intent that you want to start as argument.

Do not forgot to set the onClick preperty of the button as pass the onClickButton method otherwise it will not work properly.


Implicit Intent

What is implicit intent for video Click here

Smiley face                     Smiley face