Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set image size when using setCompoundDrawablesWithIntrinsicBounds()?

I need to set image above text in tabs in Tab Layout. So I set image in my TextView using setCompoundDrawablesWithIntrinsicBounds but I don't know how give size for my image.

I tried to give size like this:

Drawable dr = ContextCompat.getDrawable(MainActivity.this, R.drawable.mobile_icon);
    Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
    mobile_drawable = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 50, 50, true));

    TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
    tabOne.setText("Mobile");
    tabOne.setCompoundDrawablesWithIntrinsicBounds(0,mobile_drawable,0,0);
    tabLayout.getTabAt(0).setCustomView(tabOne);

But it gives me this error:

Cannot resolve method setCompoundDrawablesWithIntrinsicBounds(int,android.graphics.drawable.Drawable,int,int);

I also tried

tabOne.setCompoundDrawables(0,mobile_drawable,0,0);

but it also not working?

So how to give image size when using setCompoundDrawablesWithIntrinsicBounds???

like image 494
Devendra Dagur Avatar asked Nov 05 '15 07:11

Devendra Dagur


1 Answers

The accepted answer is outdated and buggy...

You cannot set drawable size using setCompoundDrawablesWithIntrinsicBounds

Instead, you need to set it using setCompoundDrawables like this

Drawable img = ContextCompat.getDrawable(yourContext, R.drawable.yourImgId);
img.setBounds(0, 0, yourSize, yourSize);
yourTextView.setCompoundDrawables(img, null, null, null);
like image 132
Fangming Avatar answered Sep 22 '22 11:09

Fangming