Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaHow to Set the Text Color of TextView in Code?

How to Set the Text Color of TextView in Code?

One of the most often used User Interface elements is TextView. The user experience is improved, and the application looks better, due to the clear and organized text. It doesn’t matter what kind of information is being shown, it should always be done well. In an android app, TextView allows us to do this, thus understanding its properties is crucial.

There are two methods of changing the color of a TextView and the code for that has been given in both Java and Kotlin Programming Language for Android, so it can be done by directly adding a color attribute in the XML code or we can change it through the MainActivity File. By default, the color of the text will be black until it is changed.

Method 1: Changing Text Color in XML File

This is the easiest and recommended method with the following syntax.

Syntax:

android:textColor="hex_code"

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/exampleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GeeksForGeeks"
        android:textColor="#0F9D58"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
</androidx.constraintlayout.widget.ConstraintLayout>


Method 2: Changing Text Color in MainActivity File

Syntax in Java:

TextView exampleTxt = findViewById(R.id.exampleText);
exampleTxt.setTextColor(Color.parseColor("hex_code"));

Syntax in Kotlin:

val exampleTxt = findViewById<TextView>(R.id.exampleText)
exampleTxt.setTextColor(Color.parseColor("hex_code"))

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/exampleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GeeksForGeeks"
        android:textColor="@color/black"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
</androidx.constraintlayout.widget.ConstraintLayout>


Java




import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
  
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        TextView exampleTxt = findViewById(R.id.exampleText);
        exampleTxt.setTextColor(Color.parseColor("#0F9D58"));
    }
}


Kotlin




import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        val exampleTxt = findViewById<TextView>(R.id.exampleText)
        exampleTxt.setTextColor(Color.parseColor("#0F9D58"))
    }
}


Output:

The output for both the methods would be the same, just the method of implementation is different.

Text Color of Text View in Android

 

RELATED ARTICLES

Most Popular

Recent Comments