Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

100
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda