Pages

Subscribe:

Wednesday, February 2, 2011

Android - Sample code for RGB Values


Screen


Layout

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/btnRed"
android:layout_width="70px"
android:layout_height="wrap_content"
android:text="Red"
android:layout_x="38px"
android:layout_y="334px"
>
</Button>
<Button
android:id="@+id/btnGreen"
android:layout_width="68px"
android:layout_height="wrap_content"
android:text="Green"
android:layout_x="130px"
android:layout_y="334px"
>
</Button>
<Button
android:id="@+id/btnBlue"
android:layout_width="72px"
android:layout_height="wrap_content"
android:text="Blue"
android:layout_x="223px"
android:layout_y="336px"
>
</Button>
<EditText
android:id="@+id/txtRed"
android:layout_width="87px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="146px"
android:layout_y="27px"
>
</EditText>
<TextView
android:id="@+id/widget34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
android:layout_x="61px"
android:layout_y="38px"
>
</TextView>
<TextView
android:id="@+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green"
android:layout_x="56px"
android:layout_y="94px"
>
</TextView>
<EditText
android:id="@+id/txtGreen"
android:layout_width="83px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="147px"
android:layout_y="81px"
>
</EditText>
<EditText
android:id="@+id/txtBlue"
android:layout_width="80px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="148px"
android:layout_y="146px"
>
</EditText>
<TextView
android:id="@+id/widget38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue"
android:layout_x="54px"
android:layout_y="158px"
>
</TextView>
<Button
android:id="@+id/btnChangeColor"
android:layout_width="168px"
android:layout_height="wrap_content"
android:text="Change Color"
android:layout_x="79px"
android:layout_y="217px"
>
</Button>
<TextView
android:id="@+id/txtMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Message"
android:textSize="22sp"
android:textStyle="bold"
android:layout_x="99px"
android:layout_y="278px"
>
</TextView>
</AbsoluteLayout>


Java Code

package com.rgb.sample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RGBSample extends Activity {
EditText redValue;
EditText greenValue;
EditText blueValue;
TextView messageText;
Button redButton;
Button greenButton;
Button blueButton;
Button changeColorButton;
Resources myResources;
Context context;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        try {
context = this.getApplicationContext();
myResources = getResources();
messageText = (TextView) findViewById(R.id.txtMessage);
redValue = (EditText) findViewById(R.id.txtRed);
greenValue = (EditText) findViewById(R.id.txtGreen);
blueValue = (EditText) findViewById(R.id.txtBlue);
redButton = (Button) findViewById(R.id.btnRed);
greenButton = (Button) findViewById(R.id.btnGreen);
blueButton = (Button) findViewById(R.id.btnBlue);
changeColorButton = (Button) findViewById(R.id.btnChangeColor);
redButton.setOnClickListener((OnClickListener) new applyRed());
greenButton.setOnClickListener((OnClickListener) new applyGreen());
blueButton.setOnClickListener((OnClickListener) new applyBlue());
changeColorButton.setOnClickListener((OnClickListener) new changeColor());
} catch (Exception e) {
e.printStackTrace();
}
        
        
    }
    
    class changeColor implements Button.OnClickListener {

@Override
public void onClick(View v) {
Integer rv = 0,gv = 0,bv = 0;
if (validRedValue() && validGreenValue() && validBlueValue()) {
rv = Integer.parseInt(redValue.getText().toString().trim());
gv = Integer.parseInt(greenValue.getText().toString().trim());
bv = Integer.parseInt(blueValue.getText().toString().trim());
messageText.setTextColor(Color.rgb(rv, gv, bv));
Toast.makeText(context, myResources.getText(R.string.COLOR_CHANGED), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, myResources.getText(R.string.COLOR_NOT_CHANGED), Toast.LENGTH_LONG).show();
}
}
public boolean validRedValue() {
Integer rv = 0;
try {
rv = Integer.parseInt(redValue.getText().toString().trim());
} catch (NumberFormatException e) {
Toast.makeText(context, myResources.getText(R.string.UNABLE_TO_PARSE_RED_VALUE), Toast.LENGTH_LONG).show();
return false;
}
if (rv < 0) {
Toast.makeText(context, myResources.getText(R.string.RED_CROSSES_LOWER_LIMIT), Toast.LENGTH_LONG).show();
return false;
} else if (rv > 255) {
Toast.makeText(context, myResources.getText(R.string.RED_CROSSES_UPPER_LIMIT), Toast.LENGTH_LONG).show();
return false;
}
return true;
}
public boolean validGreenValue() {
Integer gv = 0;
try {
gv = Integer.parseInt(greenValue.getText().toString().trim());
} catch (NumberFormatException e) {
Toast.makeText(context, myResources.getText(R.string.UNABLE_TO_PARSE_GREEN_VALUE), Toast.LENGTH_LONG).show();
return false;
}
if (gv < 0) {
Toast.makeText(context, myResources.getText(R.string.GREEN_CROSSES_LOWER_LIMIT), Toast.LENGTH_LONG).show();
return false;
} else if (gv > 255) {
Toast.makeText(context, myResources.getText(R.string.GREEN_CROSSES_UPPER_LIMIT), Toast.LENGTH_LONG).show();
return false;
}
return true;
}
public boolean validBlueValue() {
Integer bv = 0;
try {
bv = Integer.parseInt(blueValue.getText().toString().trim());
} catch (NumberFormatException e) {
Toast.makeText(context, myResources.getText(R.string.UNABLE_TO_PARSE_BLUE_VALUE), Toast.LENGTH_LONG).show();
return false;
}
if (bv < 0) {
Toast.makeText(context, myResources.getText(R.string.BLUE_CROSSES_LOWER_LIMIT), Toast.LENGTH_LONG).show();
return false;
} else if (bv > 255) {
Toast.makeText(context, myResources.getText(R.string.BLUE_CROSSES_UPPER_LIMIT), Toast.LENGTH_LONG).show();
return false;
}
return true;
}

    
    }
    
    class applyRed implements Button.OnClickListener {

@Override
public void onClick(View v) {
messageText.setTextColor(myResources.getColor(R.color.RED));
redValue.setText("255");
greenValue.setText("0");
blueValue.setText("0");
}
    
    }
    
    class applyGreen implements Button.OnClickListener {

@Override
public void onClick(View v) {
messageText.setTextColor(myResources.getColor(R.color.GREEN));
redValue.setText("0");
greenValue.setText("255");
blueValue.setText("0");
}
    
    }
    
    class applyBlue implements Button.OnClickListener {

@Override
public void onClick(View v) {
messageText.setTextColor(myResources.getColor(R.color.BLUE));
redValue.setText("0");
greenValue.setText("0");
blueValue.setText("255");
}
    
    }
}

0 comments:

Post a Comment