Changing background color of the layout on a button click in Android -
i newbie in android. please spare me if asking stupid question.
my application contains 1 button in linear layout. requirement is, have change background color of linear layout of app on button click. default white, when press on button should change random color , when press button again, should change default color (white) again.
button.setbackgroundcolor(color.blue)
(on onclick() method), changes background color blue, how default color?
i hope code you! take boolean variable when button clicked.
public class mainactivity extends activity { boolean iscolor = true; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button btn = (button) findviewbyid(r.id.btn); final linearlayout layout = (linearlayout) findviewbyid(r.id.linearlayout1); btn.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { if(iscolor) { layout.setbackgroundcolor(color.blue); iscolor = false; } else { layout.setbackgroundcolor(color.white); iscolor = true; } } }); }
Comments
Post a Comment