Saturday, January 3, 2015

How to change TextView background color in android

TextView background color
TextView display text on android application. android developer can change TextView background color both programmatically at run time in java file and statically by declarative syntax in xml layout file or xml xml resource file.

the following example code demonstrate us how can we set or change the TextView background color by declarative syntax. in this example we did not make any changes on java file, so we does not include java file here.
activity_main.xml

<LinearLayout
    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"
    android:orientation="vertical"
    android:layout_margin="25dp"
    tools:context=".MainActivity"
    >

    <TextView
        android:id="@+id/text_view1"
        android:text="TextView background color (#FF0) [Hexadecimal Color #RGB]"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:background="#FF0"
        />

    <TextView
        android:id="@+id/text_view2"
        android:text="TextView background color with alpha (#5F00) [Hexadecimal Color #ARGB]"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:background="#5F00"
        />

    <TextView
        android:id="@+id/text_view3"
        android:text="TextView background color (#9932CC) [Hexadecimal Color #RRGGBB]"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:background="#9932CC"
        />

    <TextView
        android:id="@+id/text_view4"
        android:text="TextView background color with alpha (#22F51F8B) [Hexadecimal Color #AARRGGBB]"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:background="#22F51F8B"
        />

    <TextView
        android:id="@+id/text_view5"
        android:text="TextView background color [@android:color/holo_blue_light]"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:background="@android:color/holo_blue_light"
        />

    <TextView
        android:id="@+id/text_view6"
        android:text="TextView background color (@color/Wheat) [color from xml resource file]"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:background="@color/Wheat"
        />

    <TextView
        android:id="@+id/text_view7"
        android:text="TextView background color (@color/AliceBlue) [color from xml resource file]"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:background="@color/AliceBlue"
        />

</LinearLayout>
in this exampe code we include few TextView widgets. we define their background color by various way. the simple way to define TextView background color is its android:background attribute. we can assign a Hexadecimal color string for this attribute to generate TextView background color.

Hexadecimal color is specified with an RGB value and optional alpha channel. in this example code we uses the following hexadecimal color format to define Textview background color, those are #RGB, #ARGB, #RRGGBB and #AARRGGBB. in this hexadecimal color format 'A' describe alpha channel, 'R' describe red channel, 'G' describe green channel and 'B' describe blue channel.

Color is a simple resource which we can reference using the value provided in the name attribute not the name of xml file. so we can combine color resources with other simple resources in a single xml file. ech color resource is a name value pair. resource file saved on res/values folder.

in this example we create a resource file to store color names. this xml resource file name is colors.xml and its located at res/values/colors.xml. we saved our preferred colors name and value in the xml file to use them on app widgets.

finally. we can define TextView background color from xml resource file by android:background attribute as android:background="@color/AliceBlue". the specified TextView will display AliceBlue background on app.
res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="Wheat">#F5DEB3</color>
    <color name="PapayaWhip">#FFEFD5</color>
    <color name="Cornsilk">#FFF8DC</color>
    <color name="AliceBlue">#F0F8FF</color>
</resources>


the following image shows the xml resource file's coding and loaction in file tree.



the following image displays the output of this example app. this app demonstrate the syntaxtical way to define TextView background color.

Set TextView background color programmatically

activity_main.xml

<LinearLayout
    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"
    android:orientation="vertical"
    android:layout_margin="25dp"
    tools:context=".MainActivity"
    >

    <TextView
        android:id="@+id/text_view1"
        android:text="Sample TextView 1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        />

    <TextView
        android:id="@+id/text_view2"
        android:text="Sample TextView 2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        />

    <TextView
        android:id="@+id/text_view3"
        android:text="Sample TextView 3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        />

    <TextView
        android:id="@+id/text_view4"
        android:text="Sample TextView 4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        />

    <TextView
        android:id="@+id/text_view5"
        android:text="Sample TextView 5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        />

    <TextView
        android:id="@+id/text_view6"
        android:text="Sample TextView 6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        />

    <Button
        android:id="@+id/push_button"
        android:text="Change Background Color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="perform_action"
        />

</LinearLayout>
MainActivity.java

package com.cfsuman.me.myapplication5;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.graphics.Color;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

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

    public void perform_action(View v)
    {
        TextView tv1 = (TextView) findViewById(R.id.text_view1);
        tv1.setBackgroundColor(0xFF12FF45);
        tv1.setText("Background color from int color (0xFF12FF45)");

        TextView tv2 = (TextView) findViewById(R.id.text_view2);
        tv2.setBackgroundColor(0x55FF0000);
        tv2.setText("Background color from int color with alpha (0x55FF0000)");

        TextView tv3 = (TextView) findViewById(R.id.text_view3);
        tv3.setBackgroundColor(Color.parseColor("#FF5555"));
        tv3.setText("Background color from Color.parseColor() method (#FF5555)");

        TextView tv4 = (TextView) findViewById(R.id.text_view4);
        tv4.setBackgroundColor(Color.parseColor("#33FF5555"));
        tv4.setText("Background color from Color.parseColor() method with alpha (#33FF5555)");

        TextView tv5 = (TextView) findViewById(R.id.text_view5);
        tv5.setHighlightColor(Color.CYAN);
        tv5.setBackgroundColor(tv5.getHighlightColor());
        tv5.setText("Background color from getHighlightColor() method");

        TextView tv6 = (TextView) findViewById(R.id.text_view6);
        tv6.setBackgroundColor(getResources().getColor(R.color.Cornsilk));
        tv6.setText("Background color from xml resource file (Cornsilk)");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


the following image show the intitial state of the second example.



the following image display the result of second example android app. when someone click the push button the app show the different background color for different TextView widgets.

This second example code demonstrate us how can we define TextView background color programmatically at run time using java file. setBackgroundColor() method allow us to set background color for a view. this method need to pass a parameter named 'color'. this 'color' parameter value is int representation of a color.

parseColor() method allow us to parse a color string and return the corresponding color-int. parseColor(String colorString) method support many formats as example #RRGGBB, #AARRGGBB, 'red', 'blue', 'green', 'grey', 'magenta' etc.

we can also define TextView background color from its highlight color as setBackgroundColor(TextView.getHighlightColor()). this assign background color of a specified TextView widget from specified TextView widget's highlighted color.

android developers also can define TextView background color programmatically from xml resource file. to do this we need to call two methods named getResources() and getColor(). the final calling format is as setBackgroundColor(getResources().getColor(R.color.Cornsilk)) . getResources() method return a resource instance for our application package. getColor() method return a color integer associated with a particular resource id.
More android examples