Saturday, January 24, 2015

Templates in Android Studio

I have written some utility classes for using in  Android development, such as HttpReader which takes an url downloads it and turn the contents into a string Another one is PictureHandler which takes a picture using the camera app on the phone and saves the picture to a file and will also display a thumbnail.  I had been looking for a way to conveniently add these files to new projects. HttpReader sometimes needs to be modified, for instance  if you need to set a request header for an api, so I did not want to make that class a library. I was looking at settings for Android Studio and I noticed a setting for templates. So  I looked into what they were, and it turns out you can save  a whole file as a template. The file needs to be open in the editor and then you click on Tools menu and there is an entry on the menu called Save File as template. Click on that and you will get  a dialog that creates the template. Once created you can access the template by opening the File menu and clicking on New. Since  these were Java classes, click on Java Class. This will open a New Class Dialog


As you can see HttpReader is chosen as Kind. HttpReader appears as an option in the dropdown menu. Also I added PictureHandler and it also can be chosen.

Once a file is added as a template it is available in any other Android Studio project.
So for me this solves my problem of adding utility files.