java - How to change layout background dynamically -
i having problem in code.i trying change layout background of app every second.i used thread in code.i've searched site couldn't find useful.here code.
import android.app.activity; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.bundle; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; import android.widget.linearlayout; public class mainactivity extends activity { //private bitmap open, close; private linearlayout myl; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); myl = (linearlayout) findviewbyid(r.id.linearlayout2); // myl=(linearlayout) findviewbyid(r.id.linearlayout2); //close = bitmapfactory.decoderesource(getresources(), r.drawable.kapa); //open = bitmapfactory.decoderesource(getresources(), r.drawable.ac); } @override protected void onresume() { // todo auto-generated method stub super.onresume(); thread th = new thread() { public void run() { while (true) { myl.setbackgroundresource(r.drawable.kapa); try { sleep(1000); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } myl.setbackgroundresource(r.drawable.ac); try { sleep(1000); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } } }; th.start(); } }
try this:
public class mainactivity extends activity { //private bitmap open, close; private linearlayout myl; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); myl = (linearlayout) findviewbyid(r.id.linearlayout2); // myl=(linearlayout) findviewbyid(r.id.linearlayout2); //close = bitmapfactory.decoderesource(getresources(), r.drawable.kapa); //open = bitmapfactory.decoderesource(getresources(), r.drawable.ac); } @override protected void onresume() { // todo auto-generated method stub super.onresume(); runnable runnable = new runnable() { @override public void run() { while(true) { try { thread.sleep(1000); } catch (interruptedexception e) { e.printstacktrace(); } mainactivity.this.runonuithread(new runnable() { @override public void run() { myl.setbackgroundresource(r.drawable.kapa); } }); try { thread.sleep(1000); } catch (interruptedexception e) { e.printstacktrace(); } mainactivity.this.runonuithread(new runnable() { @override public void run() { myl.setbackgroundresource(r.drawable.ac); } }); } } }; new thread(runnable).start(); } }
Comments
Post a Comment