aurelianr
aurelianr

Reputation: 549

android change color of an icon from EditText

I have the following EditText :

editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_bottom_right_black_18dp, 0);

and I am trying to change the color of ic_arrow_bottom_right_black_18dp programatically using the below methods:

     protected void setEditTextDisabled(EditText editText) {
        editText.setFocusable(false);
        editText.setClickable(false);
        editText.setEnabled(false);
        editText.setTextColor(ContextCompat.getColor(getContext(), R.color.package_components_group_text_color));
        if (editText.getTag(R.id.values_list_selected_ids) == null) {
            if (editText.getTag(R.id.values_list_selected_ids) == null) {
                editText.setFocusableInTouchMode(true);
                Drawable[] d = editText.getCompoundDrawables();
                if (d.length == 4) {
                    d[2].setColorFilter(ContextCompat.getColor(getContext(), R.color.package_components_group_text_color), PorterDuff.Mode.SRC_ATOP);
                }
            }
        }
        Drawable background = editText.getBackground();

        if (background instanceof ShapeDrawable) {
//          ((ShapeDrawable)background).getPaint().setStroke(2, getResources().getColor(R.color.package_components_group_text_color));
//            ((ShapeDrawable)background).getPaint().setStroke(2, getResources().getColor(R.color.package_components_group_text_color));

        } else if (background instanceof GradientDrawable) {
            ((GradientDrawable)background).setStroke(2, getResources().getColor(R.color.package_components_group_text_color));
        } else if (background instanceof ColorDrawable) {
//            ((ColorDrawable)background).setStroke(2, getResources().getColor(R.color.package_components_group_text_color));
        }
    }

    protected void setEditTextEnabled(EditText editText) {
        editText.setEnabled(true);
        editText.setFocusable(true);
        editText.setClickable(true);
        editText.setTextColor(ContextCompat.getColor(getContext(), R.color.black));
        if (editText.getTag(R.id.values_list_selected_ids) == null) {
            editText.setFocusableInTouchMode(true);
            Drawable[] d = editText.getCompoundDrawables();
            if (d.length == 4) {
                  d[2].setColorFilter(ContextCompat.getColor(getContext(), R.color.black), PorterDuff.Mode.SRC_ATOP);
            }
        }

        Drawable background = editText.getBackground();
        if (background instanceof ShapeDrawable) {
         //   ((ShapeDrawable)background).getPaint().setColor(getResources().getColor(R.color.black));
        } else if (background instanceof GradientDrawable) {
            ((GradientDrawable)background).setStroke(2, getResources().getColor(R.color.black));
        } else if (background instanceof ColorDrawable) {
          //  ((ColorDrawable)background).setColor(getResources().getColor(R.color.black));
        }
    }

The issue is that the drawable right icon of the EditText become invisible or white when one of the above methods is called.

I attached a picture a picture with the icon of the edit text which is located on right side.

Upvotes: 1

Views: 1563

Answers (2)

aurelianr
aurelianr

Reputation: 549

This is the right answer

Drawable ddd = getResources().getDrawable(R.drawable.ic_arrow_bottom_right_black_18dp);
Drawable drawable = DrawableCompat.wrap(ddd);

DrawableCompat.setTint(drawable, ContextCompat.getColor(getContext(), R.color.package_components_group_text_color));

editText.setCompoundDrawables(null, null, drawable, null);

Upvotes: 2

waqas
waqas

Reputation: 342

You can change the color of icon not background using Picasso library. If you can get the icon as an ImageView you can do that. Picasso is very simple and most powerful library.

Picasso.with(this)
            .load(R.drawable.your_icon)
            .transform(new ColorTransformation(getResources().getColor(R.color.your_color)))
            .into(imageViewIcon);

Upvotes: 0

Related Questions