I have weather app when i need to show an AppWidget on Home Screen , I could pull it from widgets and stick it to my device's home screen , the issue is that when i delete the widget , i no longer can find in my home screen widgets , tried to uninstall app and install it again , still not able to find the widget , i appreciate your help to find a solution for this issue . Thank you
- My AppWidget Class
class WeatherWidget : AppWidgetProvider() {
override fun onUpdate(context: Context?, appWidgetManager: AppWidgetManager?, appWidgetIds: IntArray?) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
appWidgetIds?.forEach { appWidgetId ->
updateWidget(context!!, appWidgetManager!!,appWidgetId)
}
}
private fun updateWidget(context: Context,appWidgetManager: AppWidgetManager,appWidgetId : Int){
val remoteViews = RemoteViews(context.packageName,R.layout.app_widget_layout)
remoteViews.setTextViewText(R.id.weatherRain,
Utils.getWeatherData(context)["rain"].toString()
)
remoteViews.setTextViewText(R.id.weatherWind,
Utils.getWeatherData(context)["wind"].toString()
)
remoteViews.setTextViewText(R.id.weatherHumidity,
Utils.getWeatherData(context)["humidity"].toString()
)
appWidgetManager.updateAppWidget(appWidgetId,remoteViews)
}
}
- Xml File
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/app_widget_layout"
android:initialLayout="@layout/app_widget_layout"
android:minHeight="350dp"
android:minWidth="150dp"
android:previewImage="@drawable/cloudy_sunny"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="1800000"
android:widgetCategory="home_screen"/>
- Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".di.WeatherApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Learning"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".widgets.WeatherWidget"
android:exported="false">
<intent-filter >
<action
android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/weather_app_widget" />
</receiver>
</application>
</manifest>