I have an activity with a layout and some buttons in it, I want to change color of those buttons. But there are lot more buttons so I want to change them using a method defined in other class. Is this possible? I tried by creating a new class which extends the current class. But it is getting force closed.
public class Color_change extends Calculate
{
public void test()
{
Button button = (Button)findViewById(R.id.one);
button.setTextColor(0xFFFF0000);
}
}
Calculate is a main class which extends Activity. And I’m calling as below:
Color_change a = new Color_change();
a.test();
If the current Activity being displayed on the screen isn’t Color_change the code you wrote will not function. If you want to have a separate class that changes the color of a set of buttons, I would suggest passing the buttons in some form of collection to the class that changes the button color. You could write a class like this:
Then you would just need to make a list of all the buttons you need to pass to your color changer.
Just be aware, that theming and styling in Android allows you to accomplish a great deal of these kinds of effects. I would be sure you exhausted your efforts there before turning to a code solution.