AQuery – Making it Easy

Most every language and platform you develop for has its own boilerplate code—code that has to be included with little or no change.  As a developer I appreciate tools that help remove boilerplate code.  This helps with readability, ease of development, and the overall amount of fun you have while developing.  One such tool for Android is Android-Query or AQuery.

AQuery is a lightweight library for doing asynchronous tasks and manipulating UI elements in Android.  AQuery is an open source project that contains a number of different ways to help out Android developers that makes doing repetative tasks easier.

As an example, below is the typical coding paradigm in Android without using AQuery:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public void renderContent(Content content, View view) {
        
        
        ImageView tbView = (ImageView) view.findViewById(R.id.icon);
        if(tbView !=null){
                
                tbView.setImageBitmap(R.drawable.icon);
                tbView.setVisibility(View.VISIBLE);
                
                tbView.setOnClickListener(new OnClickListener() {
                                
                                @Override
                                public void onClick(View v) {
                                        someMethod(v);
                                }
                        });
                
        }
        
        TextView nameView = (TextView) view.findViewById(R.id.name);   
        if(nameView !=null){
                nameView.setText(content.getPname());
        }
        
        TextView timeView = (TextView) view.findViewById(R.id.time); 
        
        if(timeView !=null){
                long now = System.currentTimeMillis();
                timeView.setText(FormatUtility.relativeTime(now, content.getCreate()));
                timeView.setVisibility(View.VISIBLE);
        }
        
        TextView descView = (TextView) view.findViewById(R.id.desc);   
        
        if(descView !=null){
                descView.setText(content.getDesc());
                descView.setVisibility(View.VISIBLE);
        }
}

Now this is the same code that does the same thing using AQuery:

1
2
3
4
5
6
7
8
9
10
11
public void renderContent(Content content, View view) {
        
        AQuery aq =new AQuery(view);
        
        aq.id(R.id.icon).image(R.drawable.icon).visible().clicked(this,"someMethod"); 
        aq.id(R.id.name).text(content.getPname());
        aq.id(R.id.time).text(FormatUtility.relativeTime(System.currentTimeMillis(), content.getCreate())).visible();
        aq.id(R.id.desc).text(content.getDesc()).visible();            
        
        
}

This really shows how much easier AQuery makes repetitive tasks.  Write less to do more and in turn, maintenance is a breeze.

As I include different aspects of AQuery in projects of my own, I plan to demo it here on the blog so stay tuned for more examples.  If you have used AQuery, what do you think?  Leave your comments below.

This entry was posted in Technology and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>