Skip to content

Commit

Permalink
Added chips
Browse files Browse the repository at this point in the history
  • Loading branch information
M66B committed Jul 13, 2019
1 parent 30abe9e commit 2c80c25
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
38 changes: 38 additions & 0 deletions app/src/main/java/eu/faircode/email/ChipSpan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package eu.faircode.email;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.style.ReplacementSpan;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class ChipSpan extends ReplacementSpan {
private int bg;
private int fg;
private int radius;

public ChipSpan(int bg, int fg, int radius) {
super();
this.bg = bg;
this.fg = fg;
this.radius = radius;
}

@Override
public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) {
return Math.round(paint.measureText(text, start, end) + 2 * radius);
}

@Override
public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
RectF rect = new RectF(x, top, x + paint.measureText(text, start, end) + 2 * radius, bottom);
paint.setColor(bg);
canvas.drawRoundRect(rect, radius, radius, paint);

paint.setStyle(Paint.Style.FILL);
paint.setColor(fg);
canvas.drawText(text, start, end, x + radius, y, paint);
}
}
36 changes: 36 additions & 0 deletions app/src/main/java/eu/faircode/email/FragmentCompose.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,42 @@ public void onClick(View v) {
}
});

final int chipBackground = Helper.resolveColor(getContext(), R.attr.colorPrimary);
final int chipForeground = Color.WHITE;
final int chipRadius = Helper.dp2pixels(getContext(), 6);

TextWatcher chip = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void afterTextChanged(Editable editable) {
for (Object span : editable.getSpans(0, editable.length(), ChipSpan.class))
editable.removeSpan(span);

int start = 0;
for (int i = 0; i < editable.length(); i++)
if (editable.charAt(i) == ',') {
if (i > start) {
if (editable.charAt(start) == ' ')
start++;
editable.setSpan(new ChipSpan(chipBackground, chipForeground, chipRadius),
start, i, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
start = i + 1;
}
}
};

etTo.addTextChangedListener(chip);
etCc.addTextChangedListener(chip);
etBcc.addTextChangedListener(chip);

View.OnClickListener onPick = new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down

0 comments on commit 2c80c25

Please sign in to comment.