Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
How to update more than one Remote View in Android Studio Widget

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!