Home » Android » android – setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) not working

android – setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) not working

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a few EditText views where I want to set the image on the left and setCompoundDrawablesWithIntrinsicBounds does not seems to be working. The graphics do not seem to be getting changed.

does anyone know why this might be the case?

Here is how I am setting the drawables:

        mFirstname.setCompoundDrawablesWithIntrinsicBounds(R.drawable.user_icon, 0, 0, 0);
        mLastname.setCompoundDrawablesWithIntrinsicBounds(R.drawable.user_icon, 0, 0, 0);
        mEmail.setCompoundDrawablesWithIntrinsicBounds(R.drawable.mailicon, 0, 0, 0);
        mPassword.setCompoundDrawablesWithIntrinsicBounds(R.drawable.lockicon, 0, 0, 0);
        mDateOfBirth.setCompoundDrawablesWithIntrinsicBounds(R.drawable.calico, 0, 0, 0);
        mCity.setCompoundDrawablesWithIntrinsicBounds(R.drawable.mailicon, 0, 0, 0);
        mStreet.setCompoundDrawablesWithIntrinsicBounds(R.drawable.mailicon, 0, 0, 0);
        mPostcode.setCompoundDrawablesWithIntrinsicBounds(R.drawable.mailicon, 0, 0, 0);
        mPhoneNumber.setCompoundDrawablesWithIntrinsicBounds(R.drawable.mailicon, 0, 0, 0);
How to&Answers:

If anyone else has this seemingly unexplainable issue then try the following:

  • Go to your XML template.
  • Remove the XML image representing it.
  • Your view should refresh the compounddrawable.

basically this portion of the view does not seem to get reinflated on ICS devices. Hopefully this solves the problem for some people!

Answer:

Another solution is to setup compound drawable on the next (after layout) cycle of the UI loop:

final TextView viewById = (TextView) findViewById(R.id.label);

viewById.post(new Runnable()
{
    @Override
    public void run()
    {
        viewById.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_menu_call, 0, 0, 0);
    }
});

Answer:

mBtnNext=(Button) rootView.findViewById(R.id.btnNext);

Drawable drawableRight=new IconicsDrawable(getActivity().getApplicationContext())
               .icon(Ionicons.Icon.ion_arrow_right_a)
                .color(getResources().getColor(R.color.colorBlack))
                .sizeDp(24);

mBtnNext.setCompoundDrawables(null,null,drawableRight,null);

Try this your problem will get resolved.

Answer:

The problem arise when you have defined Drawable (top, left, right, bottom) Image in xml layout. It’s like the xml drawable is applied after the one defined in code. Remove the xml one and do it in code where ever you want.

Answer:

I used Ionicons plugins. Go to the following links. You will get some idea about how to use IonIcons in your android project.

IonIcons For Android

After implement the above Ionicons plugins in your project. You will get IconicsDrawable class.

Gradle image

See here to know how to drive IconicsDrawable class in your class

mBtnNext=(Button) rootView.findViewById(R.id.btnNext);

Drawable drawableRight=new IconicsDrawable(getActivity().getApplicationContext())
               .icon(Ionicons.Icon.ion_arrow_right_a)
                .color(getResources().getColor(R.color.colorBlack))
                .sizeDp(24);

mBtnNext.setCompoundDrawables(null,null,drawableRight,null);

I hope you will understand now.