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.NonNull; 20 import android.os.Parcel; 21 import android.text.Layout; 22 import android.text.ParcelableSpan; 23 import android.text.TextUtils; 24 25 /** 26 * Span that allows defining the alignment of text at the paragraph level. 27 */ 28 @android.ravenwood.annotation.RavenwoodKeepWholeClass 29 public interface AlignmentSpan extends ParagraphStyle { 30 31 /** 32 * Returns the alignment of the text. 33 * 34 * @return the text alignment 35 */ getAlignment()36 Layout.Alignment getAlignment(); 37 38 /** 39 * Default implementation of the {@link AlignmentSpan}. 40 * <p> 41 * For example, a text written in a left to right language, like English, which is by default 42 * aligned to the left, can be aligned opposite to the layout direction like this: 43 * <pre>{@code SpannableString string = new SpannableString("Text with opposite alignment"); 44 *string.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE), 0, 45 *string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}</pre> 46 * <img src="{@docRoot}reference/android/images/text/style/ltralignmentspan.png" /> 47 * <figcaption>Align left to right text opposite to the layout direction.</figcaption> 48 * <p> 49 * A text written in a right to left language, like Hebrew, which is by default aligned to the 50 * right, can be aligned opposite to the layout direction like this: 51 * <pre>{@code SpannableString string = new SpannableString("טקסט עם יישור הפוך"); 52 *string.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE), 0, 53 *string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}</pre> 54 * <img src="{@docRoot}reference/android/images/text/style/rtlalignmentspan.png" /> 55 * <figcaption>Align right to left text opposite to the layout direction.</figcaption> 56 */ 57 class Standard implements AlignmentSpan, ParcelableSpan { 58 private final Layout.Alignment mAlignment; 59 60 /** 61 * Constructs a {@link Standard} from an alignment. 62 */ Standard(@onNull Layout.Alignment align)63 public Standard(@NonNull Layout.Alignment align) { 64 mAlignment = align; 65 } 66 67 /** 68 * Constructs a {@link Standard} from a parcel. 69 */ Standard(@onNull Parcel src)70 public Standard(@NonNull Parcel src) { 71 mAlignment = Layout.Alignment.valueOf(src.readString()); 72 } 73 74 @Override getSpanTypeId()75 public int getSpanTypeId() { 76 return getSpanTypeIdInternal(); 77 } 78 79 /** @hide */ 80 @Override getSpanTypeIdInternal()81 public int getSpanTypeIdInternal() { 82 return TextUtils.ALIGNMENT_SPAN; 83 } 84 85 @Override describeContents()86 public int describeContents() { 87 return 0; 88 } 89 90 @Override writeToParcel(@onNull Parcel dest, int flags)91 public void writeToParcel(@NonNull Parcel dest, int flags) { 92 writeToParcelInternal(dest, flags); 93 } 94 95 /** @hide */ 96 @Override writeToParcelInternal(@onNull Parcel dest, int flags)97 public void writeToParcelInternal(@NonNull Parcel dest, int flags) { 98 dest.writeString(mAlignment.name()); 99 } 100 101 @Override getAlignment()102 public Layout.Alignment getAlignment() { 103 return mAlignment; 104 } 105 106 @Override toString()107 public String toString() { 108 return "AlignmentSpan.Standard{alignment=" + getAlignment() + '}'; 109 } 110 } 111 } 112