How to use retrieve image as drawable of radio button

Viewed 83

I am using RadioButton

 <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:id="@+id/optionC"
        android:textColor="@color/black"
        android:text="@string/opetion1"
        android:drawableRight="@drawable/ic_google_logo"
        android:textSize="18sp"
        android:elevation="3dp"
        android:translationZ="3dp"
        android:stateListAnimator="@null" />

Now I want to set the drawableRihgt as a retrieved image. which I get from the real-time database with help of a URL. My question is that how I can set the retrieved image in drawableRight or drawable of RadioButton

2 Answers

You can use Picasso with an imageView for that. After you load the image from url you can show it on the right of the RadioButton. If you want to code example for that i can help you.

For example:

In app/build.gradle,

implementation 'com.squareup.picasso:picasso:2.5.2'

In res/layout/activity_main.xml,

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">

   <RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:id="@+id/optionC"
    android:textColor="@color/black"
    android:text="@string/opetion1"
    android:drawableRight="@drawable/ic_google_logo"
    android:textSize="18sp"
    android:elevation="3dp"
    android:translationZ="3dp"
    android:stateListAnimator="@null" />

   <ImageView
      android:id="@+id/image_view"
      android:layout_width="200dp"
      android:layout_toRightOf="@id/optionC"
      android:layout_height="300dp"/>

</RelativeLayout>

In MainActivity.kt,

import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.squareup.picasso.Picasso

class MainActivity : AppCompatActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      val imageView: ImageView = findViewById(R.id.image_view)
      val url = "YOUR_URL"
      Picasso.with(this).load(url).into(imageView)
   }
}

You can get bitmap from Glide.After that set that bitmap to radio button like this::

Glide.with(getApplicationContext())
                        .asBitmap()
                        .load(URL)
                          .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
Drawable drw = new BitmapDrawable(getResources(), resource);
button.setCompoundDrawablesWithIntrinsicBounds(drw, 0, 0, 0);
                            }

                              @Override
                              public void onLoadFailed(@Nullable Drawable errorDrawable) {
                                  super.onLoadFailed(errorDrawable);
                                 
                              }
                        });

Here are the dependencies:

implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
Related