From 3e56c6f90ec7a8f3bf9eab349e4e338c7b9ff17e Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Sun, 2 Sep 2018 01:54:48 +0200 Subject: [PATCH] Prepare suffix code for some inputs --- .../TextInputEditTextSuffix.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 app/src/main/java/com/herbron/moodl/CustomLayouts/TextInputEditTextSuffix.java diff --git a/app/src/main/java/com/herbron/moodl/CustomLayouts/TextInputEditTextSuffix.java b/app/src/main/java/com/herbron/moodl/CustomLayouts/TextInputEditTextSuffix.java new file mode 100644 index 0000000..8ea9d73 --- /dev/null +++ b/app/src/main/java/com/herbron/moodl/CustomLayouts/TextInputEditTextSuffix.java @@ -0,0 +1,51 @@ +package com.herbron.moodl.CustomLayouts; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.support.design.widget.TextInputEditText; +import android.text.TextPaint; +import android.util.AttributeSet; + +public class TextInputEditTextSuffix extends TextInputEditText { + + private TextPaint textPaint = new TextPaint(); + private String suffix = ""; + + public TextInputEditTextSuffix(Context context) { + super(context); + } + + public TextInputEditTextSuffix(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public TextInputEditTextSuffix(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + if(!getText().toString().equals("")) + { + int suffixXPosition = (int) textPaint.measureText(getText().toString() + getPaddingLeft()); + canvas.drawText(suffix, Math.max(suffixXPosition, 0), getBaseline(), textPaint); + } + } + + @Override + protected void onFinishInflate() { + super.onFinishInflate(); + + textPaint.setColor(getCurrentTextColor()); + textPaint.setTextSize(getTextSize()); + textPaint.setTextAlign(Paint.Align.LEFT); + } + + public void setSuffix(String suffix) + { + this.suffix = suffix; + } +}