Android Widget Variable Generator
Declaring variables for Android widgets is very repetitive. This Ruby script generates the code that can be copy-pasted into onCreate. Use Android Studio (Cmd+Option+F) to refactor the variables into member variables.
#androidLayoutToVars.rb
require 'nokogiri'
class LayoutProcessor < Nokogiri::XML::SAX::Document
def start_document
end
def start_element(name, attrs=[])
attrs = attrs.to_h
if attrs.key?('android:id')
varName = attrs['android:id'][5..]
declaration = "#{name} #{varName} = findViewById(R.id.#{varName});"
puts declaration
end
end
def end_element(name)
end
def end_document
end
end
parser = Nokogiri::XML::SAX::Parser.new(LayoutProcessor.new)
parser.parse(File.open(ARGV[0]))