Home > Back-end >  How to update more than one Remote View in Android Studio Widget
How to update more than one Remote View in Android Studio Widget

Time:03-02

That's my code for the widget

class MovieOfTheDayWidget : AppWidgetProvider() {
    override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray,
    ) {
        // There may be multiple widgets active, so update all of them
        for (appWidgetId in appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId)
        }
    }

    override fun onEnabled(context: Context) {
        // Enter relevant functionality for when the first widget is created
    }

    override fun onDisabled(context: Context) {
        // Enter relevant functionality for when the last widget is disabled
    }
}

internal fun updateAppWidget(
    context: Context,
    appWidgetManager: AppWidgetManager,
    appWidgetId: Int,
) {

    val randomMovieList = Constants.getAllMovies()
    val movie: AllMovies
    val randomMovieListSize = randomMovieList.size
    var randomPosition = Random().nextInt(randomMovieListSize)

    movie = randomMovieList[randomPosition]

    val idArray = arrayListOf<RemoteViews>()

    // Construct the RemoteViews object
    val movieName = RemoteViews(context.packageName, R.layout.movie_of_the_day_widget)
    movieName.setTextViewText(R.id.seriesMovieNameAndYearWidget, "${movie.movieName} (${movie.date}")

    val movieImg = RemoteViews(context.packageName, R.layout.movie_of_the_day_widget)
    movieImg.setImageViewResource(R.id.seriesMovieImgWidget, movie.moviePic)

    val movieRating = RemoteViews(context.packageName, R.layout.movie_of_the_day_widget)
    movieRating.setTextViewText(R.id.seriesMovieRatingWidget, movie.rating)

    idArray.add(movieName)
    idArray.add(movieRating)
    idArray.add(movieImg)
    
    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, idArray[0])
    appWidgetManager.updateAppWidget(appWidgetId, idArray[1])
    appWidgetManager.updateAppWidget(appWidgetId, idArray[2])

}

But the last 3 lines don't work properly, they only the second line works, how to update all of the RemoteViews in the idArray. I've tried putting it in a for loop and updating i, but it also didn't work. Is it even the wright thing to call the updateAppWidget function more than once? I've tried passing more than one parameter to the function but it returned an error.

CodePudding user response:

You are correct, appWidgetManager.updateAppWidget() should be called just once.

You need to apply all changes to the same RemoteViews instance, so it should be:

// Construct the RemoteViews object
val remoteViews = RemoteViews(context.packageName, R.layout.movie_of_the_day_widget)

remoteViews.setTextViewText(R.id.seriesMovieNameAndYearWidget, "${movie.movieName} (${movie.date}") 
remoteViews.setImageViewResource(R.id.seriesMovieImgWidget, movie.moviePic)
remoteViews.setTextViewText(R.id.seriesMovieRatingWidget, movie.rating)

// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, remoteViews)
  • Related