In this article, we record a few methods that are not commonly used in TextView.





TextView is not a common method effect.gif

SetTextIsSelectable (Boolean selectable) :

SetTextIsSelectable (Boolean Selectable) corresponds to android:textIsSelectable in XML, which is used to declare whether the content in the TextView can be selected.

SetTextIsSelectable screenshots:





Optional. PNG

SetSelectAllOnFocus (Boolean selectAllOnFocus) :

SetSelectAllOnFocus (Boolean selectAllOnFocus) corresponds to Android :selectAllOnFocus in XML, which is used to declare whether the entire TextView/EditText instance is selected after getting focus.

SetSelectAllOnFocus screenshots:





Optional + All PNG

SetHighlightColor (@ ColorInt int color) :

SetHighlightColor (@ ColorInt int color) in the corresponding XML android: textColorHighlight, used to declare the TextView is selected in the content of highlighting the background color.

SetHighlightColor screenshots:





Optional + Select highlight background color.png

Links related:

setAutoLinkMask(int mask)

SetAutoLinkMask (int mask) corresponds to Android :autoLink in XML and is used to declare the link types recognized by TextView instances.

setLinkTextColor(@ColorInt int color)

SetLinkTextColor (@colorInt int color) corresponds to Android :textColorLink in XML, which is used to set the color of links in TextView instances.

setLinksClickable(boolean whether)

SetLinksClickable (Boolean whether) Corresponds to Android in XML :linksClickable, which is used to set whether links in TextView instances are clickable or whether clicks perform corresponding actions.

setTextScaleX(float size)

SetTextScaleX (float size) corresponds to Android :textScaleX in XML, which is used to set the horizontal stretch ratio of text in TextView instances.

SetTextScaleX screenshot (highlighted yellow area) :





Text horizontal stretch multiple highlighted. PNG

Set TextView height to row dependent:

setMinLines(int minlines)

SetMinLines (int minlines) corresponds to Android: minlines in XML and is used to set the minimum height of the TextView to the specified line height.

setMaxLines(int maxlines)

SetMaxLines (int maxlines) corresponds to Android: maxlines in XML and is used to set the maximum height of the TextView to the specified line height.

setLines(int lines)

SetLines (int lines) corresponds to Android :lines in XML and is used to set the exact height of the TextView to the specified line height.

Set the TextView height to the number of rows





Line number highlight.png

android:password

Android: Password is used to set whether the content in the TextView is plaintext.

setShadowLayer(float radius, float dx, float dy, int color)

SetShadowLayer (float radius, float dx, float dy, int color) corresponds to android:shadowRadius, Android :shadowDx, Android :shadowDy, Android :shadowColor, used to set text shadow.

SetShadowLayer screenshot (yellow highlighted area) :





Shadow highlight.png

setEllipsize(TextUtils.TruncateAt where)

SetEllipsize (textutils.truncateAT where) corresponds to Android: EllipSIZE in XML and is used for text ellipsis style Settings.

setCompoundDrawablePadding(int pad)

SetCompoundDrawablePadding (int pad) corresponding to the android: XML drawablePadding, used to set the TextView characters with all round the drawable spacing.

SetCompoundDrawablePadding screenshots (yellow highlighted area) :





DrawablePadding highlighted. PNG

setCompoundDrawableTintList(@Nullable ColorStateList tint)

SetCompoundDrawableTintList (@ Nullable ColorStateList tint) corresponding to the android: XML drawableTint, used to set up around the TextView drawable coloring.

Set the color of the images around the TextView only if SDK>=23.

{@link Drawable#setColorFilter(int, porterduff.mode)} method, in Java code to manually color around the image

Example: Tested on Android4.22Drawablecompat.settint (d1, color.red); drawablecompat.settint (d1, color.red); drawablecompat.settint (d1, color.red); DrawableCompat.setTintMode(d1, PorterDuff.Mode.SRC_ATOP); Drawable d1 = getResources().getDrawable(r.map.i1).mutate(); d1.setBounds(0.0.64.64);
d1.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
Drawable d2 = getResources().getDrawable(R.mipmap.i2).mutate();
d2.setBounds(0.0.64.64);
d2.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
Drawable[] drawables = tv_drawableTint.getCompoundDrawables();
drawables[0] = d1;
drawables[2] = d2;
tv_drawableTint.setCompoundDrawables(drawables[0],drawables[1],drawables[2],drawables[3]);Copy the code
SetLineSpacing (float Add, float Mult) :

SetLineSpacing (float add, float mult) corresponding to the android: XML lineSpacingMultiplier and android: lineSpacingExtra, used to set the multi-line TextView in single row height.

SetLineSpacing screenshot (yellow highlighted area) :





Single line height yellow highlight in multi-line content. PNG

setLetterSpacing(float letterSpacing)

SetLetterSpacing (float letterSpacing) corresponds to Android :letterSpacing in XML, which is used to set the character spacing of text.

SetLetterSpacing screenshot (yellow highlighted area) :





Character spacing yellow highlighted. PNG

setAllCaps(boolean allCaps)

SetAllCaps (Boolean allCaps) Corresponds to Android :textAllCaps in XML, which is used to set whether all characters in the TextView are displayed in uppercase.

setTransformationMethod(TransformationMethod method)

SetTransformationMethod (TransformationMethod Method) is used to set the conversion rules between the TextView presentation and the actual content.

setTextSize(int unit, float size)

SetTextSize (int Unit, float Size) sets the text size of the TextView instance to the specified value size of the specified unit.

XML and Java code:
<?xml version="1.0" encoding="utf-8"? >
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_article1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="TextIsSelectable: Whether text can be selected"
            />
        <TextView
            android:id="@+id/tv_textIsSelectable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="Test text is selectable \n Test text is selectable"
            android:textIsSelectable="true"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="SelectAllOnFocus: Is the text selected after the focus is obtained?"
            />
        <EditText
            android:id="@+id/tv_selectAllOnFocus"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="Is the text selected after it gets focus?"
            android:textIsSelectable="true"
            android:selectAllOnFocus="true"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="TextColorHighlight: The background color highlighted when the text is selected"
            />
        <TextView
            android:id="@+id/tv_textColorHighlight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="Highlight background color when text is selected \n Highlight background color when text is selected"
            android:textIsSelectable="true"
            android:textColorHighlight="#36B34A"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint=Link correlation :autoLink+textColorLink+linksClickable
            />
        <TextView
            android:id="@+id/tv_link"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="http://baidu.com\n Why headphones Fox \n2346726\[email protected]"
            android:autoLink="all"
            android:textColorLink="@color/colorPrimary"
            android:linksClickable="true"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="TextScaleX: Horizontal stretching multiple of text"
            />
        <TextView
            android:id="@+id/tv_textScaleX"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="hgjkshgkhsdkfgjldfjhgkhshjg; ld"
            android:textScaleX="2.6"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="MinLines: Set the minimum height of TextView to the specified line height \n Current :minLines=3"
            />
        <TextView
            android:id="@+id/minLines"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="The text is only one line."
            android:minLines="3"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="MaxLines: Set the maximum height of TextView to the specified line height \n Current :maxLines=2 Text has 5 lines"
            />
        <TextView
            android:id="@+id/maxLines"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="First line \n second line \n third line \n fourth line \n fifth line"
            android:maxLines="2"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Lines: Sets the TextView precision height to the specified line height \n Current :lines=2 Text has 5 lines"
            />
        <TextView
            android:id="@+id/lines"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="First line \n second line \n third line \n fourth line \n fifth line"
            android:lines="2"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password: Sets whether content is plaintext"
            />
        <TextView
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="Test text test text test text test text test text test text test text"
            android:password="true"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Shadow: Shadow correlation"
            />
        <TextView
            android:id="@+id/tv_shadow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="Test text test text test text test text test text test text test text"
            android:shadowDx="12.0"
            android:shadowDy="16.0"
            android:shadowColor="#EA2000"
            android:shadowRadius="8.0"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Ellipsize: Text ellipsis style Settings"
            />
        <TextView
            android:id="@+id/tv_ellipsize"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="Test Text test Text Test Text Test Text Test Text Test Text Test Text Test Text Test"
            android:maxLines="1"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="DrawablePadding: Sets the distance between the text and the drawable around the TextView."
            />
        <TextView
            android:id="@+id/tv_drawablePadding"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="Test text test text"
            android:drawableLeft="@mipmap/ic_launcher"
            android:drawablePadding="32dp"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint=DrawableTint: Sets the color of the drawable around the TextView.
            />
        <TextView
            android:id="@+id/tv_drawableTint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="Test text test text"
            android:gravity="center"
            android:drawableLeft="@mipmap/ic_launcher"
            android:drawableTop="@mipmap/ic_launcher"
            android:drawableRight="@mipmap/ic_launcher"
            android:drawableBottom="@mipmap/ic_launcher"
            android:drawableTint="#3A92FF"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="LineSpacing: Set single row height in multi-line TextView"
            />
        <TextView
            android:id="@+id/tv_lineSpacing_original"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:text="Test text test text test text test text test text \n Test text test text test text test text"
            android:lineSpacingMultiplier="1.0"
            android:lineSpacingExtra="0dp"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@android:color/darker_gray"
            />
        <TextView
            android:id="@+id/tv_lineSpacing_setting"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:text="Test text test text test text test text test text \n Test text test text test text test text"
            android:lineSpacingMultiplier="2.0"
            android:lineSpacingExtra="16dp"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="LetterSpacing: Sets the character spacing of text"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="LetterSpacing = 2.0"
            android:letterSpacing="2.0"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="LetterSpacing = 0.2"
            android:letterSpacing="0.2"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="TextAllCaps: Whether the text is all caps"
            />
        <TextView
            android:id="@+id/tv_textAllCaps"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="ghkldjgkldjlkldffjgjfdslkfh; hkslkhfgkdj"
            android:textAllCaps="true"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="SetTransformationMethod: Sets the conversion rules between the TextView presentation and the actual content"
            />
        <TextView
            android:id="@+id/tv_SetTransformationMethod"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="abcdefghijklmn"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="SetTextSize (int unit, float size): Sets the text size to the specified value of the specified unit."
            />
        <TextView
            android:id="@+id/tv_setTextSize"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textColor="@android:color/black"
            android:text="setTextSize(int unit, float size)"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/colorAccent"
            />
    </LinearLayout>

</ScrollView>Copy the code
public class Article1Activity extends AppCompatActivity {
    private TextView tv_ellipsize = null;
    int index = 0;
    private TextUtils.TruncateAt[] ellipsizes = new TextUtils.TruncateAt[]{
            TextUtils.TruncateAt.START,
            TextUtils.TruncateAt.MIDDLE,
            TextUtils.TruncateAt.END
    };
    Handler handlerEllipsize = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            index = ++index%ellipsizes.length;
            tv_ellipsize.setEllipsize(ellipsizes[index]);
            handlerEllipsize.sendMessageDelayed(handlerEllipsize.obtainMessage(),2000); }}; Handler handlerTransformationMethod =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            int what = 0;
            if(msg.what == 0) {/ / the capital
                tv_SetTransformationMethod.setTransformationMethod(new MyTransformationMethod(true));
                what = 1;
            }else {
                / / lowercase
                tv_SetTransformationMethod.setTransformationMethod(new MyTransformationMethod());
                what = 0;
            }
            handlerTransformationMethod.sendEmptyMessageDelayed(what,2000); }};private TextView tv_SetTransformationMethod = null;
    private int[] units = new int[]{
            TypedValue.COMPLEX_UNIT_PX,
            TypedValue.COMPLEX_UNIT_DIP,
            TypedValue.COMPLEX_UNIT_SP
    };
    Handler handlerSetTextSize = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            index = ++index%units.length;
            tv_setTextSize.setTextSize(units[index],24.0 f);
            handlerSetTextSize.sendMessageDelayed(handlerSetTextSize.obtainMessage(),2000); }};private TextView tv_setTextSize = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article1);
        tv_ellipsize = (TextView) findViewById(R.id.tv_ellipsize);
        handlerEllipsize.sendMessage(handlerEllipsize.obtainMessage());
        tv_SetTransformationMethod = (TextView) findViewById(R.id.tv_SetTransformationMethod);
        handlerTransformationMethod.sendEmptyMessage(0);
        tv_setTextSize = (TextView) findViewById(R.id.tv_setTextSize);
        handlerSetTextSize.sendEmptyMessage(0);
    }
    class MyTransformationMethod implements TransformationMethod {
        private boolean upperCase = false;
        public MyTransformationMethod(boolean. upperCase){
            this.upperCase = (upperCase==null||upperCase.length<=0)?false:upperCase[0];
        }
        @Override
        public CharSequence getTransformation(CharSequence source, View view) {
            if(this.upperCase){
                return TextUtils.isEmpty(source)?"":source.toString().toUpperCase(getResources().getConfiguration().locale)+"Capital";
            }else{
                return TextUtils.isEmpty(source)?"":source.toString().toLowerCase(getResources().getConfiguration().locale)+"Small"; }}@Override
        public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect) {}}}Copy the code

The above is the personal experiment and analysis of a point of results, if there is a mistake, please inform the students message!

That’s all !