title: LineBackgroundSpan drawBackground() called repeatedly
body: <p><strong>Edit:</strong> I have been able to track the problem down to the use of <code>EditText</code> rather than <code>TextView</code>. The repeated calls only happen when a field is an <code>EditText</code> and the system behaves itself when the field is a <code>TextView</code>. I can find nothing in the documentation or online that indicates that <code>LineBackgroundSpan</code> will not work with <code>EditText</code>.</p>
  
  <p>I have updated the MCVE to show how things work with <code>TextView</code> (it does) and with <code>EditText</code> (it doesn't - at least not well). My updated question is how to get <code>LineBackgroundSpan</code> working with <code>EditText</code>.</p>
  
  <hr>
  
  <p>I have implemented a simple class to add a rounded background to text in an <code>EditText</code> using <a href="https://developer.android.com/reference/android/text/style/LineBackgroundSpan.html" rel="nofollow noreferrer"><code>LineBackgroundSpan</code></a>. Everything works OK but while debugging I noticed that the <code>drawBackground</code> method of my class is called repeatedly and, seemingly, without end for each span in the string even though no changes are being made. It is not apparent on the display, but is readily apparent if a breakpoint is set in the <code>drawBackground</code> method.</p>
  
  <p>In trying to track down the issue, I was able to reduce the code down to an MCVE.The following code will simply highlight an entire line of text. The top line is an <code>EditText</code> and the bottom line is a <code>TextView</code>. (This is not what I am really trying to do, but it serves the purpose.) </p>
  
  <p>This MCVE exhibits the problem for me on emulators running API 17 and API 24 as well as an actual phone running API 24. Setting the <code>disableDraw</code> argument to <code>true</code> for the constructor of <code>RoundedBackgroudSpan()</code> will disable background drawing action in <code>drawBackground()</code>. I am seeing the problem on the <code>EditText</code> even with background drawing disabled.</p>
  
  <p>What is going on here? Am I misunderstanding how to work with spans? Will spans not work with <code>EditText</code>? Any help will be greatly appreciated.</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>    package com.example.bgspanmcve;
  
  import android.graphics.Canvas;
  import android.graphics.Paint;
  import android.graphics.RectF;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.text.SpannableString;
  import android.text.style.LineBackgroundSpan;
  import android.util.Log;
  import android.widget.EditText;
  import android.widget.TextView;
  
  import static android.text.Spanned.SPAN_INCLUSIVE_INCLUSIVE;
  
  public class MainActivity extends AppCompatActivity {
      final String dispString = "XAB CD EF";
      private static int count = 0; // times drawBackground is called
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          EditText editText;
          TextView textView;
          RoundedBackgroundSpan bg;
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Set up the EditText field with a span.
          // RoundedBackgroundSpan#drawBackground will be called forever for this EditText.
          editText = ((EditText) findViewById(R.id.editText));
          SpannableString ssEditText = new SpannableString(dispString);
          bg = new RoundedBackgroundSpan(INHIBIT_DRAWING, false);
          ssEditText.setSpan(bg, 0, ssEditText.length(), SPAN_INCLUSIVE_INCLUSIVE);
          editText.setText(ssEditText);
  
          // Set up the TextView field with a span.
          // RoundedBackgroundSpan#drawBackground will be called once for this TextView.
          textView = ((TextView) findViewById(R.id.textView));
          SpannableString ssTextView = new SpannableString(dispString);
          bg = new RoundedBackgroundSpan(INHIBIT_DRAWING, true);
          ssTextView.setSpan(bg, 0, ssTextView.length(), SPAN_INCLUSIVE_INCLUSIVE);
          textView.setText(ssTextView, TextView.BufferType.EDITABLE);
      }
  
      private static class RoundedBackgroundSpan implements LineBackgroundSpan {
          private boolean mDisableDraw;
          private boolean mIsTextView;
  
          RoundedBackgroundSpan(boolean disableDraw, boolean isTextView) {
              super();
              mDisableDraw = disableDraw;
              mIsTextView = isTextView;
          }
  
          @Override
          public void drawBackground(
                  Canvas canvas, Paint paint, int left, int right, int top,
                  int baseline, int bottom, CharSequence text, int start, int end, int lnum) {
  
              count++;
              if (mIsTextView) {
                  Log.d(TAG, "&lt;&lt;&lt;&lt;drawBackground (TextView) #" + count);
              } else {
                  Log.d(TAG, "&lt;&lt;&lt;&lt;drawBackground (EditText) #" + count);
              }
              if (mDisableDraw) return;
  
              Paint localPaint = new Paint();
              RectF rect = new RectF(left, top, right, bottom);
              localPaint.setColor(BG_COLOR);
              canvas.drawRoundRect(rect, RADIUS_X, RADIUS_Y, localPaint);
          }
  
          private final String TAG = RoundedBackgroundSpan.class.getSimpleName();
          private final int BG_COLOR = 0xfF00FF00;
          private final int RADIUS_X = 20;
          private final int RADIUS_Y = 20;
      }
  
      private final static String TAG = MainActivity.class.getSimpleName();
      private final boolean INHIBIT_DRAWING = true;
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.example.bgspanmcve.MainActivity"&gt;
  
      &lt;EditText
          android:id="@+id/editText"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerInParent="true"
          android:layout_marginStart="0dp"
          android:inputType="text"
          android:paddingEnd="0dp"
          android:paddingStart="0dp"
          android:text="EditText"
          android:textSize="20sp" /&gt;
  
      &lt;TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignStart="@id/editText"
          android:layout_below="@id/editText"
          android:layout_marginStart="0dp"
          android:layout_marginTop="16dp"
          android:paddingEnd="0dp"
          android:paddingStart="0dp"
          android:text="TextView"
          android:textSize="20sp"
          android:textStyle="bold" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>