May 22, 2011

Change background color using Java code, setBackgroundColor()

Change background color using Java code, setBackgroundColor()

package com.AndroidBackgroundColor;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;

public class AndroidBackgroundColor extends Activity {

SeekBar seekRed, seekGreen, seekBlue, seekAlpha;
View targetView;

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

targetView = (View)findViewById(R.id.mainlayout);

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
updateBackgroundColor();
}

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

}

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

}};

private void updateBackgroundColor(){

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

targetView.setBackgroundColor(
((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"
android:id="@+id/mainlayout"
>
<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"
/>
</LinearLayout>


1 comment:

Infolinks In Text Ads