EditText does not increase width when BulletSpan is added
When adding a BulletSpan to the EditText, the size of the EditText does not increase with the mileage added by the bullet. This causes the text to wrap around or scroll out of view.
Sofar I was unable to get the EditText to change its width correctly.
Unfortunately, I don't have enough reputation to illustrate the situation. Let me try to describe the situation.
-
When BulletSpan is added to the base Editable for the EditText, the resulting larger paragraph scrolls to the right.
|0123456789| -> | • 0123456|789
The last characters are scrolled past the right edge.
-
When the modified editable is assigned to the EditText editor, the field width remains unchanged and the wider text is wrapped.
|0123456789| -> | • 0123456| | 789 |
In both cases, the EditText does not compensate for its width for the leading space.
-
Adding extra characters at the end of a paragraph
| • 0123456789xxx| | xxx |
When characters are added to the EditText, the margin grows as expected, but does not account for the added marker space and its margin, and retains the wrapper.
Several questions have been asked about BulletSpans, but not before this strange behavior. Over the course of two days I have tried many layout settings in xml and many options in the code to no avail.
Does anyone have a suggestion?
Code: MainActivity.java
mToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean showBullet) {
final Editable editable = mEditText1.getText();
if (showBullet) {
// Add bullet
editable.setSpan(new BulletSpan(50), 0, editable.length(), Spannable.SPAN_PARAGRAPH);
} else {
// Remove bullet
BulletSpan[] spans = editable.getSpans(0, editable.length(), BulletSpan.class);
if (spans.length > 0) {
editable.removeSpan(spans[0]);
}
}
// See what happens when assigning the editable to an other TextView
mEditText2.setText(editable);
}
});
Layout: activity_main.xml (partial)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<!-- ToggleButton and TextView removed -->
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:inputType="textMultiLine"
android:text="0123456789" />
source to share
Try to override the methods BulletSpan
and change its implementation:
public class MyBulletSpan extends BulletSpan {
@Override
public int getLeadingMargin(boolean first) {
return super.getLeadingMargin(first);
}
@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom, CharSequence text, int start, int end, boolean first, Layout l) {
super.drawLeadingMargin(c, p, x, dir, top, baseline, bottom, text, start, end, first, l);
}
}
source to share