0% found this document useful (0 votes)
10 views4 pages

Fariya Wazir Assignment 3 L

Its a assignment from my university

Uploaded by

fariyawazir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Fariya Wazir Assignment 3 L

Its a assignment from my university

Uploaded by

fariyawazir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

NATIONAL UNIVERSITY OF MODERN

LANGUAGES

Mobile Application

Assignment 3 Lab

Submitted By

Name Fariya wazir


Class BS-IT (6th)
Roll No MTF-1013
Submitted To Miss Hina Mehjabeen

Question:
What are intents? Create an Android Application in which when you click a button
then it displays any web page (you can pass url of any web page) using intent.
(2+8=10)

What are intents?


ANSWER: An intent in Android development is a message carrying information about an action
you want to perform. It acts as a messenger between different parts of your app or even
communicates with other apps.
Intents come in two types:
 Explicit intents: These are like named invitations. You tell the system exactly which app
component (like a specific activity in another app) you want to talk to.
 Implicit intents: These are more like open invitations. You tell the system what you want
done (like taking a picture), and the system finds any app that can handle that task.

MAIN ACTIVITY.JAVA
package com.example.buttonopenurl;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText urlEditText;

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

Button openButton = findViewById(R.id.open_button);


urlEditText = findViewById(R.id.url_edit_text);

openButton.setOnClickListener(v -> {
String url = urlEditText.getText().toString().trim();

// Check if URL is empty


if (url.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter a URL",
Toast.LENGTH_SHORT).show();
return;
}

// Create an Intent with action to view and data as the URL


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

// Check if there's an app to handle the intent


if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "No app found to open
webpages", Toast.LENGTH_SHORT).show();
}
});
}
}
ACTIVITY MAIN XML
<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">

<EditText
android:id="@+id/url_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Enter URL"
android:inputType="textUri" />

<Button
android:id="@+id/open_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/url_edit_text"
android:layout_centerHorizontal="true"
android:layout_margin="16dp"
android:text="Open Webpage" />
</RelativeLayout>

OutPut SCREENSHOT

You might also like