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

Program 5

The document outlines the implementation of an Android application that demonstrates explicit intent, implicit intent, and content provider usage. It includes XML layouts for the main and second activities, Kotlin code for the main activity handling button clicks to open a new activity, a web browser, and pick a contact. Additionally, it provides a theme definition and the necessary manifest entry for the second activity.

Uploaded by

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

Program 5

The document outlines the implementation of an Android application that demonstrates explicit intent, implicit intent, and content provider usage. It includes XML layouts for the main and second activities, Kotlin code for the main activity handling button clicks to open a new activity, a web browser, and pick a contact. Additionally, it provides a theme definition and the necessary manifest entry for the second activity.

Uploaded by

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

Program 5:

Create an application to implement new activity using explicit intent, implicit intent and
content provider:

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

<Button
android:id="@+id/btnExplicit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open New Activity (Explicit Intent)" />

<Button
android:id="@+id/btnImplicit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Browser (Implicit Intent)" />

<Button
android:id="@+id/btnContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pick Contact (Content Provider)" />

</LinearLayout>

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

<TextView
android:text="Second Activity"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

MainActivity.kt:
package com.example.program5
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.ContactsContract
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

val PICK_CONTACT = 1

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val btnExplicit = findViewById<Button>(R.id.btnExplicit)


val btnImplicit = findViewById<Button>(R.id.btnImplicit)
val btnContact = findViewById<Button>(R.id.btnContact)

btnExplicit.setOnClickListener {
// Explicit Intent to open SecondActivity
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}

btnImplicit.setOnClickListener {
// Implicit Intent to open a webpage
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("https://www.google.com")
startActivity(intent)
}

btnContact.setOnClickListener {
// Content Provider to pick a contact
val intent = Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI)
startActivityForResult(intent, PICK_CONTACT)
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data:


Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == PICK_CONTACT && resultCode ==
Activity.RESULT_OK) {
val contactUri: Uri? = data?.data
// You can use this URI to fetch more contact details
}
}
}

SecondActivity.kt:
package com.example.program5

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class SecondActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
}
}

themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Program4" parent="Theme.AppCompat.DayNight" />
</resources>

AndroidManifest.xml:
<activity
android:name=".SecondActivity"
android:exported="true"
android:label=""
android:theme="@style/Theme.Program4"
tools:ignore="ExtraText">
</activity>

You might also like