0

I have an Android widget for the home screen, whose layout is basically an ImageView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_ultraviolet_widget_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center">

    <ImageView
        android:id="@+id/iv_ultraviolet_widget_image"
        android:layout_width="@dimen/widget_size"
        android:layout_height="@dimen/widget_size"
        android:src="@drawable/ic_uvi_loading" />

</RelativeLayout>

The ImageView will be updated over the time with the "current" value, which is represented with an image (a drawable) using this code:

/**
* Updates ultraviolet index widget view.
*/
@JvmStatic
private fun updateWidgetViews(context: Context, appWidgetId: Int, @DrawableRes drawableId: Int) {
    val widgetView = RemoteViews(context.packageName, R.layout.ultraviolet_widget)

    // Set an Intent to launch MainActivity when clicking on the widget
    val intent = Intent(context, MainActivity::class.java)
    val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
    widgetView.setOnClickPendingIntent(R.id.rl_ultraviolet_widget_layout, pendingIntent)

    val appWidgetTarget = AppWidgetTarget(context, R.id.iv_ultraviolet_widget_image, widgetView, appWidgetId)

    Glide
        .with(context)
        .asBitmap() // This is needed because AppWidgetTarget is a Bitmap target. See: https://github.com/bumptech/glide/issues/2717#issuecomment-351791721
        .transition(BitmapTransitionOptions.withCrossFade())
        .load(drawableId)
        .into(appWidgetTarget)

    pushWidgetUpdate(context, appWidgetId, widgetView)
}

/**
* Pushes update for a given widget.
*/
@JvmStatic
fun pushWidgetUpdate(context: Context, appWidgetId: Int, remoteViews: RemoteViews) {
    val manager = AppWidgetManager.getInstance(context)
    // Remember to check always for null in platform types (types ended in !).
    // See: https://stackoverflow.com/a/43826700/5189200
    manager?.let {
        manager.updateAppWidget(appWidgetId, remoteViews)
        Timber.d("Widget %d was updated", appWidgetId)
    }
}

My question is how to also update the contentDescription property in the widget's ImageView with a description on every update and using Glide, so that my widget is more accessible and it can be used with TalkBack. I have searched, but I haven't found any example with Glide and a remote view.

Thanks in advance,

Xavi

3 Answers 3

1

You can add listener to Glide when load an image. So, when load image success, you update the contentDescription of imageView.

You can read how to add listener to Gilde in this: https://github.com/codepath/android_guides/wiki/Displaying-Images-with-the-Glide-Library#loading-errors

GlideApp.with(context)
    .load("http://via.placeholder.com/300.png")
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.imagenotfound)
    .listener(new RequestListener<Drawable>() {
        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
            // log exception
            Log.e("TAG", "Error loading image", e);
            return false; // important to return false so the error placeholder can be placed
        }

        @Override
        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
            // load image success, update the contentDescription here
            return false;
        }
    })
    .into(ivImg);
0
0

Thanks to the help of @Nguyễn Quang Thọ, I found the solution. First, I created a data class (a tuple) in Kotlin for holding drawable and its associated string:

data class UltravioletImageContents(@DrawableRes val drawableId: Int, val contentDescription: String)

Second, I used the Glide result listener to add the contentDescription update and push the changes to the widget in my updateWidgetViews function.

@JvmStatic
private fun updateWidgetViews(context: Context, appWidgetId: Int, ultravioletImageContents: UltravioletImageContents) {
    val widgetView = RemoteViews(context.packageName, R.layout.ultraviolet_widget)

    // Set an Intent to launch MainActivity when clicking on the widget
    val intent = Intent(context, MainActivity::class.java)
    val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
    widgetView.setOnClickPendingIntent(R.id.rl_ultraviolet_widget_layout, pendingIntent)

    // Set image
    val appWidgetTarget = AppWidgetTarget(context, R.id.iv_ultraviolet_widget_image, widgetView, appWidgetId)
    Glide
        .with(context)
        .asBitmap() // This is needed because AppWidgetTarget is a Bitmap target. See: https://github.com/bumptech/glide/issues/2717#issuecomment-351791721
        .transition(BitmapTransitionOptions.withCrossFade())
        .load(ultravioletImageContents.drawableId)
        .listener(object : RequestListener<Bitmap> {
            override fun onLoadFailed(e: GlideException?, model: Any?, target: com.bumptech.glide.request.target.Target<Bitmap>?, isFirstResource: Boolean): Boolean {
                Timber.e(e, "Drawable cannot be loaded and set in widget image view")
                return false // Important to return false so the error placeholder can be placed
            }

            override fun onResourceReady(resource: Bitmap?, model: Any?, target: com.bumptech.glide.request.target.Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                // Set contentDescription in order to enable accessibility and be usable with TalkBack
                widgetView.setContentDescription(R.id.iv_ultraviolet_widget_image, ultravioletImageContents.contentDescription)

                pushWidgetUpdate(context, appWidgetId, widgetView)

                return false
            }
        })
        .into(appWidgetTarget)
}

Lastly, I would like to remark that pushWidgetUpdate was moved inside onResourceReady, since I think it doesn't have sense to update my widget without the image (but, maybe, I am missing something).

0

Here is a sample in Kotlin

Glide.with(context)
    .load(data.imageUrl) // URL Of Image
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.image_not_found)
    .listener(object : RequestListener<Drawable> {
        override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
            return false // important to return false so the error placeholder can be placed
        }

        override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
            myImageView.contentDescription = data.accessibilityText
            return false 
        }
    })
    .into(myImageView)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.