Each Android project should use the Android library

Source: Internet
Author: User

Http://blog.teamtreehouse.com/android-libraries-use-every-project

A good developer knows to never reinvent the wheel ("Unless you plan on learning more about wheels"). With it roots in Linux and the open source community, Android have a vibrant and strong ecosystem of open source libraries That's developers can freely use in their own apps. While some is for very specific uses the may never need, others is so helpful and, well, simply delightful That's should never start an app without them.

It may sound like a lot of work to use an open source library in a project, but with Android Studio, it ' s easier than ever . Often It takes one line of code in your app's Build.gradle file to include a library automatically. Gradle is the new build automation tool in Android Studio, and it'll automatically download and include libraries if the Y is set up for it. It's so easy and popular that many libraries has this built in and include instructions on how to do it.

Butterknife

On to my favorite libraries! Let's start with a test:which of these does you like better?

Protected TextView mwelcomelabel;protected EditText musernamefield;protected EditText mpasswordfield;protected Button Msubmitbutton; @Overrideprotected void OnCreate (Bundle savedinstancestate) {  super.oncreate (savedinstancestate) ;  Setcontentview (r.layout.activity_main);  Mwelcomelabel = (TextView) Findviewbyid (R.id.welcomelabel);  Musernamefield = (EditText) Findviewbyid (R.id.usernamefield);  Mpasswordfield = (EditText) Findviewbyid (R.id.passwordfield);  Msubmitbutton = (Button) Findviewbyid (R.id.submitbutton);}

Or

@InjectView (R.id.welcomelabel) protected TextView Mwelcomelabel; @InjectView (R.id.usernamefield) protected EditText Musernamefield, @InjectView (r.id.passwordfield) protected EditText Mpasswordfield; @InjectView (R.id.submitbutton) protected Button Msubmitbutton; @Overrideprotected void OnCreate (Bundle savedinstancestate) {  super.oncreate ( Savedinstancestate);  Setcontentview (r.layout.activity_my);  Butterknife.inject (this);}

The latter code is more concise and understandable, don ' t you think? The second block of code is using a library called Butterknife, which uses annotations to "inject" views by creating Boile Rplate code for you. Butterknife is small, simple, and lightweight, and because it makes your life as a developer easier, you should pretty MUC h Always use it. It would is great if the Android SDK itself could improve in this manner!

There is additional attributes you can use for make onclicklisteners and other common, verbose aspects of Android DEVELOPM Ent easier to write and understand.

Below is a need to include this library automatically in your Android Studio projects. Just Add this one line to your app ' s build.gradle file (in app/src):

Compile ' com.jakewharton:butterknife:5.1.2 '

You need to add it dependencies in the sections, like this:

dependencies {  Compile filetree (dir: ' Libs ', include: [' *.jar '])  compile ' com.jakewharton:butterknife:5.1.2 ' }
Picasso

Need this for every app, and if you is downloading images from the Web and then your should use Picasso. There is a few popular libraries that does this kind of work, but Picasso is my favorite because it's simple and easy and I really the API is written. Code should always being this intuitive and pleasurable to use!

If you have never downloaded a image from the web on Android, then perhaps you don ' t know why this is helpful. Here is the steps you need-to-download an image with only the standard Android APIs:

    1. Get Image URL
    2. Create asynctask to download image
    3. Execute Asynctask
    4. Store result in Bitmap
    5. Set Bitmap as Source
    6. Cache image for the future

That's a lot of work! doesn ' t it seem like you should just is able to provide the URL to a ImageView and magically have it appear? Check out the steps if your use Picasso:

    1. Get Image URL
    2. Load it into a ImageView with one line:
Picasso.with (this). Load (IMAGEURL). into (Mimageview);

This page says, "With this context, the load this image URL to this ImageView." Not only was it short and sweet, but it also takes care of those other steps mentioned above behind the scenes. It's an asynchronous download and the image are automatically cached for future use. It also have additional features that make it helpful for debugging and other work.

Once again, adding it to your project are super easy in Android Studio. Just Add this line to your sections dependencies (like Butterknife above):

Compile ' com.squareup.picasso:picasso:2.3.3 '

If you want to see the this in action, I cover using it on Build a self-destructing Message App and implementing Designs for a Ndroid.

Downloading things other than images from the Web? Check out android-async-http!

Animations

The Material Design guidelines focus heavily on animation, and if your use of it correctly, you can really make your app look Polished and make your interactions more intuitive and enjoyable. Animations can is hard, but good libraries make them easy!

For a set of regular View animations, check out androidviewanimations. I ' ll let the author's animated GIF speak for itself:

The syntax for this library are similar to what we saw for Picasso above:

Yoyo.with (techniques.bounce)    duration.    Playon (Findviewbyid (R.id.usernamefield));

Including it is simple, though it does require the other projects as well:

dependencies {  compile ' com.nineoldandroids:library:2.4.0 '  compile ' com.daimajia.easing:library:[email Protected] '  compile ' com.daimajia.androidanimations:library:[email protected] '}

Another really helpful animations library is listviewanimations, which makes animating items in a list very easy. Check out the demo video on Google Play to see examples of everything it can do.

How to Discover Libraries

If you were new to the open source community, you may be wondering how can I find libraries like this on your own. It ' s really just a mindset. Every time you go to add new code, ask, "Have somebody do this already?"

    • Ask Google
    • Ask GitHub
    • Ask StackOverflow
    • Ask a Friend

Then, once you find a library of that looks interesting, remember the most important both letters for researching open source Libraries: vs.

Whenever I ' m researching a library, I Google the library ' s name followed by "vs". Then in AutoComplete I see competing libraries the one I ' m researching have been compared to. That helps me evaluate similar libraries and choose the best one for me. Check out This example of Butterknife:

Roboguice, Androidannotations, and Dagger is all libraries that does similar injection things like we saw Butterknife does For us.

Lastly, you can utilize social media and newsletters to keep a eye on the open source landscape. I passively monitor the following and bookmark any libraries that look useful.

    • Twitter (my "Android" list)
    • Google +
    • Androidweekly.net Newsletter
    • http://android-arsenal.com/
    • http://www.appdevwiki.com/

Remember, one of the main goals of programming is to was efficient and complete. Learning the work of others to augment your own are a valuable skill even the most seasoned professionals rely O N.

Each Android project should use the Android library

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.