pimp my widget!

55
Pimp my widget! Photo: Guillame Mailheiro

Upload: others

Post on 13-Apr-2022

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 2: Pimp my widget!

Hello! whoami?

āœ“ Marco Zanetti

āœ“ Android Developer in Synesthesia

āœ“ Currently using 12 widgets, proudly

Page 3: Pimp my widget!

What are we talking about?

First: Basics

1. What is a widget?

2. Do people really use

them?

3. How to build one?

Then: Power up!

1. Whatā€™s new in Android 12?

2. Backwards compatibility

3. Question time

Page 4: Pimp my widget!

Basics

Page 5: Pimp my widget!

Whatā€™s a Widget?!?

If you ask a poet:

ā€œA widget is a snack while your app is the mealā€

...and itā€™s actually from the official Android docs

If you ask Lars Vogel:

ā€œHome screen widgets are broadcast receivers which provide interactive componentsā€

Page 6: Pimp my widget!

Whatā€™s a Widget?!?

If you ask me:

ā€œA widget is a small portion of your app running into the launcherā€™s

process and orchestrated by a Broadcast Receiver with

superpowersā€

Page 7: Pimp my widget!

Does anybody really use them?

Source: Androidpolice (not a truly representative dataset, I know)

Page 8: Pimp my widget!

How do they work: The toolkit

1. AppWidgetProviderInfo ā†’ XML file

providing widget basic information

2. AppWidgetProvider ā†’ A

BroadcastReceiver with some

convenience methods for widgets

3. A xml layout for the widget

4. RemoteViews

Page 9: Pimp my widget!

How do they work: RemoteViews

ā— Allow a process to describe a View hierarchy to another process

ā— Used for Push notifications and Widgets

ā— Can include: LinearLayout, GridView, RelativeLayout, Button,

ImageView, TextView... no ConstraintLayout, EditText etc.

ā— API 31 (Android 12) allows CheckBox, RadioButton, RadioGroup,

Switch

Page 10: Pimp my widget!

How do they work

RemoteViews

Processing

Action

InteractionAppWidgetManager.update(widgetId, remoteViews)

AppWidgetProvider.onReceive(context, intent)

PendingIntent.getBroadcast(...)

PendingIntent.getActivity(...) ā†’ deeplink

Actions on widget

New data Old data

creation

Life inside a AppWidgetProvider

Thank you Łukasz Marczak

Page 11: Pimp my widget!

Letā€™s make one!

Letā€™s create a beautiful ugly widget for

- Displaying deviceā€™s brand and model in a

TextView

- Displaying current date and time in a toast

when the widget is clicked

Page 12: Pimp my widget!

Letā€™s make one! Step 1/5

Widget Provider Info XML file

Page 13: Pimp my widget!

Letā€™s make one! Step 2/5

Widget XML layout

Page 14: Pimp my widget!

Letā€™s make one! Step 3/5

BroadcastReceiver in

app Manifest

Page 15: Pimp my widget!

Letā€™s make one! Step 4/5

Page 16: Pimp my widget!

Letā€™s make one! Step 4/5

RemoteViews

Processing

Action

Interaction

AppWidgetProvider.onReceive(context, intent)

PendingIntent.getBroadcast(...)

New data Old data

Life inside a AppWidgetProvider

Page 17: Pimp my widget!

Letā€™s make one! Step 5/5

AppWidgetProvider ā†’ onReceive method

Page 18: Pimp my widget!

Letā€™s make one! Step 5/5

RemoteViews

Processing

Action

Interaction

AppWidgetProvider.onReceive(context, intent)

PendingIntent.getBroadcast(...)

Life inside a AppWidgetProvider

Page 19: Pimp my widget!

Power up!

Page 20: Pimp my widget!

Android 12: fancy, roundy, glossy

Page 21: Pimp my widget!

1. Responsive layouts

Resizing widgets?

A lot of space wasted!

Android 12 fixes this with style

šŸ˜Ž

Page 22: Pimp my widget!

1. Responsive layouts

Page 23: Pimp my widget!

1. Responsive layouts

Documentation: Provide flexible layouts

We can still include xml layouts

Page 24: Pimp my widget!

1. Responsive layouts - compatibility

Not available on API < 31

Page 25: Pimp my widget!

2. Rounded corners

official documentation

Page 26: Pimp my widget!

2. Rounded corners - Compatibility

- Donā€™t place content in the corners :-)

- If you already have rounded corners, align them

to system values in API 31

Page 27: Pimp my widget!

3. Configuration

Reconfigure on demand

Page 28: Pimp my widget!

3. Configuration

Optionality. Remember to provide default data!

Page 29: Pimp my widget!

4. Widget description

Add android:description in the

widget-provider-info xml file

Page 30: Pimp my widget!

4. Widget description - Compatibility

Choose a good preview as well!

Remember you can give your

widget a label with

android:label in

BroadcastReceiver in the

manifest

Page 31: Pimp my widget!

4. Widget description - Compatibility

Android < 12 ā†’ label Android 12 ā†’ label + description

Page 32: Pimp my widget!

5. Scalable previews

Actual vector LOCALIZED preview of your widget

as a thumbnail

Page 33: Pimp my widget!

5. Scalable previews - Compatibility

ā— Implement previewLayout and previewImage

ā— You need a dedicated layout for the preview

ā— Localize your strings

Page 34: Pimp my widget!

6. Theming šŸŒˆMaterial You infers palette from the background + Light/Dark theme

Page 35: Pimp my widget!

6. Theming šŸŒˆColor groups: accent1, accent2, accent3, neutral1, neutral2.

13 shades for each: the lightest being coded with 0, the darkest ā€” 1000

system_accent3_0 // the lightest shade of accent color #3 system_accent3_10system_accent3_50 system_accent3_100system_accent3_200system_accent3_300ā€¦system_accent3_1000 // the darkest shade of accent color #3

Android devs post on medium

Page 36: Pimp my widget!

6. Theming šŸŒˆ - Compatibility

Create a custom styles and override them for Android 12

values/styles.xml

values-31/styles.xml

Page 37: Pimp my widget!

6. Theming šŸŒˆ

Page 38: Pimp my widget!

7. Voice support

Google Assistant can display a widget:

ā— As a response to a specific query

ā— Because a vocal query triggered a

Built-in-intent the app has registered to

More:

https://developers.google.com/assistant/app/widgets

Page 39: Pimp my widget!

8. Stateful checkboxes and radio buttons

Check the state of checkboxes and radio buttons

in RemoteViews

Compatibility ā†’ Provide two layouts for each

widget or use workarounds

Page 40: Pimp my widget!

8. Stateful checkboxes and radio buttons

In lists donā€™t map each checkbox to a single

Pending Intent. Use the following

Page 41: Pimp my widget!

9. Simplified RemoteViews on collections

When loading lists you donā€™t need anymore a

RemoteViewsService returning RemoteViewsFactory.

Just the following (available through androidx in API < 31)

Official docs

Page 42: Pimp my widget!

A recap

ā— Widgets were tricky and still are

ā— Android 12 introduced some tasty

new features

ā— Still devs need to invest quite a

lot of effort in backwards

compatibility

ā— We expect this effort to get lower

as Android 12 spreads

Page 43: Pimp my widget!

Any questions?

Slides šŸ‘‡ https://www.marcozanetti.it

Write me šŸ‘‡

[email protected]

marco-zanetti-really

Page 44: Pimp my widget!

Thatā€™s all, folks

Photo: Guillame Mailheiro

Thank you for watching

Slides šŸ‘‡ https://www.marcozanetti.it

Write me šŸ‘‡

[email protected]

marco-zanetti-really

Page 45: Pimp my widget!
Page 46: Pimp my widget!
Page 47: Pimp my widget!
Page 48: Pimp my widget!

Widgets have been around for some time

Widgets: 2009 Up: 2009 The hangover: 2009

Page 49: Pimp my widget!

Itā€™s time to pimp ā€˜em up!

Page 50: Pimp my widget!

Itā€™s time to pimp ā€˜em up!

Rounded corners Checkboxes

Responsive layouts

And much, much more...

Page 51: Pimp my widget!

8. RemoteViews improved

Methods added to RemoteViews to change details at runtime.

Examples:

ā— setColorStateList(), changing the color of a progress bar at runtimeā— setViewLayoutMargin(), setting exact margins for RemoteViewsā— setRemoteAdapter(int viewId,

RemoteViews.RemoteCollectionItems items) to pass a whole collection of RemoteItems at the same time.

Page 52: Pimp my widget!

How do they work: The toolkit

From official Android documentation

Page 53: Pimp my widget!

How do they work: RemoteViews

This happens in your app This happens in the launcherPicture from LearningTree

Page 54: Pimp my widget!

Ok but does anybody use widgets?

Poll by droid-life.com, 2016 Poll by Nextpit.com, 2019

Page 55: Pimp my widget!

3. Configuration - Compatibility

Flag already available on Android > 8

Reconfiguration from a much less intuitive action point