Skip to content

Commit 2c80c25

Browse files
committed
Added chips
1 parent 30abe9e commit 2c80c25

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package eu.faircode.email;
2+
3+
import android.graphics.Canvas;
4+
import android.graphics.Paint;
5+
import android.graphics.RectF;
6+
import android.text.style.ReplacementSpan;
7+
8+
import androidx.annotation.NonNull;
9+
import androidx.annotation.Nullable;
10+
11+
public class ChipSpan extends ReplacementSpan {
12+
private int bg;
13+
private int fg;
14+
private int radius;
15+
16+
public ChipSpan(int bg, int fg, int radius) {
17+
super();
18+
this.bg = bg;
19+
this.fg = fg;
20+
this.radius = radius;
21+
}
22+
23+
@Override
24+
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) {
25+
return Math.round(paint.measureText(text, start, end) + 2 * radius);
26+
}
27+
28+
@Override
29+
public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
30+
RectF rect = new RectF(x, top, x + paint.measureText(text, start, end) + 2 * radius, bottom);
31+
paint.setColor(bg);
32+
canvas.drawRoundRect(rect, radius, radius, paint);
33+
34+
paint.setStyle(Paint.Style.FILL);
35+
paint.setColor(fg);
36+
canvas.drawText(text, start, end, x + radius, y, paint);
37+
}
38+
}

app/src/main/java/eu/faircode/email/FragmentCompose.java

+36
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,42 @@ public void onClick(View v) {
293293
}
294294
});
295295

296+
final int chipBackground = Helper.resolveColor(getContext(), R.attr.colorPrimary);
297+
final int chipForeground = Color.WHITE;
298+
final int chipRadius = Helper.dp2pixels(getContext(), 6);
299+
300+
TextWatcher chip = new TextWatcher() {
301+
@Override
302+
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
303+
}
304+
305+
@Override
306+
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
307+
}
308+
309+
@Override
310+
public void afterTextChanged(Editable editable) {
311+
for (Object span : editable.getSpans(0, editable.length(), ChipSpan.class))
312+
editable.removeSpan(span);
313+
314+
int start = 0;
315+
for (int i = 0; i < editable.length(); i++)
316+
if (editable.charAt(i) == ',') {
317+
if (i > start) {
318+
if (editable.charAt(start) == ' ')
319+
start++;
320+
editable.setSpan(new ChipSpan(chipBackground, chipForeground, chipRadius),
321+
start, i, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
322+
}
323+
start = i + 1;
324+
}
325+
}
326+
};
327+
328+
etTo.addTextChangedListener(chip);
329+
etCc.addTextChangedListener(chip);
330+
etBcc.addTextChangedListener(chip);
331+
296332
View.OnClickListener onPick = new View.OnClickListener() {
297333
@Override
298334
public void onClick(View view) {

0 commit comments

Comments
 (0)