how to change imagview margins dynamically based on button click in android -
hi trying change imageview postion based on button click event,i tried using below code,if click button1 ic_lancher image showing 1 postion,and again click button2 here changed left margin, image not moving,but in button2 on click if gave different background means time moving perfeclty,if give same background in both button on clicks ,just changing postion of image means not working good,first image displayed if click buttons image not moving,its present in same postion... 1 suggest me how solve this...
public class mainactivity extends activity { button b1,b2; imageview arrow; float screenheight,screenwidth,screendensity; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen); displaymetrics displaymetrics = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(displaymetrics); screenheight = displaymetrics.heightpixels; screenwidth = displaymetrics.widthpixels; setcontentview(r.layout.activity_main); b1=(button)findviewbyid(r.id.button1); b1.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { // todo auto-generated method stub arrow=(imageview)findviewbyid(r.id.imageview1); arrow.setbackgroundresource(r.drawable.ic_launcher); arrow.setvisibility(view.visible); relativelayout.layoutparams layoutarrow = (relativelayout.layoutparams)arrow.getlayoutparams(); layoutarrow.leftmargin= (int)(120*(screenwidth/1024)); layoutarrow.bottommargin= (int)(235*(screenheight/600)); layoutarrow.width= (int)(50*(screenwidth/1024)); layoutarrow.height= (int)(20*(screenheight/600)); } }); b2=(button)findviewbyid(r.id.button2); b2.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { // todo auto-generated method stub arrow=(imageview)findviewbyid(r.id.imageview1); arrow.setbackgroundresource(r.drawable.ic_launcher); arrow.setvisibility(view.visible); relativelayout.layoutparams layoutarrow = (relativelayout.layoutparams)arrow.getlayoutparams(); layoutarrow.leftmargin= (int)(500*(screenwidth/1024)); layoutarrow.bottommargin= (int)(235*(screenheight/600)); layoutarrow.width= (int)(50*(screenwidth/1024)); layoutarrow.height= (int)(20*(screenheight/600)); } }); } }
you need add lines below code in both onclick
method.
arrow.setlayoutparams(layoutarrow);
this like
public void onclick(view arg0) { // todo auto-generated method stub arrow=(imageview)findviewbyid(r.id.imageview1); arrow.setbackgroundresource(r.drawable.ic_launcher); arrow.setvisibility(view.visible); relativelayout.layoutparams layoutarrow = (relativelayout.layoutparams)arrow.getlayoutparams(); layoutarrow.leftmargin= (int)(120*(screenwidth/1024)); layoutarrow.bottommargin= (int)(235*(screenheight/600)); layoutarrow.width= (int)(50*(screenwidth/1024)); layoutarrow.height= (int)(20*(screenheight/600)); arrow.setlayoutparams(layoutarrow); }
Comments
Post a Comment