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 Theme Creation, Programmatic Way

Flutter and Dart is an excellent combination for creating the mobile app with material design with nice GUI. Its lots of widget which help the developer for creating the app UI very fast with respect to the other language. You may face some problem in starting due to comm, curly brace and semicolon but after some time you will find the way how to work with these all stuff and it becomes very easy for you. But in this article, we are not going to discuss all these. In this article, we will handle a very interesting topic on how to create a custom theme or multiple themes and let's user decide which theme to use.



Create a flutter project



So let's create a flutter project by selecting the appropriate option from the File menu in android studio or Visual Code. If you are new to Flutter and Dart you can check out our recent post to get started Creating Cross-platform app using flutter.



Once your project is created you will get the default counter app and you try running the app in the…

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 sync completed.
Update the project build config …

WordPress Android App with REST API And Retrofit

Retrofit is the library which converts your JSON data into Java object. In this tutorial, we will learn about how to implement the Retrofit to create a WordPress Mobile App. As we all know, WordPress is the one of the biggest CMS (Content Management System) of the world. Wordpress full fill the requirements for all most every type of the website. So, in this tutorial, I am going to learn how to create an Android App using the WordPress REST API and Retrofit.
What is Retrofit?
If you want to know about Retrofit, I would like to give you just a simple one-line explanation of the Retrofit. It is a library which converts your REST HTTP request to Java interface. We will learn how we can do all these stuff using some very simple steps.

Further, you can check the Retrofit Library here.

So, In this tutorial, we will use the Retrofit to retrieve all post and there except ( A short Description of Post) form a WordPress website.
Prerequisite for using the Retrofit for WordPress Android App
There is s…