0% found this document useful (0 votes)
14 views10 pages

Worksheet 1.3 MAD

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 1.3

Student Name – HIMANSHU UID -20BET1033


Branch: IT Section/Group - Subject
Semester: 6th Code: 20ITP-356
Subject Name: Mobile Application Development Lab

1. Aim: Create Application by Using Widgets.

2. Objective: Create Application by Using Widgets.

Widgets are the micro-version of the application that consists of some functionality of the application that is displayed
only on the Home Screens or the Lock Screen. For example, we see Weather, Time, Google Search Bars on the Home
Screen, and FaceLock, FingerprintLock on the
Lock Screen, which are some of the Widgets available on the device. Widgets come along with the Application when you
install it or download it from the Web. Generally, phones come with a manufacturing configuration but such elements
can be adjusted by a user later in time.

3. System Requirements:
• Microsoft Windows 7/8/10 (32-bit or 64-bit)
• 4 GB RAM minimum, 8 GB RAM recommended (plus 1 GB for the Android Emulator)
• 2 GB of available disk space minimum, 4 GB recommended (500 MB for IDE plus 1.5 GB for Android SDK and
emulator system image)
• 1280 x 800 minimum screen resolution
• Java JDK5 or later version
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
• Java Runtime Environment (JRE) 6 Android Studio

4. Steps/Program:

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, select it.
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.

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. No programming is required for generating a basic widget and is only required if an application is to be
embedded inside the widget, as discussed in the later parts of the article. Let us now explain the newly generated files
the changes make to the existing ones, one by one.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

1. NewAppWidget.kt :-
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

import android.appwidget.AppWidgetManager; import

android.appwidget.AppWidgetProvider; import

android.content.Context; import

android.widget.RemoteViews; class NewAppWidget extends

AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){ for

(int appWidgetId : appWidgetIds) {updateAppWidget(context, appWidgetManager, appWidgetId);

@Override public void onEnabled(Context context){ super.onEnabled(context);

@Override public void onDisabled(Context context){ super.onDisabled(context);

}private void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)

{
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
String widgetText = context.getString(R.string.appwidget_text);

RemoteViews views = new RemoteViews(context.getPackageName(),

R.layout.new_app_widget); views.setTextViewText(R.id.appwidget_text, widgetText);

appWidgetManager.updateAppWidget(appWidgetId, views);

}}
2. new_app_widget.xml :-

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent" android:layout_height="match_parent"

android:background="#09C" android:padding="@dimen/widget_margin">

<TextView

android:id="@+id/appwidget_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true" android:layout_centerVertical="true"

android:layout_margin="8dp" android:background="#09C"
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
android:contentDescription="@string/appwidget_text"

android:text="@string/appwidget_text" android:textColor="#ffffff"

android:textSize="24sp"

android:textStyle="bold|italic" />
</RelativeLayout>

3. dimens.xml :-

<?xml version="1.0" encoding="utf-8"?>

<resources>

<dimen name="widget_margin">8dp</dimen>

</resources>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
4. new_app_widget_info.xml :-

<?xml version="1.0" encoding="utf-8"?> <appwidget-provider

xmlns:android="http://schemas.android.com/apk/res/android"

android:initialKeyguardLayout="@layout/new_app_widget" android:initialLayout="@layout/new_app_widget"

android:minWidth="40dp" android:minHeight="40dp"

android:previewImage="@drawable/example_appwidget_preview" android:resizeMode="horizontal|vertical"

android:updatePeriodMillis="86400000" android:widgetCategory="home_screen">

</appwidget-provider>

5. Changes made to AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="org.geeksforgeeks.widget_basic">

<application

android:allowBackup="true"
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
android:icon="@mipmap/ic_launcher" android:label="@string/app_name "

android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true"

android:theme="@style/AppTheme">

<receiver android:name=".NewAppWidget">

<intent-filter>

<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />


</intent-filter>
<meta-data

android:name="android.appwidget.provider"

android:resource="@xml/new_app_widget_info" />

</receiver>

<! Until Here >

<activity android:name=".MainActivity">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter></activity></application></manifest>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Learning outcomes (What I have learnt):

• To design an android application which uses widget in android studio.


• Learnt about running application on android studio.

You might also like