Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to automatically generate Java code from layout file?

Does anyone know of any Eclipse plug-in or anything that can be used to automatically generate Java code from layout file? As in, if I have an EditText in my layout file with the ID "@+id/txtHello", I expect something like the following to be generated:

EditText txtHello = (EditText) findViewById(R.id.txtHello);

Thanks for your time!

Harris :)

like image 841
hsoetikno Avatar asked Sep 18 '11 14:09

hsoetikno


4 Answers

Use this online tool:

http://www.buzzingandroid.com/tools/android-layout-finder/

It simply gets the job done quickly. I use it every day.

like image 137
Andreas Rudolph Avatar answered Nov 11 '22 00:11

Andreas Rudolph


Normally there are three different ways to do this:

  1. at run time (via annotations per reflection)
  2. at compile time (via annotations or aspects)
  3. at development time (via code generators)

A good article to start is Clean Code in Android Applications.

Ad 1) Two solutions, see

  • RoboGuice, see http://code.google.com/p/roboguice/
  • AndJect, see https://github.com/ko5tik/andject

Ad 2) Android Annotations, see http://androidannotations.org/

Ad 3) Two solutions, see

  • the lazy-android plugin as mentioned in this thread, see
    http://marketplace.eclipse.org/content/lazy-android
  • MotoDev Studio (available as standalone and as eclipse plugin), see http://developer.motorola.com/docstools/motodevstudio/

If there is more, please tell! I personally prefer 2) and therefore Android Annotations.

Hope that helps!

like image 20
ChrLipp Avatar answered Nov 11 '22 02:11

ChrLipp


There is an Eclipse plugin that does exactly what you want , I guess: http://marketplace.eclipse.org/content/lazy-android

like image 5
Diego Torres Milano Avatar answered Nov 11 '22 01:11

Diego Torres Milano


There is new plugin that can generate Activity, Fragment, Adapter based on xml layout. Can also generate Menu related code (handling actions) for xml menu files. And has editable templates, so the user has more control on the generated data.

For Eclipse: http://tmorcinek.github.io/android-codegenerator-plugin-eclipse/

For Android Studio/IntelliJ IDEA: http://tmorcinek.github.io/android-codegenerator-plugin-intellij/

like image 1
tmorcinek Avatar answered Nov 11 '22 00:11

tmorcinek