Profile Pic

Iqbal Ahmed

I develop Android Apps along with Backend
and My only LOVE is Kotlin

1 minute read

We had the requirement to use icomoon.io font file. but we had the concerned when we update font file, its changed icon hexcode. and we wanted to use those hexcode everywhere in our project.

We have written mapping strings xml file in which we were using demo.html file and using icon name as our key and hexcode as our value, but when we update icon name, icon name remain same but hexcode change. so We have written this script to generate icons.xml file. so We don’t have to map each icon hexcode manually.

demo.html file generate when we select icon and press on Generate Font. Please take a look here

Install this dependency to run python script

pip3 install bs4 lxml

Python Script icons.py

import sys
from bs4 import BeautifulSoup as bs

separator = "="
file = open("icons.xml",'w')
file.write('<?xml version="1.0" encoding="utf-8"?>\n')
file.write('<resources>\n')

with open(sys.argv[1],'r') as f:
	contents = f.read()
	soup = bs(contents, 'lxml')
	ls = soup.find_all("div",{"class":"glyph fs1"})
	for en in ls:
		iconName = en.find("span",{"class":"mls"}).text.strip().replace("-","_")
		iconCode = en.find("input",{"class":"unit size1of2"})["value"].strip()
		file.write(f"""\t<string name="{iconName}" translatable="false">\\u{iconCode}</string>\n""")
file.write('</resources>')
file.close

To Run

python3 icons.py demo.html

Happy Coding!!

Say something

Comments

Nothing yet.

Recent posts

Categories

About

I develop Android Apps along with Backend
and My only LOVE is Kotlin