May 21, 2011

Change Text color using setTextColor()

Change Text color using setTextColor()

package com.exercise.AndroidTextColor;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;

public class AndroidTextColor extends Activity {

SeekBar seekRed, seekGreen, seekBlue, seekAlpha;
TextView targetText;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

targetText = (TextView)findViewById(R.id.targetview);

seekRed = (SeekBar)findViewById(R.id.seekred);
seekGreen = (SeekBar)findViewById(R.id.seekgreen);
seekBlue = (SeekBar)findViewById(R.id.seekblue);
seekAlpha = (SeekBar)findViewById(R.id.seekalpha);

seekRed.setOnSeekBarChangeListener(seekChangeListener);
seekGreen.setOnSeekBarChangeListener(seekChangeListener);
seekBlue.setOnSeekBarChangeListener(seekChangeListener);
seekAlpha.setOnSeekBarChangeListener(seekChangeListener);
}

private SeekBar.OnSeekBarChangeListener seekChangeListener
= new SeekBar.OnSeekBarChangeListener(){

@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
updateTextColor();
}

@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}};

private void updateTextColor(){

int red = seekRed.getProgress();
int green = seekGreen.getProgress();
int blue = seekBlue.getProgress();
int alpha = seekAlpha.getProgress();

targetText.setTextColor(
((alpha << 24) & 0xFF000000)
+ ((red << 16) & 0x00FF0000)
+ ((green << 8) & 0x0000FF00)
+ (blue & 0x000000FF));

}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RED"
/>
<SeekBar
android:id="@+id/seekred"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="GREEN"
/>
<SeekBar
android:id="@+id/seekgreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BLUE"
/>
<SeekBar
android:id="@+id/seekblue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ALPHA"
/>
<SeekBar
android:id="@+id/seekalpha"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="255"
/>
<TextView
android:id="@+id/targetview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="How to change Text Color?"
/>
</LinearLayout>


No comments:

Post a Comment

Infolinks In Text Ads