android-logo

TextView in Android

Table of Content

  • Set TextView color
  • Change background color
  • Set font capital
  • Set font family
  • Set font size

A TextView in Android displays to the user and allows them to programmatically edit it. The TextView class is derived from the View class. In terms of the user experience, this has a significant impact on how information is displayed. TextView will behave like a label control and you will not be able to edit it.


TextView code in XML


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:p="Android Aura"/>


TextView code in Java


TextView tv = findViewById(R.id.tv);
tv.setText("Android Aura"); 

activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:p="Welcome to Androidaura!"
        android:textColor="#03A9F4"
        android:textSize="22dp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Output
text-view-1

Set TextView color


//XML
android:textColor="#1C83EA"
android:textColor="@color/colorAccent"

//JAVA
tv.setTextColor(Color.BLUE);
tv.setTextColor(Color.parseColor("#00abd7"));

//styles.xml
//<item name="colorAccent">@color/colorAccent</item>
tv.setTextColor(getResources().getColor(R.color.colorAccent)); 


Change background color


//XML
android:background="#F7DC6F"

//JAVA
tv.setBackgroundColor(Color.parseColor("#F7DC6F")); 


Set font capital


//XML
android:textAllCaps="true"

//JAVA
tv.setAllCaps(true); 


Set font family


//XML
android:fontFamily="sans-serif" 


Set font size


//XML
android:textStyle="bold"
android:textStyle="italic"
android:textStyle="normal"

//JAVA
tv.setTypeface(Typeface.DEFAULT_BOLD);


previous-button
next-button

facebook-logo
twitter-logo
instagram-logo

2020 © All rights reserved.