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

MAD LAB WORKSHEET 3 - Utkarsh

Uploaded by

Sidharth Thakur
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)
24 views4 pages

MAD LAB WORKSHEET 3 - Utkarsh

Uploaded by

Sidharth Thakur
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

DEPARTMEN

T OF

Experiment Title 3
Student Name: Siharth Kumar UID: 20BCS1997

Branch: BE-CSE Section/Group: 20BCS-DM-602 B


Semester: 6 Subject Code: 20CSP-356
Subject Name: Mobile Application Development Lab
Date of Performance: 28-02-2023

Aim/Overview of the practical: Create Application by Using Widgets.

Objective: To create an android application with widget named “Open Web”.

Steps:
1. Create a New Project
2. Add the App Widget to the Project. Right-Click on the app, move the
cursor to new, find the “Widget” option at the end, select it.
3. Specify the required properties for the widget such as min. width
and height, config file and preferred language, etc, and proceed.
Files are automatically generated.
4. Create Code according to app requirements.
5. Select your device and run the code.

Code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app=http://schemas.android.com/apk/res-auto
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<TextView
Android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
DEPARTMEN
T OF

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

first_widget.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.Exp3_WidgetApp.AppWidget.Container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.Exp3_WidgetApp.AppWidgetContainer">
<Button
android:id="@+id/openWebButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Open Web"
android:textColor="@color/white"
android:background="@color/purple_500"
/>
</RelativeLayout>

first_widget.kt:
package com.example.exp3_widgetapp
import android.app.PendingIntent
import android.appwidget.AppWidgetManager import
android.appwidget.AppWidgetProvider import
android.content.Context import android.content.Intent import
android.net.Uri
import android.widget.RemoteViews

/**
* Implementation of App Widget functionality.
*/
class FirstWidget : AppWidgetProvider()
{ override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager, appWidgetIds: IntArray
){
// There may be multiple widgets active, so update all of them
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId)
DEPARTMEN
COMPUTER
T OF SCIENCE & ENGINEERING

}
}
}
internal fun
updateAppWidget( cont
ext: Context,
appWidgetManager: AppWidgetManager
appWidgetId: Int
)
{
//1. Create remote view
val remoteViews = RemoteViews(context.packageName,
R.layout.first_widget)
//2. define intent ---> action which will be performed
val intent = Intent(Intent.ACTION_VIEW)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.data = Uri.parse("https://insideandroid.in")
val pendingIntent = PendingIntent.getActivity(context,0,intent,0)
//3.set pending intent on view
remoteViews.setOnClickPendingIntent(R.id.openWebButton,
pendingIntent)
//4. update the widget appWidgetManager.updateAppWidget(appWidgetId,
remoteViews)
}

MainActivity.kt:
package com.example.exp3_widgetapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}}

Output:
DEPARTMEN
T OF
COMPUTER SCIENCE & ENGINEERING

You might also like