java - how to store button colour value in shared preference and used in condition and change button colour while click the button -
in app have show event invitation if click invitation yes no maybe button displayed , if click of button colour of button should change , should hold colour forever until change response(yes no maybe). here posted code
public class invitationfragment extends fragment { layoutinflater inflater; viewgroup container; bundle savedinstancestate; private list<string> eventname = new arraylist<>(); private list<long> eventid = new arraylist<>(); private list<string> eventplace = new arraylist<>(); private list<eventmo> eventmos = new arraylist<>(); private list<long> eventuserid = new arraylist<>(); private context context; private useroccasions useroccasions; private userdelegate userdelegate = new userdelegate(); private eventdelegates eventdelegates = new eventdelegates(); private gson gson = new gson(); private progressdialog prgdialog; private usermo usermo; private checkbox invitationcheckbox; private listview occasionview; private databasehelper dbhelper; private view view; private sharedpreferences sharedpreferences; public boolean onoptionsitemselected(menuitem item) { log.d("onoptionsitemselected", "yes"); switch (item.getitemid()) { case r.id.menu_cancel_event: displayinvitationlist(); return true; default: return super.onoptionsitemselected(item); } } public void displayinvitationlist() { int itemcount = occasionview.getcheckeditemcount(); (int = 1; <= itemcount; i++) { if (useroccasions.getcheckedinvitationcount() != null) { sqlitedatabase db = dbhelper.getwritabledatabase(); // eventuserid = useroccasions.getcheckedinvitationcount(); // db.delete("user_relation", "event_user_id" + " = ?", new string[]{long.tostring(eventuserid)}); db.close(); } } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { this.inflater = inflater; this.container = container; this.savedinstancestate = savedinstancestate; super.oncreate(savedinstancestate); sethasoptionsmenu(true); final view view = inflater.inflate(r.layout.invitation_tab, container, false); context = getactivity().getapplicationcontext(); prgdialog = new progressdialog(getactivity()); eventid.clear(); eventname.clear(); eventplace.clear(); // set progress dialog text prgdialog.setmessage("please wait..."); // set cancelable false prgdialog.setcancelable(false); prgdialog.show(); databasehelper dbhelper = new databasehelper(context); usermo = dbhelper.getringeeuserdata(1); new asynctask<void, void, string>() { @override protected string doinbackground(void... arg0) { return eventdelegates.getalleventforuser(usermo, context); } @override protected void onpostexecute(string eventlists) { if (eventlists != "null") { eventmos = gson.fromjson(eventlists, new typetoken<list<eventmo>>() { }.gettype()); toast.maketext(context, "total items of eventmo" + eventmos.size(), toast.length_long).show(); (eventmo eventmo : eventmos) { eventname.add(eventmo.gettext()); eventid.add(eventmo.geteventid()); eventplace.add(eventmo.getplace()); } databasehelper dbhelper = new databasehelper(context); //long totalinsertion = dbhelper.insertuserrelationtable(usermos); prgdialog.dismiss(); //toast.maketext(context, "total usermos size " + usermos.size() + "total db insertion size " + totalinsertion, toast.length_long).show(); occasionview = (listview) view.findviewbyid(r.id.invitation_list_view); useroccasions = new useroccasions(); occasionview.setadapter(useroccasions); occasionview.setitemscanfocus(false); occasionview.settextfilterenabled(true); occasionview.setonitemclicklistener(occasionview.getonitemclicklistener()); } } }.execute(null, null, null); return view; } private class useroccasions extends baseadapter { layoutinflater minflater; textview eventnametxtv, eventplacetxtv; private arraylist<eventmo> checkedinvitationitems; private object invitationtag; useroccasions() { minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); this.checkedinvitationitems = new arraylist<eventmo>(); } @override public int getcount() { return eventmos.size(); } @override public object getitem(int position) { return position; } @override public long getitemid(int position) { return 0; } // show list values name , mobile number in contact page @override public view getview(final int position, view convertview, viewgroup parent) { if (null == convertview) { convertview = minflater.inflate(r.layout.invitation, null); } eventnametxtv = (textview) convertview.findviewbyid(r.id.invitationtitle); eventplacetxtv = (textview) convertview.findviewbyid(r.id.invitationplace); eventnametxtv.settext(eventmos.get(position).gettext()); eventplacetxtv.settext(eventmos.get(position).getplace()); // intializing variables final eventmo eventmo = eventmos.get(position); convertview.settag(position); view v = convertview.findviewbyid(r.id.invitation_single); final linearlayout eventlayout = (linearlayout) convertview.findviewbyid(r.id.invitation_single); invitationcheckbox = (checkbox) convertview.findviewbyid(r.id.invitationcheckbox); button yesbtn = (button) convertview.findviewbyid(r.id.yesbutton); button nobtn = (button) convertview.findviewbyid(r.id.nobutton); button maybebtn = (button) convertview.findviewbyid(r.id.buttonmaybe); final linearlayout responselayout = (linearlayout) convertview.findviewbyid(r.id.hidden); invitationcheckbox.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { if (ischecked) { checkedinvitationitems.add((eventmo) eventmo); } else { checkedinvitationitems.remove((eventmo) eventmo); } } }); //based on user click, response stored yesbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { responselayout.setvisibility(view.gone); //if user clicks yes button in invitation response layout,response stored 1 event user final int response = 1; final long eventid = eventmos.get(position).geteventid(); usermo.setisattending(response); new asynctask<void, void, string>() { protected string doinbackground(void... arg0) { return userdelegate.updateeventuserrelationshipmapping(usermo, eventid); } }.execute(null, null, null); } }); nobtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { responselayout.setvisibility(view.gone); //if user clicks no button in invitation response layout,response stored 0 event user final int response = 0; final long eventid = eventmos.get(position).geteventid(); usermo.setisattending(response); new asynctask<void, void, string>() { protected string doinbackground(void... arg0) { return userdelegate.updateeventuserrelationshipmapping(usermo, eventid); } }.execute(null, null, null); } }); maybebtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { responselayout.setvisibility(view.gone); //if user clicks maybe button in invitation response layout,response stored event user final int response = 2; usermo.setisattending(response); final long eventid = eventmos.get(position).geteventid(); new asynctask<void, void, string>() { protected string doinbackground(void... arg0) { return userdelegate.updateeventuserrelationshipmapping(usermo, eventid); } }.execute(null, null, null); } }); eventlayout.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { switch (v.getid()) { case r.id.invitation_single: responselayout.setvisibility(view.visible); break; } } }); return convertview; } public arraylist<eventmo> getcheckedinvitationcount() { return this.checkedinvitationitems; } } }
here have tried following code change colour of button when button clicked not hold colour when go , return button , work events
yesbtn.setbackgroundcolor(color.yellow); nobtn.setbackgroundcolor(color.blue); maybebtn.setbackgroundcolor(color.blue);
the above mentioned code under yesbtn.onclicllistener() method
here how use shared preference solve issue
you can store color in shared preference int. suggestion , need not store color , flag value 1,2,3,etc , based on value can set color on button
Comments
Post a Comment