Cosc 5/4730 Android App Widgets. App Widgets App Widgets are miniature application views that can be embedded in other applications (such as the Home.

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
Advertisements

Android User Interface
Mobile Computing Lecture#13. Lecture Contents 2 Widgets  Creating App Widget  Unsupported/Supported Views/Layouts  Widget Layout  Widget Settings.
WIDGET PLAYER STEPHEN NEWBY. WHAT ARE WIDGETS? Widgets are placed on the Android device’s home screen Can be added, removed, resized, moved at will Stay.
Chapter 6: Jam! Implementing Audio in Android Apps.
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
CSS216 MOBILE PROGRAMMING Android, Chapter 10 Book: “Professional Android™ 2 Application Development” by Reto Meier, 2010 by: Andrey Bogdanchikov (
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
CS378 - Mobile Computing What's Next?. Fragments Added in Android 3.0, a release aimed at tablets A fragment is a portion of the UI in an Activity multiple.
Android development the first app. Andoid vs iOS which is better? Short answer: neither Proponents on both sides For an iOS side, see this article on.
CS5103 Software Engineering Lecture 08 Android Development II.
© Keren Kalif Intro to Android Development Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation.
Chapter 9: Customize! Navigating with Tabs on a Tablet App.
6-2 2D Graphics CSNB544 Mobile Application Development Thanks to Utexas Austin.
Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Simplify! The Android User Interface
Broadcast Receiver Android Club Agenda Broadcast Receiver Widget.
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
Android Layouts. Layouts Define the user interface for an activity Layouts are defined in.xml files – within /res/layout folder – different layout can.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Homescreen Widgets Demystified Pearl AndroidTO // Oct 26, 2010.
Mobile Programming Lecture 17 Creating Homescreen Widgets.
Using Intents to Broadcast Events Intents Can be used to broadcast messages anonymously Between components via the sendBroadcast method As a result Broadcast.
Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.
DUE Hello World on the Android Platform.
CS378 - Mobile Computing Intents.
Frank Xu Gannon University.  Linear Layout  Relative Layout  Table Layout.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
INTRODUCTION TO ANDROID. Slide 2 Application Components An Android application is made of up one or more of the following components Activities We will.
SpotOn Game App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
Android Boot Camp for Developers Using Java, 3E
User Interfaces: Part 1 (View Groups and Layouts).
Configuring Android Development Environment Nilesh Singh.
Presented By: Muhammad Tariq Software Engineer Android Training course.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
First Venture into the Android World Chapter 1 Part 2.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
Pearson Webcast Series
CS378 - Mobile Computing User Interface Basics. User Interface Elements View – Control – ViewGroup Layout Widget (Compound Control) Many pre built Views.
Building User Interfaces Basic Applications
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
ANDROID LAYOUTS AND WIDGETS. Slide 2 Introduction Parts of the Android screen Sizing widgets and fonts Layouts and their characteristics Buttons, checkboxes.
Cosc 5/4730 Support design library. Support Design library Adds (API 9+) back support to a number of 5.0 lollipop widgets and material design pieces –
Cosc 4735 Nougat API 24+ additions.
Chapter 2: Simplify! The Android User Interface
Java FX: Scene Builder.
Android Mobile Applications Course Introduction
Android Application Development 1 6 May 2018
Android – Event Handling
Messaging Unit-4.
Android Widget Tutorial
Creation of an Android App By Keith Lynn
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
Android Programming Lecture 9
CIS 470 Mobile App Development
Android Layout Basics Topics
CS5103 Software Engineering
Android Notifications (Part 2) Plus Alarms and BroadcastReceivers
CA16R405 - Mobile Application Development (Theory)
Android Topics Threads and the MessageQueue
CIS 470 Mobile App Development
Android Sensor Programming
CA16R405 - Mobile Application Development (Theory)
Presentation transcript:

cosc 5/4730 Android App Widgets

App Widgets App Widgets are miniature application views that can be embedded in other applications (such as the Home screen and lock screen) and receive periodic updates. These views are referred to as Widgets in the user interface, and you can publish one with an App Widget provider. An application component that is able to hold other App Widgets is called an App Widget host. Example: Music App Widget.

Basics You need the following: AppWidgetProviderInfo object – Describes the metadata for an App Widget, such as the App Widget's layout, update frequency, and the AppWidgetProvider class. Defined in XML. View layout – Defines the initial layout for the App Widget, defined in XML AppWidgetProvider class implementation – Defines the basic methods that allow you to programmatically interface with the App Widget, based on broadcast events. – This is a BroadcastReceiver. An Optional Activity, that can be used to configure the app. – Note it’s required (as far as I can) in 4.2 or the widget will not work.

Basics (2) The AppWidgetProvider implements the onRecieve() method of the BroadcastReceiver and then calls: – OnEnabled() when first instance is added to homescreen – onDisabled() when last instance removed from homescreen – onDelete() when the widget is removed from homescreen – onUpdate() called for every update of the widget. A note, your widget can be added to the homescreen more then once.

Basics(3) BroadcastReceiver – registered to receive system messages and Intents. – A BroadcastReceiver will get notified by the Android system, if the specified situation happens. For example a BroadcastReceiver could get called once the Android system completed the boot process or if a phone call is received. In the case of app widgets, an example is a click event Services (for advanced, but good place to define it) – perform background tasks without providing a user interface. They can notify the user via the notification framework in Android.

Manifestfile.xml In the application section, we will have the activity and we need to add the broadcastreceiver and intent-filter <meta-data android:name="android.appwidget.provider" /> widget_info is the xml file that describes the widget in xml. – Note widget_info is just a file name and your file can have different names. (or more then one if you have multiple widgets).

AppWidgetProviderInfo My file is called widget_info.xml in res/xml directory eclipse created the xml directory for me <appwidget-provider xmlns:android=" android:minHeight="40dp" android:minWidth="40dp" android:resizeMode="horizontal|vertical" android:updatePeriodMillis=" " android:widgetCategory="keyguard|home_screen" >

AppWidgetProviderInfo (2) Attributes: minWidth and minHeight is minimum space an App widget uses by default. – 1 cell is 40 dp, 2 is 110dp, 3 cells are 180dp, 4 cells are 250dp. It recommended you don’t use more then 4x4 as default, because of screen size. both height and width See /widget_design.html#anatomy_determining_size to determine size. /widget_design.html#anatomy_determining_size

AppWidgetProviderInfo (3) updatePeriodMillis – How often an update to this widget is made. – A note, it can wake up every couple of minutes, but this will really drain the battery. This wakeup will happen even when the phone is asleep. – Google recommends no more lower then 30 minutes and prefers an hour. – Setting to 0, means it will not update without user intervention.

AppWidgetProviderInfo (4) initialLayout points to layout resource the widget uses. Configure points to the activity the app uses to configure it self on launch (optional) – Example: edu.cs4730.widgetdemo.confActivity The rest of for v3.0+ and above minResizeWidth and minResizeHeight are how small it can be resized to android:resizeMode="horizontal|vertical" allows the widget to be resized in both directions. android:widgetCategory="keyguard|home_screen" allows the widget to on the homescreen and the lockscreen. There are a couple more as well for previews and other stuff.

View layout. There will be in two pieces. – widget layout and the background layout The layout in the app is a RemoteViews and has limited support for widgets and layouts – FrameLayout, LinearLayout, and RelativeLayout – AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar, and TextView – v3.0+ added: GridLayout, ViewFlipper, ListView, GridView, StackView, and AdapterViewFlipper The adapter is for a collection view widget.

Example layout <LinearLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="8dip" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center_horizontal|center_vertical" android:layout_margin="4dip" android:text="Text" > This is the background xml, that we’ll create next. Other then the background, then standard layout file

Myshape.xml <shape xmlns:android=" android:shape="rectangle"> <stroke android:width="2dp" android:color="#FFFFFFFF" /> <gradient android:angle="225" android:endColor="#DD2ECCFA" android:startColor="#DD000000" /> <corners android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" android:topLeftRadius="7dp" android:topRightRadius="7dp" /> In /res/drawable/myshape.xml See For more information on shapes.

App Tutorial This is a very simple widget. It’s a button that displays a random number It will update every thirty minutes milliseconds Also registers a OnClickListener so random number updates when the user clicks on it.

AppWidgetProvider Because the app is simple enough we will only use the onUpdate method Remember our widget can be placed on the homescreen multiple times, so have to deal with all of them – As note, the code has comments on how to configure it to update all widgets or just the widget that was clicked.

AppWidgetProvider (2) public class Example extends AppWidgetProvider public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // Get all ids ComponentName thisWidget = new ComponentName(context, Example.class); int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); for (int widgetId : allWidgetIds) { // Create some random data int number = (new Random().nextInt(100)); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example); // Set the text remoteViews.setTextViewText(R.id.update, String.valueOf(number)); //next set the listener.

AppWidgetProvider (3) // Register an onClickListener Intent intent = new Intent(context, Example.class); intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); int[] ids = {widgetId}; //only update this widget, not all! intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent); appWidgetManager.updateAppWidget(widgetId, remoteViews); }

Config activity The activity that is launched at the time the widget is put on the homescreen. – It’s intended to configure the widget. – This is a normal activity with one exception It must use setResult(RESULT_OK, “result value”) and finish() or the widget will not be installed on the homescreen. – In the AppWidgetProviderInfo the following line is needed: – android:configure="edu.cs4730.widgetdemo2.Widgetd2Config“ Widgetd2Conf is the activity now and the rest is the package.

Example widgetDemo2.zip is provided as an example. – This functions as the lecture has explained. – But the configuration uses sharedPreferences, so no matter how many widgets are placed on the screen, they use the same info and all update when one of them is clicked. If at some point, I can figure out how to get widget controlled separately, I’ll post widgetDemo3.zip and the preferences will be changed as well.

Example (2) widgetDemo is also included – This is more complex and uses a preference object for each widget. The data is stored/loaded from the configure activity. Several methods are now static as well. – This example matches the template more closely. – The code is the same as example used through out this lecture.

Advanced A BroadcastReceiver must finish the onReceive() in 5 seconds or less. – So for long actions (network access as example) So we create a service to run in the background from the onUpdate(), which can then update the widget (and take as long as needed as well). – See the simpleWidgetExample.zip for how to integrate the service into the provider.

References See the github for source code and more examples. le.html le.html tutorial/ tutorial/ s/index.html s/index.html elines/widget_design.html elines/widget_design.html

Q A &