Thursday, June 12, 2014

Android AQuery tutorial

What is AQuery

Android-Query (AQuery) is a light-weight library for doing asynchronous tasks and manipulating UI elements in Android in efficient way. The main goal of AQuery library is to make Android coding simpler, easier, and more fun. It allows developer to write less code to do lot more.

Why AQuery?

AQuery has abundant of features in it and they are:
Less Code
AJAX Callback
Image Loading
XML Parsing
Chaining
Binding
Authentication
Multiple UI, One Piece of Code
Light Weight
Non-intrusive

Documentation link is  AQuery Library Documentation

 AQuery Features

Less Code
AQuery allows the developer to be more expressive in terms of writing code i.e., write-less/do-more. Simpler code is always easier to read and maintain.
Compare the length of these pieces of code that does the same thing:
Before using AQuery
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void onCreate(Bundle savedInstanceState) {
 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);             
 
     TextView nameView = (TextView) findViewById(R.id.name);     
       nameView.setText("Hello");        
 
    Button button = (Button) findViewById(R.id.button);
    button.setText("Click Me");
 
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            button.setText("Hello World");   
        }
    }            
 
}
After using AQuery
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private AQuery aq;
public void onCreate(Bundle savedInstanceState) {
 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);             
 
        aq = new AQuery(this);         
 
        aq.id(R.id.text).text("Hello");         
 
        aq.id(R.id.button).text("Click Me").clicked(this, "buttonClicked");
 
}
 
public void buttonClicked(View button){
 
        //update the text
        aq.id(R.id.text).text("Hello World");
 
}
Take a look at the simple example to understand how to Write less code to Do more.
AJAX Callback
Asynchronous AJAX or RPC calls are made simple with built in methods and it supports following types:
  • JSONObject
  • JSONArray
  • String (HTML, XML)
  • XmlDom (XML parsing)
  • XmlPullParser (Large XML files)
  • byte array
  • User defined custom type (Transformer)
  • Bitmap
Image Loading
AQuery library supports easy asynchronous image loading from network, with automatic file and memory caching. Best of it’s image loading features are:
  • Simple Loading
  • Memory & File Caching
  • Down Sampling
  • Zoomable (WebView)
  • Fallback Image
  • Preloading
  • Animation
  • Dynamic Aspect Ratio
  • Avoid Duplicated Simultaneous Fetches
  • Custom Callback
XML Parsing
AQuery library provides a light weight XML parser called XmlDom.
XmlDom is simple and can be used for doing XML parsing.
Chaining
AQuery library designed set methods in such a way that it will return itself, so it is quite easy for developers to chain set methods. Here is an example:
1
2
String name = "AQuery is a simple Android Library";
aq.id(R.id.txtname).text(name).background(R.color.red).textColor(R.color.black).enabled(true).visible();
Binding
AQuery makes binding listeners to views simple and easy. Here is an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Override
protected void onCreate(Bundle savedInstanceState){
 
        //set content view here...
 
        AQuery aq = new AQuery(this);  
 
        aq.id(R.id.button).clicked(this, "buttonClicked");
        aq.id(R.id.list).itemClicked(this, "itemClicked")
}
 
public void buttonClicked(View view){
 
        //a button is clicked
 
}
 
public void itemClicked(AdapterView<?> parent, View v, int pos, long id) {
 
        //list item is clicked
 
}
Authentication
AQuery provides an easy and simple authentication mechanism for Google and other OAuth enabled services.
Multiple UI, One Piece of Code
With the advent of new Tablet devices, Android apps will need to support a wider range of screen sizes. Developers might need to design different UI layout for phone and tablets, but do we need to write a different set of code also?
AQuery allows developers to manage different layouts with the same piece of code. If a view is not in a layout, AQuery will just ignore all the operations performed on the view.
Light Weight
Memory is scarce for mobile apps. The AQuery lib is standalone and relatively small and optimized with proguard (~60k). The actual increase in apk size will be even smaller.
Non-intrusive
AQuery has no dependency and does not require developers to inherit specialized views or activities. Developers are free to use AQuery along with any other library without any conflict.

No comments:

Post a Comment