adding button to android java code -
how can add button android java code? main activity java code:
package com.example.pafima_trial; import android.os.bundle; import android.app.activity; import android.view.menu; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(new singletoucheventview(this, null)); // setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } } it singletoucheventview.java
public class singletoucheventview extends view { private paint paint = new paint(); private path path = new path(); boolean touched = false; float x =0; float y =0; float [] inputx = new float[200]; float [] inputy = new float[200]; string [] direction = new string [200]; int count =0; int dcount =0; public singletoucheventview(context context, attributeset attrs) { super(context, attrs); paint.setantialias(true); paint.setstrokewidth(6f); paint.setcolor(color.black); paint.setstyle(paint.style.stroke); paint.setstrokejoin(paint.join.round); } @override protected void ondraw(canvas canvas) { canvas.drawpath(path, paint); } @override public boolean ontouchevent(motionevent event) { float eventx = event.getx(); float eventy = event.gety(); switch (event.getaction()) { case motionevent.action_down: path.moveto(eventx, eventy); return true; case motionevent.action_move: path.lineto(eventx, eventy); touched=true; inputx[count]=eventx; inputy[count]=eventy; system.out.println("x is: "+ inputx[count]); system.out.println("y is: "+inputy[count]); if(count>=2 && count%2 != 1){ if(inputx[count-2]-inputx[count]<=-15){ if(inputy[count-2]-inputy[count]<=-15){ direction[dcount]="right down"; dcount++; } if(inputy[count-2]-inputy[count]>15){ direction[dcount]="right up"; dcount++; } if(-14<=inputy[count-2]-inputy[count] && inputy[count-2]-inputy[count]<=15 ){ direction[dcount]="right"; dcount++; } } if(inputx[count-2]-inputx[count]>15){ if(inputy[count-2]-inputy[count]>=15){ direction[dcount]="left up"; dcount++; } if(inputy[count-2]-inputy[count]<-15){ direction[dcount]="left down"; dcount++; } if(15>inputy[count-2]-inputy[count] && inputy[count-2]-inputy[count]>=-15 ){ direction[dcount]="left"; dcount++; } } if (inputx[count-2]-inputx[count]<=15 && inputx[count-2]-inputx[count]>-15){ if(inputy[count-2]-inputy[count]<-15){ direction[dcount]="down"; dcount++; } if(inputy[count-2]-inputy[count]>=15){ direction[dcount]="up"; dcount++; } } } count++; break; case motionevent.action_up: system.out.println("count "+count); break; default: return false; } int =0; while(a<dcount){ system.out.println("direction["+a+"] is: "+ direction[a]); a++; } // schedules repaint. invalidate(); return true; } } i couldn't add boutton in main activity java code, couldn't set activity main xml file content view. can give link xml file in java code?
you can add view in main activity layout using getwindow().addcontentview as:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(new singletoucheventview(this, null)); //add xml layout activity window layoutinflater inflater = getlayoutinflater(); getwindow().addcontentview(inflater.inflate(r.layout.activity_main, null), new viewgroup.layoutparams( viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent)); }
Comments
Post a Comment