Chip measure

This commit is contained in:
M66B 2022-02-21 20:16:43 +01:00
parent 09922a3ec2
commit 6099e26624
2 changed files with 90 additions and 75 deletions

View File

@ -79,22 +79,13 @@ public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextVie
ContextThemeWrapper ctx = new ContextThemeWrapper(context, ContextThemeWrapper ctx = new ContextThemeWrapper(context,
dark ? R.style.Base_Theme_Material3_Dark : R.style.Base_Theme_Material3_Light); dark ? R.style.Base_Theme_Material3_Dark : R.style.Base_Theme_Material3_Light);
ContentResolver resolver = context.getContentResolver(); ContentResolver resolver = context.getContentResolver();
int dp3 = Helper.dp2pixels(context, 3);
DisplayMetrics dm = getResources().getDisplayMetrics();
addTextChangedListener(new TextWatcher() { Runnable update = new Runnable() {
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void run() {
// Do nothing try {
} Editable edit = getText();
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Do nothing
}
@Override
public void afterTextChanged(Editable edit) {
boolean added = false; boolean added = false;
List<ClipImageSpan> spans = new ArrayList<>(); List<ClipImageSpan> spans = new ArrayList<>();
spans.addAll(Arrays.asList(edit.getSpans(0, edit.length(), ClipImageSpan.class))); spans.addAll(Arrays.asList(edit.getSpans(0, edit.length(), ClipImageSpan.class)));
@ -117,7 +108,7 @@ public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextVie
} }
} }
if (!found) { if (!found && start < i + 1) {
String email = edit.subSequence(start, i + 1).toString(); String email = edit.subSequence(start, i + 1).toString();
InternetAddress[] parsed; InternetAddress[] parsed;
try { try {
@ -147,7 +138,7 @@ public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextVie
cd.setChipIcon(avatar); cd.setChipIcon(avatar);
// cd.setLayoutDirection(View.LAYOUT_DIRECTION_LOCALE); // cd.setLayoutDirection(View.LAYOUT_DIRECTION_LOCALE);
cd.setText(text); cd.setText(text);
cd.setMaxWidth(2 * dm.widthPixels / 3); cd.setMaxWidth(getWidth());
cd.setBounds(0, 0, cd.getIntrinsicWidth(), cd.getIntrinsicHeight()); cd.setBounds(0, 0, cd.getIntrinsicWidth(), cd.getIntrinsicHeight());
ClipImageSpan is = new ClipImageSpan(cd); ClipImageSpan is = new ClipImageSpan(cd);
@ -168,6 +159,29 @@ public class EditTextMultiAutoComplete extends AppCompatMultiAutoCompleteTextVie
if (spans.size() > 0 || added) if (spans.size() > 0 || added)
invalidate(); invalidate();
} catch (Throwable ex) {
Log.e(ex);
}
}
};
addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Do nothing
}
@Override
public void afterTextChanged(Editable edit) {
if (getWidth() == 0)
post(update);
else
update.run();
} }
}); });
} }

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<chip xmlns:android="http://schemas.android.com/apk/res/android" <chip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:ellipsize="middle"
android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:closeIconVisible="false" app:closeIconVisible="false"
app:textEndPadding="3dp" app:textEndPadding="3dp"