0% found this document useful (0 votes)
53 views

Android 12

The document describes an Android program that implements implicit and explicit intents. For implicit intents, buttons are coded to launch new activities for filling a form, viewing details, and visiting a website when clicked. For explicit intents, buttons are coded to open a web page, dial a phone number, and view a map location using intents specifying the ACTION_VIEW, ACTION_DIAL, and ACTION_VIEW actions respectively. The program demonstrates how to use intents to launch both internal and external activities and actions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Android 12

The document describes an Android program that implements implicit and explicit intents. For implicit intents, buttons are coded to launch new activities for filling a form, viewing details, and visiting a website when clicked. For explicit intents, buttons are coded to open a web page, dial a phone number, and view a map location using intents specifying the ACTION_VIEW, ACTION_DIAL, and ACTION_VIEW actions respectively. The program demonstrates how to use intents to launch both internal and external activities and actions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Practical No.

12
Aim: Develop a program to implement new activity using explicit
intent and implicit intent.
1)Implicti intent
XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/fillFormButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fill Admission Form"
android:layout_centerInParent="true"/>

<Button
android:id="@+id/viewDetailsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View Admission Details"
android:layout_below="@id/fillFormButton"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>

<Button
android:id="@+id/visitWebsiteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Visit Website"
android:layout_below="@id/viewDetailsButton"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>

</RelativeLayout>
Java Code:
package com.example.myapplicationintentreal;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

Button fillFormButton = findViewById(R.id.fillFormButton);


Button viewDetailsButton =
findViewById(R.id.viewDetailsButton);
Button visitWebsiteButton =
findViewById(R.id.visitWebsiteButton);

fillFormButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,
AdmissionFormActivity.class);
startActivity(intent);
}
});

viewDetailsButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,
AdmissionDetailsActivity.class);
startActivity(intent);
}
});

visitWebsiteButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// Define the URL to visit
String websiteUrl = "https://www.google.com"; // Replace
with your website URL
// Create the intent to open a web browser
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(websiteUrl));

// Verify that the intent can be resolved to avoid crashing


if (browserIntent.resolveActivity(getPackageManager()) !=
null) {
// Start the activity to open the web browser
startActivity(browserIntent);
}
}
});
}
}
Output:
1)
2)

2)Explicit Intent
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonOpenWebPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Web Page"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"/>

<Button
android:id="@+id/buttonDialNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dial Number"
android:layout_below="@id/buttonOpenWebPage"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>

<Button
android:id="@+id/buttonOpenMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Map"
android:layout_below="@id/buttonDialNumber"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>

</RelativeLayout>
Java Code:
package com.example.myintent;

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

public class MainActivity extends AppCompatActivity {

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

Button buttonOpenWebPage =
findViewById(R.id.buttonOpenWebPage);
buttonOpenWebPage.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// URL to open in the browser
String url = "https://www.example.com";
openWebPage(url);
}
});

Button buttonDialNumber =
findViewById(R.id.buttonDialNumber);
buttonDialNumber.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// Phone number to dial
String phoneNumber = "1234567890";
dialPhoneNumber(phoneNumber);
}
});

Button buttonOpenMap = findViewById(R.id.buttonOpenMap);


buttonOpenMap.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
// Location coordinates for the map
double latitude = 37.7749;
double longitude = -122.4194;
openMap(latitude, longitude);
}
});
}

private void openWebPage(String url) {


Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}

private void dialPhoneNumber(String phoneNumber) {


Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
+ phoneNumber));
startActivity(intent);
}

private void openMap(double latitude, double longitude) {


Uri gmmIntentUri = Uri.parse("geo:" + latitude + "," + longitude);
Intent intent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
}
}
Output:
1)

2)

You might also like