TY Practical No. 19 – Develop a program to implement new Activity using explicit intent and implicit intent to open any other website.

Practical No. 19 –  Develop a program to implement new Activity using explicit intent and implicit intent to open any other website.


๐Ÿ“ 1. Project Structure

app/
 ├── java/com/example/intentapp/
 │     ├── MainActivity.java
 │     ├── SecondActivity.java
 │
 ├── res/layout/
 │     ├── activity_main.xml
 │     ├── activity_second.xml
 │
 └── AndroidManifest.xml

๐Ÿ’ป 2. MainActivity.java

package com.example.intentapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.net.Uri;

public class MainActivity extends AppCompatActivity {

    Button btnExplicit, btnImplicit;

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

        btnExplicit = findViewById(R.id.btnExplicit);
        btnImplicit = findViewById(R.id.btnImplicit);

        // Explicit Intent → Open SecondActivity
        btnExplicit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, SecondActivity.class);

                // Passing data
                intent.putExtra("firstname", "Student");
                intent.putExtra("lastname", "Name");

                startActivity(intent);
            }
        });

        // Implicit Intent → Open Website
        btnImplicit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://www.google.com"));

                startActivity(intent);
            }
        });
    }
}

๐Ÿ’ป 3. SecondActivity.java

package com.example.intentapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

    TextView txtData;

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

        txtData = findViewById(R.id.txtData);

        // Receiving data
        String fname = getIntent().getStringExtra("firstname");
        String lname = getIntent().getStringExtra("lastname");

        txtData.setText("Student Name: " + fname + " " + lname);
    }
}

๐Ÿงพ 4. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="20dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Student: Student Name"
        android:textSize="20sp"
        android:layout_marginBottom="30dp"/>

    <Button
        android:id="@+id/btnExplicit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Open Second Activity"
        android:layout_marginBottom="20dp"/>

    <Button
        android:id="@+id/btnImplicit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Open Website"/>

</LinearLayout>

๐Ÿงพ 5. activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Data will appear here"
        android:textSize="20sp"/>

</LinearLayout>

⚙️ 6. AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">

        <activity android:name=".SecondActivity"></activity>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

๐Ÿชœ 7. Step-by-Step Procedure

  1. Open Android Studio

  2. Create New Project → Empty Activity

  3. Select Java language

  4. Create SecondActivity.java

  5. Design both XML layouts

  6. Add buttons in MainActivity

  7. Write code for:

    • Explicit Intent (SecondActivity)

    • Implicit Intent (Open website)

  8. Add SecondActivity in Manifest

  9. Run the app on emulator/device


๐Ÿ“˜ 8. Simple Explanation

  • Activity → A screen in Android app

  • Intent → Used to move between screens or apps

๐Ÿ”น Explicit Intent

  • Used to open your own activity

  • Example: MainActivity → SecondActivity

๐Ÿ”น Implicit Intent

  • Used to open external apps

  • Example: Open browser (Google website)


๐Ÿ“ฑ Second Screen

Image

Image

Image

  • Shows:

    • Student First Name + Last Name



Popular posts from this blog