Android Save chosen item from Context Menu -
hi struggling witch menu context after chose item setting new background color how save it??? idea save doesn't work :(
public class mainactivity extends appcompatactivity {
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button btn2 = (button)findviewbyid(r.id.btn2); linearlayout mainscreen = (linearlayout) findviewbyid(r.id.mainscreen); this.registerforcontextmenu(btn2); sharedpreferences settings = getsharedpreferences("mypref", 0); if(settings!=null){ //do nothing }else if(settings.getstring("color", "red").equals("red")){ mainscreen.setbackgroundcolor(color.red); }else if(settings.getstring("color", "blue").equals("blue")){ mainscreen.setbackgroundcolor(color.blue); }else if(settings.getstring("color", "green").equals("green")){ mainscreen.setbackgroundcolor(color.green); }else if(settings.getint("color", 1)==1){ mainscreen.setbackgroundresource(r.drawable.tlodelikatne); }else if(settings.getint("color", 2)==2){ mainscreen.setbackgroundresource(r.drawable.tlodelikatne); } } public void onhelp(view v) { opencontextmenu(v); } @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenu.contextmenuinfo menuinfo){ if(v.getid()==r.id.btn2){ this.getmenuinflater().inflate(r.menu.settings_menu, menu); } super.oncreatecontextmenu(menu, v ,menuinfo); } @override public boolean oncontextitemselected(menuitem item) { int selecteditemid = item.getitemid(); linearlayout mainscreen = (linearlayout) findviewbyid(r.id.mainscreen); sharedpreferences settings = getsharedpreferences("mypref", 0); sharedpreferences.editor editor = settings.edit(); editor.commit(); switch (selecteditemid) { case r.id.minired: mainscreen.setbackgroundcolor(color.red); editor.putstring("color", "red"); break; case r.id.miniblue: mainscreen.setbackgroundcolor(color.blue); editor.putstring("color", "blue"); break; case r.id.minigreen: mainscreen.setbackgroundcolor(color.green); editor.putstring("color","green"); break; case r.id.light: mainscreen.setbackgroundresource(r.drawable.backgroundone); editor.putint("color",1); break; case r.id.blue: mainscreen.setbackgroundresource(r.drawable.backgroundtwo); editor.putint("color",2); break; } editor.commit(); return super.oncontextitemselected(item); }
}
Comments
Post a Comment