0

I'm experiencing issues trying to use a custom type that implements Editable as the basis for an EditText. Here is my code:

// This is a custom type that implements Editable.
// It's just a wrapper over a SpannableStringBuilder, with some
// irrelevant added functionality.
public class ColoredText implements Editable {
    private final SpannableStringBuilder builder;

    public ColoredText(String text) {
        this.builder = new SpannableStringBuilder(text);
    }

    // All Editable methods are implemented through this.builder.
    @Override
    public Editable replace(int i, int i1, CharSequence charSequence, int i2, int i3) {
        this.builder.replace(i, i1, charSequence, i2, i3);
        return this;
    }

    ...
}

public class NoCopyEditableFactory extends Editable.Factory {
    @Override
    public Editable newEditable(CharSequence source) {
        return (Editable) source;
    }
}

// In MainActivity.java
@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    setContentView(R.layout.main);

    String line = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
    String text = TextUtils.join("\n", Collections.nCopies(100, line));
    Editable e = new ColoredText(text);

    EditText et = (EditText) findViewById(R.id.editText);
    et.setEditableFactory(new NoCopyEditableFactory()); // This line is causing trouble.
    et.setText(e, TextView.BufferType.EDITABLE);
}

// In the MainActivity layout file
<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

For some reason, when I comment out the NoCopyEditableFactory, the cursor updates just fine. However, when I uncomment the line forcing the EditText not to copy the ColoredText into a new SpannableStringBuilder, the cursor does not update when I click on new places in the text. Why is this?

James Ko
  • 32,215
  • 30
  • 128
  • 239
  • how are `Spannable` / `Spanned` methods implemented? – pskink Jul 08 '17 at 05:23
  • and whats the point in returning the `CharSequence source` from `Editable.Factory#newEditable` at all? see line 4396 here: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/TextView.java#L4396 – pskink Jul 08 '17 at 15:33
  • and finally whats the point of asking the questions if you dont care about the comments posted there? do you read them at all? i could answer for example how to *"RecyclerView: Disable scrolling caused by change in focus"* but why, since you dont read it? – pskink Jul 09 '17 at 06:50
  • @pskink Sorry, I have been reading your comments but didn't have the time to respond to them. For this question, I've solved the problem myself. Since my situation was highly specific, I didn't see much value in updating here to say I'd worked around the problem. My next question, however, is much more general/likely to be of value to other devs, so I will definitely answer your questions there. I'll try to be more responsive when I see you in future posts. – James Ko Jul 09 '17 at 07:48
  • you completely ignored my three comments on `How to implement lazy rendering/pagination for an Android TextView` thread too but now it seems you are trying to use `RecycleView` with your text divided into small pieces - my advice is: just say at least: `"thanx, i will try it"`, otherwise people lose interest very quickly – pskink Jul 09 '17 at 10:16
  • @pskink Thanks for the advice, I will try to keep it in mind. – James Ko Jul 09 '17 at 14:22

1 Answers1

-1

The problem turned out to be with my Editable implementation. It works fine after I deleted some misbehaving code.

James Ko
  • 32,215
  • 30
  • 128
  • 239