1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.text.style; 18 19 import android.annotation.ColorInt; 20 import android.annotation.NonNull; 21 import android.os.Parcel; 22 import android.text.ParcelableSpan; 23 import android.text.TextPaint; 24 import android.text.TextUtils; 25 26 /** 27 * Changes the background color of the text to which the span is attached. 28 * <p> 29 * For example, to set a green background color for a text you would create a {@link 30 * android.text.SpannableString} based on the text and set the span. 31 * <pre>{@code 32 * SpannableString string = new SpannableString("Text with a background color span"); 33 *string.setSpan(new BackgroundColorSpan(color), 12, 28, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}</pre> 34 * <img src="{@docRoot}reference/android/images/text/style/backgroundcolorspan.png" /> 35 * <figcaption>Set a background color for the text.</figcaption> 36 */ 37 public class BackgroundColorSpan extends CharacterStyle 38 implements UpdateAppearance, ParcelableSpan { 39 40 private final int mColor; 41 42 /** 43 * Creates a {@link BackgroundColorSpan} from a color integer. 44 * <p> 45 * 46 * @param color color integer that defines the background color 47 * @see android.content.res.Resources#getColor(int, Resources.Theme) 48 */ BackgroundColorSpan(@olorInt int color)49 public BackgroundColorSpan(@ColorInt int color) { 50 mColor = color; 51 } 52 53 /** 54 * Creates a {@link BackgroundColorSpan} from a parcel. 55 */ BackgroundColorSpan(@onNull Parcel src)56 public BackgroundColorSpan(@NonNull Parcel src) { 57 mColor = src.readInt(); 58 } 59 60 @Override getSpanTypeId()61 public int getSpanTypeId() { 62 return getSpanTypeIdInternal(); 63 } 64 65 /** @hide */ 66 @Override getSpanTypeIdInternal()67 public int getSpanTypeIdInternal() { 68 return TextUtils.BACKGROUND_COLOR_SPAN; 69 } 70 71 @Override describeContents()72 public int describeContents() { 73 return 0; 74 } 75 76 @Override writeToParcel(@onNull Parcel dest, int flags)77 public void writeToParcel(@NonNull Parcel dest, int flags) { 78 writeToParcelInternal(dest, flags); 79 } 80 81 /** @hide */ 82 @Override writeToParcelInternal(@onNull Parcel dest, int flags)83 public void writeToParcelInternal(@NonNull Parcel dest, int flags) { 84 dest.writeInt(mColor); 85 } 86 87 /** 88 * @return the background color of this span. 89 * @see BackgroundColorSpan#BackgroundColorSpan(int) 90 */ 91 @ColorInt getBackgroundColor()92 public int getBackgroundColor() { 93 return mColor; 94 } 95 96 /** 97 * Updates the background color of the TextPaint. 98 */ 99 @Override updateDrawState(@onNull TextPaint textPaint)100 public void updateDrawState(@NonNull TextPaint textPaint) { 101 textPaint.bgColor = mColor; 102 } 103 } 104