EXPLICIT INTENT EXAMPLE

This is the directory Structure of Android Explicit Intent Example. Explicity Intents are used to start other Activity within the same Appication. I have expalined more in details about Intents.If you want to know more about Intents Click here Android Intents  

 

To work with Android Explicit Intent’s we have to create project in Android Studio. Open Android Studio and create new project name. i have created new project with anme is :ExplicitIntent”  is a name of the project: when this project is created then automatically it creates AndroidManifest.xml file, MainActivity.java class and it’s activity_main.xml file.

AndroidManifest.xml:

The manifest file is created by android studio. he below AndroidManifest.xml file code has shown below. It includes  name of the package and MainActivity along with two other Activity’s namely Secound Activity and ActivityThree.It also has intent filters.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.iFlutter.explicitintent">

    <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=".Second" />
        <activity android:name=".ActivityThree"></activity>
    </application>

</manifest>

 


MainActivity.java:

The MainActivity.java class source code given below. Inside onCreate() it executes setContentView(). this load activity_main.xml  layout file into MainActivity. it get the view from activity_main layout file asigns that to TextView instance that is “e1” . Inside MainActivity there is doSomething() to this method we are passing View of the layout file,Inside doSomething() method we are using Ecplicit Intent by this code

Intent i1 = new Intent(this,Second.class

When this code is executed it calls SecondActivity along with the activity layout view.

package com.example.iFlutter.explicitintent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
      EditText e1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1 = (EditText) findViewById(R.id.edit1);

    }

    public void doSomthing(View view) {

        Intent i1 = new Intent(this,Second.class);
       i1.putExtra("user",e1.getText().toString());

        startActivity(i1);
    }
}

 

________________________________________________________________________________

activity_main.xml: The Below Layout source code  is using RelativeLayout, TextView and Button. Both Button and Text View has Uniq ID’s based on this TextView  and Button are Identified In MainActicity.java class. When we submit the Button after entering the Input Value it goes to the Second Activity along with this entered value because we used Explicit Intent. This layout file is created inside the res/layout folder

 

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Explicit Intent"
        android:textSize="26sp"
        tools:layout_editor_absoluteX="104dp"
        tools:layout_editor_absoluteY="118dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="90dp"
        android:layout_marginStart="37dp"
        android:onClick="doSomthing"
        android:text="NEXT"
        tools:layout_editor_absoluteX="148dp"
        tools:layout_editor_absoluteY="231dp" />

    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="0dp"
        android:ems="10"
        android:hint="Enter your name"
        android:inputType="textPersonName" />
</RelativeLayout>

 

________________________________________________________________________________

ActivitySecound.java:

Create second.java class in the same directory or location where MainActivity.java class is created. In the second java class code we are using Explicit Intent Inside onCreate() meyhod then it executes this Explicit intent,

Intent i2 = new Intent(this,ActivityThree.class);

Starts ActivityThree and executes ActivityThree and shows the result to the user. It directly out puts the activity_three,xml layout file view as out put that is “hello iFlutter”. When we take one one step back the activity then it displays the activity_second.xml that is the user entered value in the activity_main.xml layout file input that will be displayed to the user

 

package com.example.iFlutter.explicitintent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Second extends AppCompatActivity {
TextView txt1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Intent i2 = new Intent(this,ActivityThree.class);
        startActivity(i2);
        txt1 = (TextView)findViewById(R.id.result);
        Bundle b1 = getIntent().getExtras();
        //if it is string we put string if it is integer we put int
        String s1 = b1.getString("user");
        txt1.setText(s1);
       // Intent i2 = new Intent(this,ActivityThree.class);
        //startActivity(i2);
    }
    //here it shoew error
    // i2 = new Intent(this,ActivityThree.class);
    //startActivity(i2);
}

 

________________________________________________________________________________

activity_second.xml:

The activity_second.xml layout code created below uses RelativeLayout, two TextView ‘s each has uniq ID’s to identify them. In java class when this layout file loads. This layout file is created inside the res/layout folder

<?xml version="1.0" encoding="utf-8"?>
<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:background="#7ba671"
    tools:context=".Second">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="36dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="212dp"
        android:background="#df622c"
        android:backgroundTint="#3bea24"
        android:text="Second Activity" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginTop="50dp"
        android:textSize="24sp" />
</RelativeLayout>

 

 


ActivityThree.java:

Create this AcivityThree.java class in the same directory where MainActivity.jaca class is created.  from Second.java class this ActivityThree.java class code is called by execuing Explicit Intent In onCreate() method of second.java class. Inside OnCreate() method of ActivityThree it executes activity_three.xml layout file and displays the output to the user. When we take one step back to the activity it shows the Second Activity Result to the user.

 

package com.example.iFlutter.explicitintent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class ActivityThree extends AppCompatActivity {
   TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three);
        textView = (TextView)findViewById(R.id.t3);
    }
}

 

________________________________________________________________________________

activity_three.xml:

This layout file code has Constraintlayout and one Text View with Id to identify In MainActivity after loading. this layout file is created inside the res/layout folder

 

<?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=".ActivityThree">

    <TextView
        android:id="@+id/t3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello iFlutter"
        tools:layout_editor_absoluteX="147dp"
        tools:layout_editor_absoluteY="246dp" />
</android.support.constraint.ConstraintLayout>

 

 

Congratulations You have Learned About Explicit Intents Using In Above Example. If you want to Know More About Android Intents Android Intents

 

Leave a Reply

Categories

Myles [CPS] IN
Shein WW