Android apps are all the rage right now, considering how commonplace smartphones have become. At the same time, one needs to be constantly in search of novelty as far as such a fast-evolving industry is concerned. In such a scenario, it becomes important to know and adopt crucial hacks to increase the efficiency of the process of application development. One of these hacks includes gaining access to Android libraries, which can considerably help in giving a new edge to the process.
Here is a list of Android libraries every android app developers: rookie or expert should know:
JSON:
If your app uses Java, which it definitely might, then JSON is a must for you. It converts Java objects into the lighter JSON format. JSON is abbreviated as JavaScript Object Notation and is used to signify a particular type of format which is considerably more convenient to read and write. Once the data transfer is complete, the same can be utilized to de-serialize JSON back to the Java format. This will ensure a more efficient and quick transfer.
// Serialize
String userJSON = new Gson().toJson(user);
// Deserialize
User user = new Gson().fromJson(userJSON, User.class);
Retrofit:
Retrofit works to convert your REST API into Java format. This nullifies the lengthy coding required for the conversion. As far as finding hacks to make the practice of coding more accurate without compromising on time and energy, Retrofit is a fine hack. The updated version of Retrofit has annotations which make it easy to add the header, request body and query parameters, along with making changes to the URLs.
public interface RetrofitInterface {
// asynchronously with a callback
@GET(“/api/user”)
User getUser(@Query(“user_id”) int userId, Callback<User> callback);
// synchronously
@POST(“/api/user/register”)
User registerUser(@Body User user);
}
// example
RetrofitInterface retrofitInterface = new RestAdapter.Builder()
.setEndpoint(API.API_URL).build().create(RetrofitInterface.class);
// fetch user with id 2048
retrofitInterface.getUser(2048, new Callback<User>() {
@Override
public void success(User user, Response response) {
}
@Override
public void failure(RetrofitError retrofitError) {
}
});
Retrofit already uses GSON as default, so the combination of these won’t require additional parsing.
EventBus:
This library is an incredible resource to code the way in which different factions of your app interact with each other.
public class NetworkStateReceiver extends BroadcastReceiver {
// post event if there is no Internet connection
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(intent.getExtras()!=null) {
NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
// there is Internet connection
} else if(intent
.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
// no Internet connection, send network state changed
EventBus.getDefault().post(new NetworkStateChanged(false));
}
}
// event
public class NetworkStateChanged {
private mIsInternetConnected;
public NetworkStateChanged(boolean isInternetConnected) {
this.mIsInternetConnected = isInternetConnected;
}
public boolean isInternetConnected() {
return this.mIsInternetConnected;
}
}
public class HomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this); // register EventBus
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this); // unregister EventBus
}
// method that will be called when someone posts an event NetworkStateChanged
public void onEventMainThread(NetworkStateChanged event) {
if (!event.isInternetConnected()) {
Toast.makeText(this, “No Internet connection!”, Toast.LENGTH_SHORT).show();
}
}
}
Active Android:
When you are using coding hacks, the key is to code less while getting the most out of the same. ActiveAndroid is an object-relational mapping tool, which replaces the lengthy structured query language on the database:
INSERT INTO Users (Nickname, Name, Address, City, PostalCode, Country) VALUES (‘Batman’,’Bruce W’,’Palisades 21′,’Gotham’,’40000′,USA’);
To a small little piece of code like so:
user.save();
Use GreenDAO and ORMLite as an open source alternative to ActiveAndroid.
Universal Image Loader (UIL):
Pictures or images are an important addition to the app. Many developers use Picasso for adding images. While this is an amazing app, it lacks specific configurations that are necessary for it to function efficiently. UIL remedies this problem easily and in a convenient fashion.
Coding can be quite a cumbersome task and therefore gaining access to Android open source libraries makes the process easier and more proficient.
Other than the above-mentioned libraries, here are some alternative libraries that you could use:
RxJava:
RxJava is the Java version of ReactiveX API. This library can be used to modify and manipulate value streams within the functioning of the Android app by using three different patterns of coding, namely Functional Programming, the Iterator pattern, and the Observer pattern.
There are two main elements that form a part of the RxJava extension; namely Observable and Subscriber. The element known as Observable is the one through the stream of data in the functioning of the app originates from. The Subscriber is the part of the extension which makes changes to the stream of data originating from the Observable.
Glide:
Glide is an alternative to extensions such as Picasso and UIL, if neither works for you, Glide is another efficient option.
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load(“https://placekitten.com/1920/1080 “).into(imageView);
Butterknife:
Butterknife is an amazing resource that can ease your coding troubles by reducing strain that originates from writing long strings of code. This is especially useful for the onCreate method as well as the onCreateView on Activity Fragments.
class ExampleActivity extends Activity {
@Bind(R.id.title) TextView title;
@Bind(R.id.subtitle) TextView subtitle;
@Bind(R.id.footer) TextView footer;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
// TODO Use fields…
}
}
Android activities are specific components which are responsible for the creation of the UI of an application. Several activities come together to create an application. The onCreate command is used to initiate an activity and hence is the beginning point of the same.
Now that you have a list of good Android libraries and open sources for Android development, the question is how to incorporate them into your Android Studio.
Well, most of the apps can be incorporated using Maven, all you need to do is code the dependencies into the build.gradle file like shown below:
dependencies {
compile ‘com.google.code.gson:gson:2.2.4’
compile ‘com.squareup.okhttp:okhttp:1.3.0’
compile ‘com.squareup.retrofit:retrofit:1.3.0’
compile ‘de.greenrobot:eventbus:2.2.+’
compile ‘com.nostra13.universalimageloader:universal-image-loader:1.9.1’
}
Even though coding is an intricate practice, there are several ways in which the process can be made more interesting and efficient. Android libraries can be a huge contribution to the ease and efficiency of the process. Moreover, following developers and development trends on Facebook and Twitter can be a considerable help as far as creating a more proficient coding practice is concerned.