I have a simple program which is able to change the background color after clicking a button, but it does not work
public class ChangeBackgroundActivity extends Activity {
/** Called when the activity is first created. */
Button blueButton;
LinearLayout myLO;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myLO=(LinearLayout)findViewById(R.layout.main);
blueButton=(Button)findViewById(R.id.button1);
blueButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
myLO.setBackgroundColor(0x0000FF); //blue color code #0000FF
}
});
}
}
Try With this,
main.xml
ChangeBackgroundActivity.java
You have to create an xml file(selector file) and put it in drawable folder in res(res->drawable->yourselectorfile.xml. After that set xml file in button's background in your layout file
button_background_selector.xml
Now set the above file in button's background.
and for changing color use
as your button_background_selector.xml
Use this, this worked for me:
use
insteadof
your layout must be like that