Skip to main content

5 ways to customize text view in android, add custom border in text view



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.


TextView


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"
/>

Text View



 

 

 

 

 

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()




If you like this then share it and like us on Facebook, Google, Tweeter by clicking on the Top-Right corner of the page.





Comments

Post a Comment

Popular posts from this blog

Flutter How to Start Android Activity from Flutter View

Flutter and Dart is an excellent combination for creating the UI, but for accessing the platform-specific service we need to open platform-specific activity. So lets in this article we will explore how to start an android activity and access the service from Flutter View. Create a Project for this Android Activity Flutter View Demo Create a Project From File menu select the New Flutter Project Enter the project name Select the AndroidX support and click on next After the above, we step click on Finish We will have the following project structure created. Create the Second Activity in Android Just go to the android folder and open it in separate windows. We will have the following project structure. Create the Activity Just right-click on the Kotlin folder and create a blank activity from the menu. If you create the activity then you may be required to upgrade the Gradle and do some import. So Just click on update and wait for the project s

Kotlin Parcelable Array Objects Send To Activity

We know that IPC (Inter Process Communication) between the activity is an extremely important part of any application development. We often required that we need to send some data to other activity. For example, we may be required to send an array of data, data could be an Integer, String, Long, Double, Float or any other custom data objects. So, In this example, we are going to learn how to implement the Kotlin Parcelable Array object to send the data from one activity to second activity. What is Parcel? The parcel class is designed as a high-performance IPC transport. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC, and references to live IBinde r objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel. Create Kotlin Parcelable Array Objects Parcelable is API for placing the arbitrary objects into the Parcel. In Actual in android app development, Parcelable is an interface

Scan and List All Available WiFi Network In Android

In this tutorial, we learn how to connect to a WiFi hotspot in android by code. In this Demo we will create an as small app which will scan the all available network or Hotspot and list down the network when you select specific network, the application will connect that particular network. You May Like Below Topic: How to Read and Write JSON data using GSON WP Android App using REST and volley WP Android App using REST and volley part2 Implementation of SwipeRefreshLayout in RecyclerView Create Amazing Bottom Navigation Bar Without Any External Library Introduction In this tutorial, we learn how to connect to a WiFi hotspot in android by code. In this Demo we will create an as small app which will scan the all available network or Hotspot and list down the network when you select specific network, the application will connect that particular network. We will add this functionality to our existing Demo app " Video Gallery ". If you would like to check out t