Skip to content

Instantly share code, notes, and snippets.

@akhy
Created May 19, 2014 16:32
Show Gist options
  • Save akhy/7adf6595fccfa390e780 to your computer and use it in GitHub Desktop.
Save akhy/7adf6595fccfa390e780 to your computer and use it in GitHub Desktop.
Android State XML Drawable Generator
"""
script by @akhy
generate android xml state drawables from png
"""
from sys import argv
from os import listdir
from os.path import isfile, join, splitext
suffix_off = "_off"
suffix_on = "_on"
targetdir = argv[1]
template = """<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/{{base}}{{suffix_on}}" android:state_pressed="true"/>
<item android:drawable="@drawable/{{base}}{{suffix_off}}"/>
</selector>
"""
for f in listdir(targetdir):
(name, ext) = splitext(f)
if name.endswith(suffix_off):
base = name[0:-len(suffix_off)]
filename = "%s/%s.xml" % (targetdir, base)
out = open(filename, "w")
print filename
text = template
text = text.replace('{{base}}', base)
text = text.replace('{{suffix_on}}', suffix_on)
text = text.replace('{{suffix_off}}', suffix_off)
out.write(text)
out.close()
@akhy
Copy link
Author

akhy commented May 19, 2014

sample usage: python drawablegen.py path-to-project/drawable/xhdpi
search for files with specified suffix (e.g. _off) and generate xml drawables accordingly

@virgiawan
Copy link

thx gaes..
Siph ^^b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment