Showing posts from October, 2017

Event Bus using Green robot and Google guava

Event bus is a concept to pass events between components without knowing to whom the event is sending and who is going to receive the event. Components just push events to event bus and subscribers subscribe to the events will receive the event. Event enables decoupling of components. In android communication between components is cumbersome. This concept, event bus tries to solve the problem.

Green robot' event bus library is a latest in android which has a neat implementation of event bus. Here I am trying to explain the concept of event bus through a simple example. I have a receiver which will listens to events from Power manager. When ever device is charging or discharging, receiver component will push events to event bus. An activity which is subscribed to the event will receive the events and displayed in the screen.

The same example I tried with both libraries green robot and Google Guava.

You can find the green robot sample code in Git hub

The example for Guava imple…

Firebase cloud messaging - Step by step procedure

Implement FCM cloud messaging in Android app through Android studio is now simple. The step by step procedure is,

1. Create a new project in android studio

2. Click on tab Tools -> Firebase

3. Firebase assistant wiil open on right hand side of studio

4. A new dialog box appears to connect to firebase (This step creates a new project in the same name, here FCMMessaging in firebase console)

5. Then add FCM to app, through firebase assisstant, this step will add dependencies that is required for the app.

6. Now Create a service which should extend FirebaseInstanceIdService, which should override the method onTokenRefresh(), code snippet is as follows @Override public void onTokenRefresh() { super.onTokenRefresh(); // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.e("FirebaseInstanceID", "Refresh token: " + refreshedToken); }

7. Now create another service which should extend FirebaseMessagingSe…

Espresso test to check the max length in an editText

Espresso is a testing framework in android, which is used to test the UI elements in android like editText, textView, button, recyclerView and so on. One thing to note here is that Espresso synchronises all UI events. In case if we want to check network operations, we have to use IdlingResource, which I will explain in my upcoming tutorials.

Here I am verifying the mobile number (editText) can enter max 13 characters. I created a custom matcher, checkMaxLength which will check the max length of editText is 13 characters. We can pass any integer value as we wish. import android.os.Build; import android.os.SystemClock; import; import; import; import android.text.InputFilter; import android.view.View; import android.widget.TextView; import com.smartandroidians.myloci.activities.LoginActivity; import org.hamcrest.Description; import org.hamcrest.TypeSafeMatcher…

Utility method to sort HashMap

A Simple utility method to sort values or keys in a hashMap. Here in the example, I am sorting by values, we can also sort by keys. For that in Compartor, user getkey() instead of getValue() method. Here in the method parameter, boolean value order true means ASC and false means DESC

private static Map sortByComparator(Map unsortMap, final boolean order) { List<Map.Entry<String, Integer>> list = new LinkedList<>(unsortMap.entrySet()); // Sorting the list based on values Collections.sort(list, new Comparator(Map .entry<String, Integer> o1, Map.Entry<String, Integer> o2) { public int compare(Map.Entry o1, Map.Entry o2) { if (order) { return o1.getValue().compareTo(o2.getValue()); } else { return o2.getValue().compareTo(o1.getValue()); } } }); // Maintaining insertion order with the help of LinkedList Map sortedMa…

How to check valid Email address

The code snippet to check whether the entered email address is valid is as follows,

Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ if (emailPattern.matcher(enteredEmailId).matches()) { // do your stuff }
Happy Coding
Team Androidians