MAD LAB WORKSHEET 3 - Utkarsh
MAD LAB WORKSHEET 3 - Utkarsh
T OF
Experiment Title 3
Student Name: Siharth Kumar UID: 20BCS1997
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