Program 5
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
val PICK_CONTACT = 1
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)
}
}
SecondActivity.kt:
package com.example.program5
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
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>