How To Set Text Color And Background Color In Android Programmatically

This example will tell you how to set the android.widget.TextView text foreground color and background color in source code. For other android UI components, it is similar to change foreground and background color like this.

1. Change TextView Foreground Background Color Programmatically Example.

  1. Below is this example screenshot.
    android-set-textview-foreground-backgroud-color-example
  2. There are one TextView and two Buttons on the above screen.
  3. When you click the first button, it will change the text color in the TextView.
  4. When you click the second button, it will change the background color of the TextView.

2. Example Source Code.

  1. Create a new android project and select the Empty Activity template. You can read How To Create New Android Studio Project to learn more.
  2. Then edit the MainActivity.java file and the activity_main.xml file content as below.

2.1 Main Activity Java File.

  1. MainActivity.java
    package com.dev2qa.textviewcolor;
    
    import android.graphics.Color;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import org.w3c.dom.Text;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            setTitle("dev2qa.com - Android Set TextView Foreground Background Color Example.");
    
            // Get the textview object.
            final TextView textView = (TextView)findViewById(R.id.text_view);
    
            // Get set text view foreground color button and make button text as normal.
            Button setForeGroundColorButton = (Button)findViewById(R.id.button_set_foreground_color);
            setForeGroundColorButton.setAllCaps(false);
            setForeGroundColorButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                      textView.setTextColor(Color.BLUE);
                }
            });
    
            // Get set text view background color button.
            Button setBackGroundColorButton = (Button)findViewById(R.id.button_set_background_color);
            setBackGroundColorButton.setAllCaps(false);
            setBackGroundColorButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    textView.setBackgroundColor(Color.YELLOW);
                }
            });
        }
    }

2.2 Main Activity Layout Xml File.

  1. app / res / layout / activity_main.xml
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="This is a textview."
            android:textSize="20dp"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/button_set_foreground_color"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Set Foreground Color"/>
    
            <Button
                android:id="@+id/button_set_background_color"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Set Background Color" />
    
        </LinearLayout>
    </LinearLayout>

6 thoughts on “How To Set Text Color And Background Color In Android Programmatically”

  1. Great tutorial! I found the step-by-step instructions really clear and helpful. It’s nice to see how easy it is to customize the text and background colors programmatically. Thanks for sharing these tips!

  2. Great post! The step-by-step guide made it really easy to understand how to change text and background colors programmatically in Android. I can’t wait to try this out in my own app. Thanks for sharing these valuable tips!

  3. Great post! I found the steps really easy to follow. The code snippets were super helpful for understanding how to change the text and background colors. Can’t wait to try this out in my own app! Thanks for sharing!

  4. This post is super helpful! I had been struggling to change text and background colors in my app. The step-by-step instructions made it really easy to understand. Thanks for sharing these Windows tricks!

  5. Great post! I appreciate the step-by-step approach you provided for changing text and background colors in Android. The examples were really helpful, especially for someone just getting started with UI customization. Looking forward to more tips like this!

  6. Great tips! I’ve always struggled with changing text and background colors programmatically, but your step-by-step guide made it so easy to understand. Thanks for sharing these valuable tricks for both Android and iPhone!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.