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