Android: make background image changable -
i have android app background image. goal create 3 or 4 background images , give possibility user change background.
actually, have 4 images named bg1.png, bg2.png, bg3.png , bg4.png. have think declare string value "background " in strings.xml name of image. name of background image allways take background string , when users change background, wil change value of string background.
is idea or there easyer? how set background of layout in xml layout file "background" string's value ? shuld programatically?
thank you.
i think if want change background activity layout, can using setbackground method layout example:
activitylayout = (linearlayout)findviewbyid(r.id.tablelayout1); activitylayout.setbackgrounddrawable(getresources().getdrawable(r.id.somedrawable)) you can store background image using example sharedpreference, when launch activity read preference contains background. example when user choice background:
sharedpreferences myprefs = this.getsharedpreferences("myprefs", context.mode_private); sharedpreferences.editor prefseditor = myprefs.edit(); prefeditor.putint("backgroudresourceid", userchoice); prefeditor.commit(); and when activity start, must read resourceid sharedpreference:
sharedpreferences myprefs = this.getsharedpreferences("myprefs", context.mode_private); drawableid = myprefs.getint("backgroundresourceid", defaultvalue); yourlayout.setbackground(drawableid); where defaultvalue default value if preference not set. yourlayout supposed initialized (in same way of activity layout).
Comments
Post a Comment