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>
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x