Issue
I want an EditText with rupee symbol fix at left side of the EditText.
Note: It is a floating labeled EditText. I have used android.support.design.widget.TextInputLayout
. And when I am deleting the text inside EditText, all the text should be cleared except Rupee symbol.
I want to use Rupee font string <string name="rs"> \u20B9 </string>
instead of rupee png file.
If it is recommended to use only rupee png file, then we can use drawableStart
. But how to control the size of that icon?
I need suggestions.
Solution
You can generate drawable at runtime. Below is the code to generate drawable
public Drawable getSymbol(Context context, String symbol, float textSize, int color) {
Paint paint = new Paint(ANTI_ALIAS_FLAG);
paint.setTextSize(textSize);
paint.setColor(color);
paint.setTextAlign(Paint.Align.LEFT);
float baseline = -paint.ascent(); // ascent() is negative
int width = (int) (paint.measureText(symbol) + 0.5f); // round
int height = (int) (baseline + paint.descent() + 0.5f);
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
canvas.drawText(symbol, 0, baseline, paint);
return new BitmapDrawable(context.getResources(), image);
}
Now pass the values like this.
Drawable drawable = getSymbol(context, "\u20B9"/*Your Symbol*/, editText.getTextSize(), editText.getCurrentTextColor()); // This will return a drawable
To set this drawable to your EditText, use this code
editText.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
Answered By - Suresh Kumar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.