Dzhuneyt
Dzhuneyt

Reputation: 8701

Constrain drawableLeft and drawableRight's height to TextView's height

I have the following TextView:

<TextView
    android:drawableLeft="@drawable/loading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Updating Rates" />

And the result is this:

How do I make the loading circle drawable constrain proportionally so its height matches the height of the text?

Also, if possible, I would like to add some spacing between the text and the image.

Upvotes: 26

Views: 36548

Answers (5)

Dmide
Dmide

Reputation: 6462

Ravi's solution worked for me, but on some devices drawable was disappearing. Fixed this by removing the scaling.

    int bound = (int) (drawable.getIntrinsicWidth() * 0.5);
    drawable.setBounds(0, 0, bound, bound);
    b.setCompoundDrawables(drawable, null, null, null);

Upvotes: 2

Ravi
Ravi

Reputation: 960

I think I found the solution, but a bit late

Drawable drawable = getResources().getDrawable(R.drawable.s_vit);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.5),(int)(drawable.getIntrinsicHeight()*0.5));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, scaleWidth, scaleHeight);
Button btn = findViewbyId(R.id.yourbtnID);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null); 

Upvotes: 8

Abdul Vajid
Abdul Vajid

Reputation: 1411

A Better solution will to create a XML in drawable folder and add the image to this XML, and maintain scaleType as "fitXY". Now you can make this XML as the drawableLeft.

Upvotes: 0

JafarKhQ
JafarKhQ

Reputation: 8734

To make the loading smaller just make the images smaller.

Or use RelativeLayout and put the loading in an ImageView and scale it.

Or you can add padding between the drawables and the text using android:drawablePadding (or setCompoundDrawablePadding()):

android:drawablePadding="3dp"

Upvotes: 22

jay
jay

Reputation: 292

You just need to declare two separate views imageview and textview, and set them accordingly

Upvotes: -1

Related Questions