Skip to content

Commit

Permalink
Add weather forecast widget to demonstrate:
Browse files Browse the repository at this point in the history
- Responsive size layouts
- Dynamic colors
- Rounded corners including outer container and inner views
- Improved preview picker experience
  • Loading branch information
thagikura committed Sep 9, 2021
1 parent 421513f commit 2169e22
Show file tree
Hide file tree
Showing 16 changed files with 861 additions and 1 deletion.
12 changes: 12 additions & 0 deletions AppWidget/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@
android:resource="@xml/app_widget_items_list" />
</receiver>

<receiver
android:name=".WeatherForecastAppWidget"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/app_widget_weather_forecast" />
</receiver>

<service
android:name=".ItemsCollectionRemoteViewsService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.appwidget

import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.util.SizeF
import android.widget.RemoteViews

/**
* Implementation of the weather forecast app widget that demonstrates the flexible layouts based
* on the size of the device.
*/
class WeatherForecastAppWidget : AppWidgetProvider() {

override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
for (appWidgetId in appWidgetIds) {
updateWeatherWidget(context, appWidgetManager, appWidgetId)
}
}

private fun updateWeatherWidget(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetId: Int
) {
val viewMapping: MutableMap<SizeF, RemoteViews> = mutableMapOf()
// Specify the maximum width and height in dp and a layout, which you want to use for the
// specified size
viewMapping[SizeF(180.0f, 110.0f)] = RemoteViews(
context.packageName,
R.layout.widget_weather_forecast_small
)
viewMapping[SizeF(270.0f, 110.0f)] = RemoteViews(
context.packageName,
R.layout.widget_weather_forecast_medium
)
viewMapping[SizeF(270.0f, 280.0f)] = RemoteViews(
context.packageName,
R.layout.widget_weather_forecast_large
)
appWidgetManager.updateAppWidget(appWidgetId, RemoteViews(viewMapping))
}
}

26 changes: 26 additions & 0 deletions AppWidget/app/src/main/res/drawable-nodpi/ic_cloudy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2021 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M17.5,19h-10C4.47,19 2,16.53 2,13.5c0,-2.76 2.09,-5.09 4.78,-5.44C7.83,6.18 9.82,5 12,5c2.97,0 5.45,2.18 5.92,5.02C20.21,10.23 22,12.16 22,14.5C22,16.98 19.98,19 17.5,19zM12,7c-1.59,0 -3.03,0.94 -3.67,2.41L8.08,10l-0.64,0.01C5.54,10.04 4,11.61 4,13.5C4,15.43 5.57,17 7.5,17h10c1.38,0 2.5,-1.12 2.5,-2.5S18.88,12 17.5,12H16v-1C16,8.79 14.21,7 12,7z"/>
</vector>
41 changes: 41 additions & 0 deletions AppWidget/app/src/main/res/drawable-nodpi/ic_partly_cloudy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2021 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,5c0.55,0 1,-0.45 1,-1V2c0,-0.55 -0.45,-1 -1,-1s-1,0.45 -1,1v2C11,4.55 11.45,5 12,5z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M18.36,7.05l1.41,-1.42c0.39,-0.39 0.39,-1.02 0,-1.41s-1.02,-0.39 -1.41,0l-1.41,1.42c-0.39,0.39 -0.39,1.02 0,1.41C17.34,7.44 17.97,7.44 18.36,7.05z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M22,11h-2c-0.55,0 -1,0.45 -1,1s0.45,1 1,1h2c0.55,0 1,-0.45 1,-1S22.55,11 22,11z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M18.36,16.95c-0.39,-0.39 -1.02,-0.39 -1.41,0c-0.39,0.39 -0.39,1.02 0,1.41l1.41,1.41c0.39,0.39 1.02,0.39 1.41,0c0.39,-0.39 0.39,-1.02 0,-1.41L18.36,16.95z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M5.64,7.05c0.39,0.39 1.02,0.39 1.41,0c0.39,-0.39 0.39,-1.02 0,-1.41L5.64,4.22c-0.39,-0.39 -1.02,-0.39 -1.41,0s-0.39,1.02 0,1.41L5.64,7.05z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M12,6C9.03,6 6.56,8.16 6.09,11C6.06,11 6.03,11 6,11c-2.76,0 -5,2.24 -5,5s2.24,5 5,5c0.9,0 3.73,0 4.5,0c1.88,0 3.4,-1.48 3.48,-3.34C16.32,16.83 18,14.61 18,12C18,8.68 15.31,6 12,6zM10.5,19H6c-1.65,0 -3,-1.35 -3,-3s1.35,-3 3,-3c1.19,0 2.27,0.71 2.75,1.81l0.51,1.17L10.53,16C11.36,16.02 12,16.68 12,17.5C12,18.32 11.33,19 10.5,19zM13.49,15.7c-0.6,-0.99 -1.67,-1.67 -2.91,-1.7c-0.49,-1.14 -1.4,-2.05 -2.53,-2.56C8.33,9.5 9.98,8 12,8c2.21,0 4,1.79 4,4C16,13.67 14.96,15.11 13.49,15.7z"/>
</vector>
35 changes: 35 additions & 0 deletions AppWidget/app/src/main/res/drawable-nodpi/ic_rainy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2021 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M17.92,7.01C17.45,4.17 14.97,1.99 12,1.99c-2.18,0 -4.17,1.18 -5.22,3.06C4.09,5.41 2,7.73 2,10.49c0,3.03 2.47,5.5 5.5,5.5h10c2.48,0 4.5,-2.02 4.5,-4.5C22,9.15 20.21,7.23 17.92,7.01zM17.5,13.99h-10c-1.93,0 -3.5,-1.57 -3.5,-3.5C4,8.6 5.54,7.04 7.44,7l0.64,-0.01L8.33,6.4C8.97,4.94 10.41,3.99 12,3.99c2.21,0 4,1.79 4,4v1h1.5c1.38,0 2.5,1.12 2.5,2.5S18.88,13.99 17.5,13.99z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M12.89,17.56c-0.25,-0.49 -0.85,-0.69 -1.34,-0.45c-0.49,0.25 -0.69,0.85 -0.45,1.34l1.5,3c0.19,0.39 0.75,0.74 1.34,0.45c0.49,-0.25 0.69,-0.85 0.45,-1.34L12.89,17.56z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M18.89,17.56c-0.25,-0.49 -0.85,-0.69 -1.34,-0.45c-0.49,0.25 -0.69,0.85 -0.45,1.34l1.5,3c0.19,0.39 0.75,0.74 1.34,0.45c0.49,-0.25 0.69,-0.85 0.45,-1.34L18.89,17.56z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M6.89,17.56c-0.25,-0.49 -0.85,-0.69 -1.34,-0.45c-0.49,0.25 -0.69,0.85 -0.45,1.34l1.5,3c0.19,0.39 0.75,0.74 1.34,0.45c0.49,-0.25 0.69,-0.85 0.45,-1.34L6.89,17.56z"/>
</vector>
50 changes: 50 additions & 0 deletions AppWidget/app/src/main/res/drawable-nodpi/ic_sunny.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2021 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,6c-3.31,0 -6,2.69 -6,6s2.69,6 6,6s6,-2.69 6,-6S15.31,6 12,6zM12,16c-2.21,0 -4,-1.79 -4,-4s1.79,-4 4,-4c2.21,0 4,1.79 4,4S14.21,16 12,16z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M12,5c0.55,0 1,-0.45 1,-1V2c0,-0.55 -0.45,-1 -1,-1s-1,0.45 -1,1v2C11,4.55 11.45,5 12,5z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M12,19c-0.55,0 -1,0.45 -1,1v2c0,0.55 0.45,1 1,1s1,-0.45 1,-1v-2C13,19.45 12.55,19 12,19z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M18.36,7.05l1.41,-1.41c0.39,-0.39 0.39,-1.02 0,-1.41s-1.02,-0.39 -1.41,0l-1.41,1.41c-0.39,0.39 -0.39,1.02 0,1.41C17.34,7.44 17.97,7.44 18.36,7.05z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M5.64,16.95l-1.41,1.41c-0.39,0.39 -0.39,1.02 0,1.41c0.39,0.39 1.02,0.39 1.41,0l1.41,-1.41c0.39,-0.39 0.39,-1.02 0,-1.41S6.03,16.56 5.64,16.95z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M22,11h-2c-0.55,0 -1,0.45 -1,1s0.45,1 1,1h2c0.55,0 1,-0.45 1,-1S22.55,11 22,11z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M5,12c0,-0.55 -0.45,-1 -1,-1H2c-0.55,0 -1,0.45 -1,1s0.45,1 1,1h2C4.55,13 5,12.55 5,12z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M18.36,16.95c-0.39,-0.39 -1.02,-0.39 -1.41,0c-0.39,0.39 -0.39,1.02 0,1.41l1.41,1.41c0.39,0.39 1.02,0.39 1.41,0c0.39,-0.39 0.39,-1.02 0,-1.41L18.36,16.95z"/>
<path
android:fillColor="@android:color/white"
android:pathData="M5.64,7.05c0.39,0.39 1.02,0.39 1.41,0c0.39,-0.39 0.39,-1.02 0,-1.41L5.64,4.22c-0.39,-0.39 -1.02,-0.39 -1.41,0c-0.39,0.39 -0.39,1.02 0,1.41L5.64,7.05z"/>
</vector>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ appWidgetInnerRadius attribute value
android:shape="rectangle">

<corners android:radius="?attr/appWidgetInnerRadius" />
<solid android:color="?android:attr/colorAccent" />
<solid android:color="?android:attr/colorButtonNormal" />
</shape>
Loading

0 comments on commit 2169e22

Please sign in to comment.