Skip to content

Commit fc089bc

Browse files
committed
First prototype with prefix and suffix support.
0 parents  commit fc089bc

File tree

12 files changed

+513
-0
lines changed

12 files changed

+513
-0
lines changed

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/libraries
5+
/.idea/modules.xml
6+
/.idea/workspace.xml
7+
.DS_Store
8+
/build
9+
/captures
10+
.externalNativeBuild

.idea/codeStyles/Project.xml

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.tobiasschuerg.prefixpostfix
2+
3+
import android.os.Bundle
4+
import android.support.v7.app.AppCompatActivity
5+
import kotlinx.android.synthetic.main.activity_main.*
6+
7+
class MainActivity : AppCompatActivity() {
8+
9+
override fun onCreate(savedInstanceState: Bundle?) {
10+
super.onCreate(savedInstanceState)
11+
setContentView(R.layout.activity_main)
12+
13+
edit_text_2.setPrefix("$")
14+
15+
edit_text_2.setText("$")
16+
17+
edit_text_2.setSuffix("$")
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.tobiasschuerg.prefixpostfix;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Color;
6+
import android.graphics.Paint;
7+
import android.graphics.Rect;
8+
import android.graphics.Typeface;
9+
import android.support.v7.widget.AppCompatEditText;
10+
import android.text.TextPaint;
11+
import android.util.AttributeSet;
12+
13+
/**
14+
* Inspired by https://gist.github.com/morristech/5480419
15+
*/
16+
public class PrefixSuffixEditText extends AppCompatEditText {
17+
18+
private final Paint debugPaint;
19+
20+
// Stuff to do with our rendering
21+
TextPaint mTextPaint = new TextPaint();
22+
float mFontHeight;
23+
TagDrawable left;
24+
25+
// The actual suffix
26+
String mSuffix = "";
27+
28+
// These are used to store details obtained from the EditText's rendering process
29+
Rect line0bounds = new Rect();
30+
int mLine0Baseline;
31+
32+
public PrefixSuffixEditText(Context context, AttributeSet attrs) {
33+
super(context, attrs);
34+
35+
left = new TagDrawable(this);
36+
37+
mFontHeight = getTextSize();
38+
39+
mTextPaint.setColor(getCurrentHintTextColor());
40+
mTextPaint.setTextSize(mFontHeight);
41+
mTextPaint.setTextAlign(Paint.Align.LEFT);
42+
mTextPaint.setAntiAlias(true);
43+
44+
debugPaint = new Paint();
45+
debugPaint.setStyle(Paint.Style.STROKE);
46+
debugPaint.setColor(Color.BLUE);
47+
48+
49+
// Setup the left side
50+
setCompoundDrawablesRelative(left, null, null, null);
51+
}
52+
53+
@Override
54+
public void setTypeface(Typeface typeface) {
55+
super.setTypeface(typeface);
56+
if (mTextPaint != null) {
57+
// Sometimes TextView itself calls me when i'm naked
58+
mTextPaint.setTypeface(typeface);
59+
}
60+
61+
postInvalidate();
62+
}
63+
64+
public void setPrefix(String s) {
65+
left.setText(s);
66+
setCompoundDrawablesRelative(left, null, null, null);
67+
}
68+
69+
public void setSuffix(String s) {
70+
mSuffix = s;
71+
setCompoundDrawablesRelative(left, null, null, null);
72+
}
73+
74+
@Override
75+
public void onDraw(Canvas c) {
76+
mLine0Baseline = getLineBounds(0, line0bounds);
77+
78+
super.onDraw(c);
79+
80+
// Now we can calculate what we need!
81+
String text = getText().toString();
82+
float textWidth = mTextPaint.measureText(left.text + text) + getPaddingLeft();
83+
84+
85+
c.drawRect(line0bounds, debugPaint);
86+
87+
// We need to draw this like this because
88+
// setting a right drawable doesn't work properly and we want this
89+
// just after the text we are editing (but untouchable)
90+
float y2 = line0bounds.bottom - mTextPaint.descent();
91+
c.drawText(mSuffix, textWidth, y2, mTextPaint);
92+
}
93+
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.tobiasschuerg.prefixpostfix;
2+
3+
import android.graphics.Canvas;
4+
import android.graphics.ColorFilter;
5+
import android.graphics.PixelFormat;
6+
import android.graphics.drawable.Drawable;
7+
import android.support.annotation.NonNull;
8+
9+
// This is for the prefix.
10+
// It is a drawable for rendering text
11+
public class TagDrawable extends Drawable {
12+
13+
private PrefixSuffixEditText extendedEditText;
14+
public String text = "";
15+
16+
public TagDrawable(PrefixSuffixEditText extendedEditText) {
17+
this.extendedEditText = extendedEditText;
18+
}
19+
20+
public void setText(String s) {
21+
text = s;
22+
23+
// Tell it we need to be as big as we want to be!
24+
setBounds(0, 0, getIntrinsicWidth(), getIntrinsicHeight());
25+
26+
invalidateSelf();
27+
}
28+
29+
@Override
30+
public void draw(@NonNull Canvas canvas) {
31+
// I don't know why this y works here, but it does :)
32+
// (aka if you are from Google/are Jake Wharton and I have done it wrong, please tell me!)
33+
canvas.drawText(text, 0, extendedEditText.mLine0Baseline + canvas.getClipBounds().top, extendedEditText.mTextPaint);
34+
}
35+
36+
@Override
37+
public void setAlpha(int i) {
38+
}
39+
40+
@Override
41+
public void setColorFilter(ColorFilter colorFilter) {
42+
}
43+
44+
@Override
45+
public int getOpacity() {
46+
return PixelFormat.OPAQUE;
47+
}
48+
49+
@Override
50+
public int getIntrinsicHeight() {
51+
return (int) extendedEditText.mFontHeight;
52+
}
53+
54+
@Override
55+
public int getIntrinsicWidth() {
56+
return (int) extendedEditText.mTextPaint.measureText(text);
57+
}
58+
}

build.gradle

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
3+
buildscript {
4+
ext.kotlin_version = '1.2.61'
5+
repositories {
6+
google()
7+
jcenter()
8+
}
9+
dependencies {
10+
classpath 'com.android.tools.build:gradle:3.1.4'
11+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
12+
13+
// NOTE: Do not place your application dependencies here; they belong
14+
// in the individual module build.gradle files
15+
}
16+
}
17+
18+
allprojects {
19+
repositories {
20+
google()
21+
jcenter()
22+
}
23+
}
24+
25+
task clean(type: Delete) {
26+
delete rootProject.buildDir
27+
}

gradle.properties

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Project-wide Gradle settings.
2+
# IDE (e.g. Android Studio) users:
3+
# Gradle settings configured through the IDE *will override*
4+
# any settings specified in this file.
5+
# For more details on how to configure your build environment visit
6+
# http://www.gradle.org/docs/current/userguide/build_environment.html
7+
# Specifies the JVM arguments used for the daemon process.
8+
# The setting is particularly useful for tweaking memory settings.
9+
org.gradle.jvmargs=-Xmx1536m
10+
# When configured, Gradle will run in incubating parallel mode.
11+
# This option should only be used with decoupled projects. More details, visit
12+
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13+
# org.gradle.parallel=true

gradle/wrapper/gradle-wrapper.jar

53.4 KB
Binary file not shown.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Mon Sep 10 15:22:37 CEST 2018
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

0 commit comments

Comments
 (0)