aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/PrefixedEditText.java
blob: 3cbb114e8dae36ab458c25f30986ca4f1f6ca20b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package org.sufficientlysecure.keychain.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.widget.EditText;

import org.sufficientlysecure.keychain.R;

public class PrefixedEditText extends EditText {

    private String mPrefix;
    private Rect mPrefixRect = new Rect();

	public PrefixedEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
        TypedArray style = context.getTheme().obtainStyledAttributes(
                attrs, R.styleable.PrefixedEditText, 0, 0);
        mPrefix = style.getString(R.styleable.PrefixedEditText_prefix);
        if (mPrefix == null) {
            mPrefix = "";
        }
	}

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        getPaint().getTextBounds(mPrefix, 0, mPrefix.length(), mPrefixRect);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText(mPrefix, super.getCompoundPaddingLeft(), getBaseline(), getPaint());
    }

    @Override
    public int getCompoundPaddingLeft() {
        return super.getCompoundPaddingLeft() + mPrefixRect.width();
    }

}