Set text drawable with the same size as text in edittext

Viewed 80

I faced the following problem, I need to implement this design:enter image description here

There's an edittext here (2,000 is the text in it), and an unchangeable part (dollar sign). I decided to make the dollar sign as drawable and then set it as compound drawable. For this purpose, I added the following class:

class TextDrawable(
    private val context: Context,
    private val text: String
) : Drawable() {

    private val paint: Paint = Paint()

    override fun draw(canvas: Canvas) {
        canvas.drawText(text, 0, text.length, bounds.centerX().toFloat(), bounds.centerY().toFloat(), paint)
    }

    override fun setAlpha(alpha: Int) {
        paint.alpha = alpha
    }

    override fun setColorFilter(cf: ColorFilter?) {
        paint.colorFilter = cf
    }

    override fun getOpacity() = PixelFormat.TRANSLUCENT

    init {
        paint.apply {
            color = ContextCompat.getColor(context, R.color.colorCalculatorSum)
            textSize = 26f
            isAntiAlias = true
            textAlign = Paint.Align.LEFT
        }
    }
}

And then I add the dollar sign in this way:

    binding.etSum.setCompoundDrawablesWithIntrinsicBounds(
        TextDrawable(context, "$"),
        null,
        null,
        null
    )

But when I run the code it looks like this:

enter image description here

So, what's the matter and how can I fix the issue?

0 Answers
Related