Theme Dependent Android Strings -



Theme Dependent Android Strings -

let's have application 2 themes: masculine , feminine. themes alter out color palette , few drawables appeal user's preferred tastes.

many http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html hints @ making work.

but lets want little cuter app , not alter colors , drawables, want alter strings. instance, might want add together pirate theme , "submit" "arrrrgh!"

so, basic question is: how can alter strings throughout app via user selectable themes?

edit:

making up: app has 12 buttons , 32 text views i'd have theme dependent , i'd accomplish without giant mapping or slew of custom attrs.

all 3 of current solutions work. looking cleaner though don't know such beast exists.

let's have application 2 themes: masculine , feminine. themes alter out color palette , few drawables appeal user's preferred tastes.

how pretend you're doing else? design anti-pattern, associating particular colors based on gender (e.g., "girls pink").

this not technical objective bad, stereotypical example.

for instance, might want add together pirate theme , "submit" "arrrrgh!"

only if "cancel" maps "avast!".

how can alter strings throughout app via user selectable themes?

you have not said strings coming from. string resources? database entries? ones retrieving web service? else?

i assume moment these string resources. definition, need have n copies of strings, 1 per theme.

since gender , piratical status not things tracked android possible resource set qualifiers, can't have string resources in different resource sets. while in different files (e.g., res/values/strings_theme1.xml), filenames not part of resource identifiers strings. so, wind having utilize sort of prefix/suffix maintain track of strings belong themes (e.g., @string/btn_submit_theme1).

if these strings not changing @ runtime -- it's whatever in layout resource -- take page chris jenkins' calligraphy library. has own subclass of layoutinflater, used overload of standard xml attributes. in case, focus on android:fontfamily, supports mapping font file in assets.

in case, overload android:text. in layout file, rather pointing of actual strings, have base of operations name of desired string resource, sans theme identifier (e.g., if real strings @string/btn_submit_theme1 , kin, have android:text="btn_submit"). layoutinflater subclass grab value, append theme name suffix, utilize getidentifier() on resources actual string resource id, , there string tied theme.

a variation on set base of operations name in android:tag instead of android:text. android:text might point 1 of real string resources, help gui design , such. layoutinflater grab tag , utilize derive right string @ runtime.

if replacing text other text pulled theme-based string resources, isolate get-the-string-given-the-base-name logic static utility method somewhere apply.

while getting right take bit of work, scale arbitrary complexity, in terms of number of affected ui widgets , strings. still have remember add together values themes new strings define (bonus points creating custom lint check or gradle task validating this).

android string resources themes customization

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -