EditText not increasing width when adding BulletSpan

807 views Asked by At

When adding a BulletSpan to an EditText, the size of the EditText is not increased with the leading space added by the bullet. This leads to wrapping or scrolling of the text out of sight.

Sofar I have not been able to force the EditText to remeasure its width correctly.

Unfortunately I have not enough reputation to illustrate the situation. Let me try to describe the situation.

  1. When adding the BulletSpan to the underlying Editable of an EditText, the resulting larger paragraph scrolls behind the right edge.

    |0123456789|  -> | • 0123456|789  
    

    The last characters are scrolled behind the right edge.

  2. When the altered editable is assigned to an EditText, the width of the field remains the same and the wider text wraps.

    |0123456789| -> | • 0123456|
                    |   789    |
    

    In both cases the EditText does not compensate its width for the leading space.

  3. Adding extra characters at end of paragraph

    | • 0123456789xxx|
    |   xxx          |
    

    When adding characters to the EditText, the size of the field increases as expected, but does not take the added space of the bullet and its margin into account and keeps wrapping.

Several questions on BulletSpans have been asked, but not before on this weird behaviour. For two days, I've tried many layout setting in xml and many options in code to no avail unfortunately.

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 (partly)

<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" />
1

There are 1 answers

0
Mo12 On

Try to override BulletSpan methods 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);
    }
}