Android Text View is the subclass of android.view.view, as per android a TextView is the complete text editor but it's configured to not allowed the editing. So you can only use the TextView to display the Text. In this post we are going to learn how to customize the TextView in Android, will learn 5 tricks to change the default TextView in Android.
How to change the TextColor and Size of TextView
A Simple Text View can be added by using the below code in any layout.xml files.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A Simple Text View!" />
So first thing we are going to change the size and color of the text. Text size always recommended using in 'sp'.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A Colored Text View!"
android:textColor="#283593"
android:textSize="18sp" />
How to add the border in text view as like given below.
For adding the border like this we need to add border.xml inside our drawable folder. So create the a border.xml and paste below the line of code inside the border.xml.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="2dp"
android:color="#4FC3F7" />
</shape>
Now modify the TextView as given below, here we are setting the border.xml as the background of our TextView.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView With Border!"
android:textColor="#283593"
android:textSize="20sp"
android:background="@drawable/border"/>
How to make TextView show the contain in Upper case.
For this, we just have to add one more line to our TextView, so modify your TextView code and add the textAllCaps equal to true as given below in your layout file.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView In Upper Case!"
android:textColor="#283593"
android:textSize="20sp"
android:background="@drawable/border"
android:textAllCaps="true"/>
How to set the TextView to be in the center of your layout?
For this, we have to add layout_gravity of our TextView, so modify your TextView as given below.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView In Center!"
android:textColor="#283593"
android:textSize="20sp"
android:background="@drawable/border"
android:textAllCaps="true"
android:layout_gravity="center"
/>
How to restrict the number of lines to be shown in TextView?
For this, we use the max and min line parameter of TextView. Check the below given code to set the min and max line of the TextView.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView Line1!n Line2 !nLine3! nLine4! "
android:textColor="#283593"
android:textSize="20sp"
android:background="@drawable/border"
android:layout_gravity="center"
android:shadowColor="@color/colorPrimary"
android:maxLines="3"
android:minLines="2"
/>
How to create Auto Scrollable TextView?
Creating a auto scrollable TextView is android is bit tricky. We have just to do two steps, first setup the gravity of textview to bottom and second set the scrollable method to text view.
TextView Layout File
<TextView
android:layout_width="wrap_content"
android:layout_height="280dp"
android:text="Hello World!"
android:id="@+id/scrollableTextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="bottom"/>
Set the method of the scrollable movement method using below code.
textView.setMovementMethod(new ScrollingMovementMethod());
If you writing the code in Kotlin then you have to use the below code.
textView?.movementMethod=ScrollingMovementMethod()
[…] out the TextView customization tricks here at below link. […]
ReplyDelete