0

I want to set an image which is from server as background of my editText.

It is easy to set background image when the image is from @drawable edit_text.setBackgroundResource(R.drawable.ic_launcher_background);

I'm using that to set the image from server:

Glide.with(img_1)
            .load(API_BASE_URL+"img/post_wall_background_1.jpg")
            .apply(RequestOptions.bitmapTransform(new RoundedCorners(15)))
            .placeholder(R.drawable.shadow)
            .into(img_1);

When user click on that image, I want to set it as the brackground of my editText.

How could I do that ?

I googled for hours but can not find anything about that.

AS suggested by @vikas kumar I tried that:

Glide.with(img_1)
            .load(API_BASE_URL+"img/post_wall_background_1.jpg")
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    new_post_edit_text.setBackground(resource);
                    return false;
                }
            })
            .into(img_1);

but not work.

Thanks alot.

ankalagba
  • 94
  • 1
  • 8
  • Use [BitmapDrawable](https://developer.android.com/reference/android/graphics/drawable/BitmapDrawable) with [View#setBackgroundDrawable(Drawable)](https://developer.android.com/reference/android/view/View#setBackgroundDrawable(android.graphics.drawable.Drawable)). – ashu Jul 09 '20 at 12:08

2 Answers2

2

use Glide to download the drawable/bitmap and use that drawble/bitmap to set the background

Glide.with(img_1)
    .load(API_BASE_URL+"img/post_wall_background_1.jpg")
    .listener(new RequestListener<Drawable>() {
        @Override
        public boolean onLoadFailed(Exception e, Object model, Target<Drawable> target, boolean isFirstResource) {
            progressBar.setVisibility(View.GONE);
            return false; // important to return false so the error placeholder can be placed
        }

        @Override
        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            edit_text.setBackgroundDrawable(resource);
            return false;
        }
    })
    .into(img_1);
vikas kumar
  • 10,447
  • 2
  • 46
  • 52
0

I have found a better solution here:

    Glide.with(getApplicationContext())
                            .load("tools/img/fsfdfg.jpg")
                            .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                            .into(new CustomTarget<Drawable>() {
                                @Override
                                public void onResourceReady(@NonNull Drawable resource, 
@Nullable Transition<? super Drawable> transition) {
                                    new_post_text_view.setBackground(resource);
                                }
                                @Override
                                public void onLoadCleared(@Nullable Drawable placeholder) {}
                            });
ankalagba
  • 94
  • 1
  • 8
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – jasie Oct 05 '20 at 09:15