📜 ⬆️ ⬇️

Several useful templates for Android development under eclipse

Introduction


Hello colleagues.

Today I want to share with you some useful templates for the IDE Eclipse, which will help you to speed up some routine operations when developing for Android. I’m still using Eclipse for development, but I’m sure that Idea will allow you to create completely similar templates.

What are templates? These are pre-prepared pieces of code that IDE can quickly substitute for you by pressing Ctrl + Space. For example, type "syso" in eclipse, press Ctrl + Space. Wham, you have System.out.println (), or "fore" - you will have a ready-made template for a for each loop. Moreover, these pieces are parameterized, and the IDE will prompt you to enter names for the desired variables.
')
If you're interested, let's get started.

Templates


To access the templates, use the Window-Preferences-Java-Templates menu. There you will see a list of ready-made templates, as well as be able to create your own.

Let's see what templates I created for myself.



In the names of my templates, I made a general prefix “at_”, but you can replace it with something that suits you.

Handler (at_handler)

${:import (android.os.Handler, android.os.Message, java.lang.ref.WeakReference)} private static final class ${handler_name} extends Handler { private WeakReference<${enclosing_type}> ${outer}Ref; public ${handler_name}(${enclosing_type} ${outer}) { ${outer}Ref = new WeakReference<${enclosing_type}>(${outer}); } public void handleMessage(Message msg) { } } 

This simple template will allow you to instantly create a blank for a static Handler with a reverse link to the external class. Let me remind you that creating a handler as an internal class that is not static is not recommended. This can lead to a leakage of the Activity, and will summon Lint's fierce hatred to your soul.

By slightly modifying, we get a template for AsyncQueryHandler (at_queryhandler).
 ${:import (android.os.AsyncQueryHandler, android.content.ContentResolver, java.lang.ref.WeakReference)} private static final class ${handler_name} extends AsyncQueryHandler { private WeakReference<${enclosing_type}> ${outer}Ref; public ${handler_name}(ContentResolver cr, ${enclosing_type} ${outer}) { super(cr); ${outer}Ref = new WeakReference<${enclosing_type}>(${outer}); } } 


Parcelable (at_parcelable)

Creating a Parcelable, as you know, requires performing the same type of template actions. What if we automate them?

 ${:import (android.os.Parcel, android.os.Parcelable)} public static class ${parcelable_class} implements Parcelable { public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { // TODO: serialize your properties here } public static final Parcelable.Creator<${parcelable_class}> CREATOR = new Parcelable.Creator<${parcelable_class}>() { public ${parcelable_class} createFromParcel(Parcel in) { return new ${parcelable_class}(in); } public ${parcelable_class}[] newArray(int size) { return new ${parcelable_class}[size]; } }; private ${parcelable_class}(Parcel in) { // TODO: deserialize your properties here } } 


This template will allow us to create a full-fledged Parcelable class, while the following (at_creator)
 ${:import (android.os.Parcel, android.os.Parcelable)} public int describeContents() { return 0; } public void writeToParcel(Parcel out, int flags) { // TODO: serialize your properties here } public static final Parcelable.Creator<${enclosing_type}> CREATOR = new Parcelable.Creator<${enclosing_type}>() { public ${enclosing_type} createFromParcel(Parcel in) { return new ${enclosing_type}(in); } public ${enclosing_type}[] newArray(int size) { return new ${enclosing_type}[size]; } }; private ${enclosing_type}(Parcel in) { // TODO: deserialize your properties here } 


allow us to quickly add a CREATOR to an already existing class.

isEmpty (at_nisempty)

 ${:import (android.text.TextUtils)}!TextUtils.isEmpty(${cursor}) 

A very simple template that allows you to quickly insert a test for empty lines.

getView (at_getview)

Below is a typical creation of getView in the adapter with the addition of a ViewHolder. This particular pattern I learned from Alexandre Thomas (the author of Android Kickstartr and the Contributor AndroidAnnotations) at DevConf 2013.

 @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { v = LayoutInflater.from(context).inflate(${layout_name}, null); ViewHolder h = new ViewHolder(); v.setTag(h); } ViewHolder holder = (ViewHolder) v.getTag(); return v; } static class ViewHolder { // ViewHolder fields } 


Conclusion


I hope someone will help these templates to make development a bit more pleasant. For my templates, I started a special githab repository , which I will try to replenish with new templates as they become available. The templates.xml file is simply imported into eclipse.

Thanks for attention.

Source: https://habr.com/ru/post/183502/


All Articles