Insider's Guide To Udacity Android Developer Nanodegree Part 3 - Making the Baking App |
Written by Nikos Vaggalis |
Monday, 03 July 2017 |
Page 5 of 8
The one for Baking App is stored in 'baking_widget_info.xml'.This file contains all the necessary information in setting up the widget's initial appearance according to the following specifications: android:initialLayout="@layout/baking_widget_before_recipe" android:previewImage= "@drawable/ic_chrome_reader_mode_black_48dp"
/**** layout/baking_widget_before_recipe.xml ****/ <RelativeLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:background= "@drawable/ic_chrome_reader_mode_black_48dp"
android:textAlignment="viewStart"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:contentDescription="@string/appwidget_text" android:text="@string/appwidget_text" android:textColor="#ffffff" android:textSize="14sp" android:fillViewport="false" android:textStyle="bold|italic" android:layout_alignParentRight="false" android:layout_alignParentBottom="false" android:layout_centerInParent="false" android:layout_centerHorizontal="false" android:layout_centerVertical="false" android:layout_alignParentTop="false" android:layout_alignParentStart="true" /> </RelativeLayout>
/**** layout/widget_grid_view.xml ****/ <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"> <GridView android:id="@+id/widget_grid_view" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="1" android:columnWidth="25dp" android:layout_margin="2dp" android:horizontalSpacing="5dp" android:verticalSpacing="5dp" /> </FrameLayout>
/**** 'layout/widget_grid_view_item.xml' ****/ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:background="#09C" android:orientation="vertical"> <TextView android:id="@+id/widget_grid_view_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAlignment="viewStart" android:textStyle="bold" android:textSize="10sp" android:layout_margin="3dp" /> </LinearLayout>
or
to implement our own AppWidget's functionality.
<receiver android:name=".widget.BakingWidgetProvider"
android:icon="@drawable/ic_art_track_black_36dp">
<intent-filter> <action android:name=
"android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name=
"android.appwidget.action.APPWIDGET_UPDATE2" />
</intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/baking_widget_info" /> </receiver>
The
action, a custom action that deviates from the default
is in fact notified that there was an update in the parent app and that there's also an associated bundle (the list of ingredients) attached to it. In turn the BakingWidgetProvider extracts that list to get all widget instances associated with our BakingWidgetProvider and refresh their state. It Is widget(s) in the plural since the user can place as many widget instances as he sees fit on the home screen. So this code notifies all of them of the change so that they can update their views with the new information. Here is where the RemoteViews come into play: RemoteViews views = new RemoteViews( context.getPackageName(), R.layout.widget_grid_view);
Intent intent = new Intent(context, GridWidgetService.class); views.setRemoteAdapter(R.id.widget_grid_view, intent);'
In essence GridWidgetService provides the RemoteView with its shape so it needs to extend the RemoteViewsService base class which connects the RemoteAdapter adapter to its RemoteView. Since it's a Service it also needs to be registered in our app's manifest:
<service android:name=".widget.GridWidgetService" android:permission= "android.permission.BIND_REMOTEVIEWS" />
What's left is to set each Remote GridView cell (widget_grid_view_item) to the proper ingredient inside
|
Last Updated ( Monday, 20 November 2017 ) |