java - Android:Painting in FragmentClass -
i using navigationdrawer in activity.if click of item in navigation drawer in default color should allow paint thing.but tried long time unable it.kindly hep possible.below code using.
painting.java
package com.example.painturselfnew; import android.annotation.targetapi; import android.app.activity; import android.app.fragment; import android.app.fragmentmanager; import android.content.intent; import android.content.res.configuration; import android.os.build; import android.os.bundle; import android.support.v4.app.actionbardrawertoggle; import android.support.v4.widget.drawerlayout; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listview; import android.widget.toast; @targetapi(build.version_codes.ice_cream_sandwich) public class painting extends activity { private listview mdrawerlist; private drawerlayout mdrawerlayout; private actionbardrawertoggle mdrawertoggle; private charsequence mdrawertitle; private charsequence mtitle; private string[] mfragmenttitles; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_painting); mtitle = mdrawertitle = gettitle(); mfragmenttitles = getresources().getstringarray(r.array.widgets); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.left_drawer); mdrawerlist.setadapter(new arrayadapter<string>(this, r.layout.drawer_list_item, mfragmenttitles)); getactionbar().setdisplayshowhomeenabled(true); getactionbar().setdisplayhomeasupenabled(true); mdrawerlist.setonitemclicklistener(new draweritemclicklistener()); mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.drawable.ic_launcher, r.string.drawer_open, r.string.drawer_close) { public void ondrawerclosed(view view) { getactionbar().settitle(mtitle); invalidateoptionsmenu(); } public void ondraweropened(view drawerview) { getactionbar().settitle(mdrawertitle); invalidateoptionsmenu(); } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); if (savedinstancestate == null) { selectitem(0); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.painting, menu); return true; } @override public boolean onprepareoptionsmenu(menu menu) { boolean draweropen = mdrawerlayout.isdraweropen(mdrawerlist); menu.finditem(r.id.action_settings).setvisible(!draweropen); return super.onprepareoptionsmenu(menu); } public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.action_settings: if (mdrawerlayout.isdraweropen(mdrawerlist)) { mdrawerlayout.closedrawer(mdrawerlist); } else { mdrawerlayout.opendrawer(mdrawerlist); } mdrawerlayout.opendrawer(mdrawerlist); /* * toast.maketext(getapplicationcontext(), "options selected", * toast.length_long).show(); */ return true; case android.r.id.home: startactivity(new intent(painting.this, mainactivity.class)); break; default: return super.onoptionsitemselected(item); } return true; } private class draweritemclicklistener implements listview.onitemclicklistener { public void onitemclick(adapterview<?> parent, view v, int position, long id) { selectitem(position); } } private void selectitem(int position) { fragment fragment = new fragment(); bundle args = new bundle(); // args.putint(fragment.arg_planet_number, position); fragment.setarguments(args); fragmentmanager fragmentmanager = getfragmentmanager(); switch (position) { case 0: fragment = new fragment1(); toast.maketext(getapplicationcontext(), "camera selected", toast.length_long).show(); break; case 1: fragment = new fragment2(); toast.maketext(getapplicationcontext(), "color selected", toast.length_long).show(); break; case 2: /* * paintfragment fragment1; fragment1 = new paintfragment(); */ paintfragment fragment1 = new paintfragment(); toast.maketext(getapplicationcontext(), "paint selected", toast.length_long).show(); break; case 3: fragment = new fragment4(); toast.maketext(getapplicationcontext(), "save selected", toast.length_long).show(); break; case 4: fragment = new fragment5(); toast.maketext(getapplicationcontext(), "share selected", toast.length_long).show(); break; case 5: fragment = new fragment6(); toast.maketext(getapplicationcontext(), "artby selected", toast.length_long).show(); break; case 6: fragment = new fragment7(); toast.maketext(getapplicationcontext(), "refresh selected", toast.length_long).show(); break; } fragmentmanager.begintransaction() .replace(r.id.content_frame, fragment).commit(); mdrawerlist.setitemchecked(position, true); settitle(mfragmenttitles[position]); mdrawerlayout.closedrawer(mdrawerlist); } @override public void settitle(charsequence title) { mtitle = title; getactionbar().settitle(mtitle); } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); // sync toggle state after onrestoreinstancestate has occurred. mdrawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); // pass configuration change drawer toggls mdrawertoggle.onconfigurationchanged(newconfig); } }
painitngfragment.java
package com.example.painturselfnew; import android.annotation.targetapi; import android.app.actionbar.layoutparams; import android.app.activity; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.path; import android.os.build; import android.os.bundle; import android.view.motionevent; import android.view.view; import android.widget.button; @targetapi(build.version_codes.honeycomb) public class paintfragment extends activity { /*private static int displaywidth = 0; private static int displayheight = 0; */ /* * @override public void oncreate(bundle savedinstancestate) { * super.oncreate(savedinstancestate); * //setcontentview(r.layout.activity_fragment3); //setcontentview(new * myview(this)); * * * } */ /*public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {*/ protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); brushview view=new brushview(this); setcontentview(view); addcontentview(view.btneraseall, view.params); /* * view view = inflater.inflate(r.layout.activity_fragment3, container, * false); */ // getactivity(); /* * display display = * ((windowmanager)this.getsystemservice(context.window_service * )).getdefaultdisplay(); displaywidth = display.getwidth(); * displayheight = display.getheight(); */ //imageview iv = (imageview) view.findviewbyid(r.id.imageview1); /* * iv.setonclicklistener(new view.onclicklistener() { * * @override public void onclick(view arg0) { // auto-generated * method stub * * brushview view = new brushview(getactivity()); //setcontentview(new * brushview(this)); } }); */ //return view; } @override protected void onpause() { super.onpause(); finish(); } public class brushview extends view { private paint brush = new paint(); private path path = new path(); public button btneraseall; public layoutparams params; public brushview(context paintfragment) { super(paintfragment); brush.setantialias(true); brush.setcolor(color.blue); brush.setstyle(paint.style.stroke); brush.setstrokejoin(paint.join.round); brush.setstrokewidth(15f); btneraseall = new button(paintfragment); btneraseall.settext("erase everything!!"); params = new layoutparams(layoutparams.match_parent, layoutparams.wrap_content); btneraseall.setlayoutparams(params); btneraseall.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // reset path path.reset(); // invalidate view postinvalidate(); } }); } @override public boolean ontouchevent(motionevent event) { float pointx = event.getx(); float pointy = event.gety(); // checks event occurs switch (event.getaction()) { case motionevent.action_down: path.moveto(pointx, pointy); return true; case motionevent.action_move: path.lineto(pointx, pointy); break; case motionevent.action_up: break; default: return false; } // force view draw. postinvalidate(); return false; } @override protected void ondraw(canvas canvas) { canvas.drawpath(path, brush); } } }
thanks in advance...
Comments
Post a Comment