Setting button color ...
I just finished 200 video tutorials on Android programming and started
experimenting with what I learned.
I am trying to set Button color in java and found that I can reliably
set them in the onCreate() constructor but not later in the program.
I created a fun little class of 5 buttons where each button click sets
the colors separately.
Setting the button color like this:
<Code>
b1.getBackground().setColorFilter(Color.GREEN,
Mode.MULTIPLY);
b2.getBackground().setColorFilter(Color.BLUE,
Mode.MULTIPLY);
b3.getBackground().setColorFilter(Color.RED, Mode.MULTIPLY);
b4.getBackground().setColorFilter(Color.CYAN,
Mode.MULTIPLY);
b5.getBackground().setColorFilter(Color.WHITE,
Mode.MULTIPLY);
</Code>
The buttons colors do not reliably change simply by clicking the color
change button.
But I can force it to appear by manipulating the phone like this:
After clicking on a button to change the color, I can click on a
TextView widget to get the keyboard to appear on the screen and then
press the menu back button and the colors for the buttons I set are
properly refreshed.
It appears I need a display refresh or something like VB.NET's
Application.DoEvents()
Got any ideas?
Here is the complete ButtonColor class I wrote:
<Code>
package com.example.helloandroid;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ButtonColor extends Activity implements
OnClickListener{
Button b1, b2, b3, b4, b5;
Bundle saveB;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.buttoncolor);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
b5 = (Button) findViewById(R.id.b5);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b1.getBackground().setColorFilter(Color.GREEN,
Mode.MULTIPLY);
b2.getBackground().setColorFilter(Color.BLUE,
Mode.MULTIPLY);
b3.getBackground().setColorFilter(Color.RED, Mode.MULTIPLY);
b4.getBackground().setColorFilter(Color.CYAN,
Mode.MULTIPLY);
b5.getBackground().setColorFilter(Color.WHITE,
Mode.MULTIPLY);
saveB = savedInstanceState;
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.b1:
b1.getBackground().setColorFilter(Color.GREEN,
Mode.MULTIPLY);
b2.getBackground().setColorFilter(Color.BLUE,
Mode.MULTIPLY);
b3.getBackground().setColorFilter(Color.RED,
Mode.MULTIPLY);
b4.getBackground().setColorFilter(Color.CYAN,
Mode.MULTIPLY);
b5.getBackground().setColorFilter(Color.WHITE,
Mode.MULTIPLY);
break;
case R.id.b2:
b1.getBackground().setColorFilter(Color.WHITE,
Mode.MULTIPLY);
b2.getBackground().setColorFilter(Color.CYAN,
Mode.MULTIPLY);
b3.getBackground().setColorFilter(Color.RED,
Mode.MULTIPLY);
b4.getBackground().setColorFilter(Color.BLUE,
Mode.MULTIPLY);
b5.getBackground().setColorFilter(Color.GREEN,
Mode.MULTIPLY);
break;
case R.id.b3:
b1.getBackground().setColorFilter(Color.GREEN,
Mode.MULTIPLY);
b2.getBackground().setColorFilter(Color.BLUE,
Mode.MULTIPLY);
b3.getBackground().setColorFilter(Color.GREEN,
Mode.MULTIPLY);
b4.getBackground().setColorFilter(Color.BLUE,
Mode.MULTIPLY);
b5.getBackground().setColorFilter(Color.GREEN,
Mode.MULTIPLY);
break;
case R.id.b4:
b1.getBackground().setColorFilter(Color.RED,
Mode.MULTIPLY);
b2.getBackground().setColorFilter(Color.WHITE,
Mode.MULTIPLY);
b3.getBackground().setColorFilter(Color.BLUE,
Mode.MULTIPLY);
b4.getBackground().setColorFilter(Color.RED,
Mode.MULTIPLY);
b5.getBackground().setColorFilter(Color.WHITE,
Mode.MULTIPLY);
break;
case R.id.b5:
b1.getBackground().setColorFilter(Color.GREEN,
Mode.MULTIPLY);
b2.getBackground().setColorFilter(Color.BLUE,
Mode.MULTIPLY);
b3.getBackground().setColorFilter(Color.GREEN,
Mode.MULTIPLY);
b4.getBackground().setColorFilter(Color.BLUE,
Mode.MULTIPLY);
b5.getBackground().setColorFilter(Color.RED,
Mode.MULTIPLY);
break;
default:
break;
}
}
}
</Code>
Carl
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en