Skip to content

Instantly share code, notes, and snippets.

@nikreiman
Created July 6, 2011 12:05
Show Gist options
  • Save nikreiman/1067076 to your computer and use it in GitHub Desktop.
Save nikreiman/1067076 to your computer and use it in GitHub Desktop.
Generate XML state resources for all graphics in an Android project
#!/bin/sh
# Script to generate XML state files for button images in a project
# This script assumes that you have named your graphics like: button_normal.png, button_pressed.png
function help() {
printf "Usage: %s [Android Sources]\n" $0
exit 1
}
androidSourceRoot=$1
[ -z "$androidSourceRoot" ] && help
function generateStateList() {
graphic=$1
graphicBaseName=`basename ${graphic}`
graphicBaseName=${graphicBaseName%%_pressed*}
graphicNormalName="${graphicBaseName}_normal"
graphicPressedName="${graphicBaseName}_pressed"
stateListFilename="$androidSourceRoot/res/drawable/${graphicBaseName}.xml"
printf "Generating '%s'\n" $stateListFilename
printf -v fileOutput "%s\n" "<?xml version=\"1.0\" encoding=\"utf-8\"?>" \
"<selector xmlns:android=\"http://schemas.android.com/apk/res/android\">" \
" <item android:drawable=\"@drawable/${graphicNormalName}\" android:state_pressed=\"false\"/>" \
" <item android:drawable=\"@drawable/${graphicPressedName}\" android:state_pressed=\"true\"/>" \
"</selector>"
printf "%s" "$fileOutput" > "$stateListFilename"
}
for graphic in $androidSourceRoot/res/drawable/*_pressed.png
do
generateStateList $graphic
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment