diff options
| author | Mihai Popa <popam@google.com> | 2018-12-04 15:45:00 +0000 |
|---|---|---|
| committer | Mihai Popa <popam@google.com> | 2018-12-06 10:39:34 +0000 |
| commit | 6c7ad1d0180935679c4f70aa870ed02812a6f67c (patch) | |
| tree | 66b8c82bd2ed1df92f011acbedb86347a10e0b5b /core/java/android/widget/TextView.java | |
| parent | 188cf112d2e71eefe6657c3677c91cb546ff4cc8 (diff) | |
Make text cursor drawable public
The CL adds public getter and setters for the drawable used for the
cursor of a TextView.
Bug: 117521190
Test: atest CtsWidgetTestCases:android.widget.cts.TextViewTest
Change-Id: I3801ee8642d277c96c094b7d4155e28fc5bcea87
Diffstat (limited to 'core/java/android/widget/TextView.java')
| -rw-r--r-- | core/java/android/widget/TextView.java | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 085f8f1d678f..90cf871830fd 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -799,8 +799,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener // Although these fields are specific to editable text, they are not added to Editor because // they are defined by the TextView's style and are theme-dependent. - @UnsupportedAppUsage + @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) int mCursorDrawableRes; + private Drawable mCursorDrawable; // Note: this might be stale if setTextSelectHandleLeft is used. We could simplify the code // by removing it, but we would break apps targeting <= P that use it by reflection. @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) @@ -3640,6 +3641,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener /** * Returns the Drawable corresponding to the right handle used * for selecting text. + * Note that any change applied to the handle Drawable will not be visible + * until the handle is hidden and then drawn again. * * @return the right text selection handle drawable * @@ -3655,6 +3658,58 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } /** + * Sets the Drawable corresponding to the text cursor. The Drawable defaults to the + * value of the textCursorDrawable attribute. + * Note that any change applied to the cursor Drawable will not be visible + * until the cursor is hidden and then drawn again. + * + * @see #setTextCursorDrawable(int) + * @attr ref android.R.styleable#TextView_textCursorDrawable + */ + public void setTextCursorDrawable(@NonNull Drawable textCursorDrawable) { + Preconditions.checkNotNull(textCursorDrawable, + "The cursor drawable should not be null."); + mCursorDrawable = textCursorDrawable; + mCursorDrawableRes = 0; + if (mEditor != null) { + mEditor.loadCursorDrawable(); + } + } + + /** + * Sets the Drawable corresponding to the text cursor. The Drawable defaults to the + * value of the textCursorDrawable attribute. + * Note that any change applied to the cursor Drawable will not be visible + * until the cursor is hidden and then drawn again. + * + * @see #setTextCursorDrawable(Drawable) + * @attr ref android.R.styleable#TextView_textCursorDrawable + */ + public void setTextCursorDrawable(@DrawableRes int textCursorDrawable) { + Preconditions.checkArgumentPositive(textCursorDrawable, + "The cursor drawable should be a valid drawable resource id."); + setTextCursorDrawable(mContext.getDrawable(textCursorDrawable)); + } + + /** + * Returns the Drawable corresponding to the text cursor. + * Note that any change applied to the cursor Drawable will not be visible + * until the cursor is hidden and then drawn again. + * + * @return the text cursor drawable + * + * @see #setTextCursorDrawable(Drawable) + * @see #setTextCursorDrawable(int) + * @attr ref android.R.styleable#TextView_textCursorDrawable + */ + @Nullable public Drawable getTextCursorDrawable() { + if (mCursorDrawable == null && mCursorDrawableRes != 0) { + mCursorDrawable = mContext.getDrawable(mCursorDrawableRes); + } + return mCursorDrawable; + } + + /** * Sets the text appearance from the specified style resource. * <p> * Use a framework-defined {@code TextAppearance} style like |
