android - SwitchPreference and Switch -
i have custom switch control class myswitch
public class myswitch extends relativelayout implements compoundbutton.oncheckedchangelistener { //private final context context; private string swkey; private integer swicoon, swicooff; switch mysw; imageview swico; sharedpreferences preferences; public myswitch(context context) { super(context, null); } public myswitch(context context, attributeset attrs) { this(context, attrs, r.layout.my_switch); } public myswitch(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); preferences = preferencemanager.getdefaultsharedpreferences(context); layoutinflater.from(context).inflate(defstyleattr, this, true); //view.inflate(context, defstyleattr, this); swico = (imageview) findviewbyid(r.id.swicon); textview mytext = (textview) findviewbyid(r.id.swtitle); textview mysummary = (textview) findviewbyid(r.id.swsummary); mysw = (switch) findviewbyid(r.id.mysw); typedarray ta = context.obtainstyledattributes(attrs, r.styleable.myswitch, 0, 0); try { mytext.settext(ta.getstring(r.styleable.myswitch_android_title)); mysummary.settext(ta.getstring(r.styleable.myswitch_android_summary)); mysw.settexton(ta.getstring(r.styleable.myswitch_android_switchtexton)); mysw.settextoff(ta.getstring(r.styleable.myswitch_android_switchtextoff)); mysw.setoncheckedchangelistener(this); swicoon = ta.getresourceid(r.styleable.myswitch_icoon, 0); swicooff = ta.getresourceid(r.styleable.myswitch_icooff, 0); swkey = ta.getstring(r.styleable.myswitch_android_key); update(); } { ta.recycle(); } } @override public void oncheckedchanged(compoundbutton compoundbutton, boolean b) { update(b); sharedpreferences.editor editor = preferences.edit(); editor.putboolean(swkey, b); editor.commit(); } public void update(boolean val) { mysw.setchecked(val); if(val) swico.setimageresource(swicoon); else swico.setimageresource(swicooff); } public void update() { update(preferences.getboolean(swkey, false)); }
}
i want reuse code class in switchpreference because code identical, class is
public class myswitchpref extends switchpreference{ myswitch obj; private sharedpreferences preferences; public myswitchpref(context context, attributeset attrs) { super(context, attrs); //setlayoutresource(r.layout.my_switch); obj = new myswitch(context,attrs); } @override protected void onbindview(view view) { super.onbindview(view); obj.update(); }
}
the issue on switchpreference icon not shown. how can icon show in switchpreference?
Comments
Post a Comment