You are on page 1of 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 1.3

Student Name: Akhilesh Singh


Branch: CSE Section/Group:21BCS CC-644-B
Semester: 6th Date of Performance: 05/02/2024
Subject Name: MAD Lab Subject Code: 21CSH-355

1. Aim:
Create Application by Using Widgets.

2. Objective:
To understand the concept of widgets and to implement the Widgets in Android
Studio.

3. Procedure:
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a
New Project in Android Studio. We are implementing it for both Java and Kotlin
languages.

Step 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, and select app widget.

Step 3: Install and Run the Code


Install and run the code on Android Virtual Device (AVD) or a personal device.
Open the widget section of the phone, lookup for a widget with the Application
name, select it, bring it to the home screen. Try changing the dimensions and we
are done! During this selecting and deploying process, a few extra files are
generated and minor changes are made to existing files as well.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

a) New*Widget+AppWidget

b) Assign Class Name and Cell min. height &width


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Step4: Write code in file


Make changes in datetimewidget.java, date time widget.xml, dimens.xml and
date time widget info.xml.

a) datetimewidget.java package
com.example . widgetexp33;
import android. appwidget .
AppWidgetManager; import android.
appwidget . AppWidgetProvider; import
android. content . Context; import android.
widget . RemoteViews;

* Implementation of App Widget functionali ty .


public class DateTimeWidget extends AppWidgetProvider {
static void updateAppWidget (Context context,
AppWidgetManager appWidgetManager, int appWidgetId) {

Char Sequence widget Text context . get String (R. string . appwidget text) ,
// Construct the RemoteViews object
RemoteViews views new RemoteViews (context . get
PackageName ( ) R. layout. date time widget) ; views . setTextViewText
(R. i d. appwidget text, widget Text) ;

// Instruct the widget manager to update the widget appWidgetManager


. updateAppWidget (appWidgetId, views) ;

@Override public void onUpdate (Context context, AppWidgetManager


appWidgetManager, int [ ] appWidgetIds)
// There may be mul tip le widgets active, so update all of them
for (int appWidgetId appWidgetIds) upda teAppWidget (context,
appWidgetManager, appWidgetId) ;

@Override public void onEnab1ed


(Context context) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

// Enter relevant
functionality for when
the first widget is
created
@Override public void onDisabled
(Context context) {
// Enter relevant
functionality for when
the last widget is
disabled

< RelativeLayout xmlns : android="http : / / schemas .


android.com/apk/res/android" . Widgetexp33 .
AppWidget . Container " android: layout width="match parent" android:
layout height="match parent"

android : theme=" @ style / Theme . Widgetexp33 . AppWidgetContainer" >

< TextC10ck android: id="@+id/appwidget text "


style="@style/Widget . Widgetexp33 . AppWidget .
Inner View" android: format12Hour="HH:mm
dd/MM/yyyy" android: layout width="wrap content"
android: layout height="wrap content" android: layout
center Horizontal = "true" android: layout
centerVertical="true" android: layout margin=" 8dp"
android : te "

android: textSize="24sp" android:


text italic" / > < /
RelativeLayout>

c) date_time_widget_info.xml
<?xml . 0" encoding="utf—8" ? >
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

<appwidget—provider xmlns : android="http : // schemas .


android.com/apk/res/android" android:description="@string/app widget
description" android: initialKeyguardLayout="@layout/date time widget"
android: time widget" android : minWidth=" 18
Odp " android : minHeight=" 4 Odp "
android:previewImage="@drawable/example appwidget preview"
android:previewLayout="@Iayout/date time widget " android:
resizeMode="horizontaI I vertical " android : targetCelIWidth=" 3 " android :
targetCellHeight=" 1 " android : updatePeriodMi11is=" 86400000 " android:
screen" / >
4.0utput:

5.Observations/Outcome:
• Learnt about Widget Functionality.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

• Learnt to create new Widget and how to configure it.


' Learnt to build a simple Widget.
• Learnt to run the built app & widget in android device.

6. Learning Outcomes:
I successfully setup Android Studio, and created my first widget in Android Studio.

You might also like