Android: Compelling reason to use Android Unit tests

Standard

About two years ago, in one of my senior classes, we were talking about unit tests and there was a classmate of mine who got into a great debate with the professor on the why we even needed them. During that time, I sided with my classmate, because I couldn’t see the point of it either and it seemed like a waste of effort to write a whole unit test to do this:

int sum  = 1 + 1
assertEquals(2, sum);

Looking back, the above test is certainly a lackluster example and I wished the professor had given us a more persuasive reason to use them.

Recently, I was working with a database and to test this database (e.g. making sure inserting a row inserted the values I expected) I was going to insert a row into my database, query the information, and spit out a log statement containing the values to confirm if they were correct. Afterwards, I would have to delete the inserted row to perform other tests. Sounds tedious right? Also testing like this is unreliable and also clutters up the production code makes everything really disorganized.

I did some research and found out that Android unit tests can help with that! It will create a new database instance, run the tests that you need and delete the database AUTOMATICALLY afterwards. So every test you run will have a new fresh database to use.

public void testQuery() {
   Cursor cursor = null;
   try {
      ContentValues values = new ContentValues();
      values.put(PersonEntry.COLUMN_FIRST_NAME, "Ellie");
      values.put(PersonEntry.COLUMN_LAST_NAME, "Van-Housen");

      // Insert mock data
      getMockContentResolver().insert(mUri, values);

      // Query the inserted data
      cursor = getMockContentResolver().query(
                  mUri, 
                  null, 
                  null, 
                  null, 
                  null);

     if(cursor == null){
         fail("Cursor was null");

     } else{
         if(cursor.moveToFirst()){
             assertEquals("Ellie", cursor.getString(0));
             assertEquals("Van-Housen", cursor.getString(1));
         }
    }finally{
       if(cursor != null){
          cursor.close;
       }
    }
}

Doing this, I would get a green light if tests were successful and a red light if test failed. The best part about this is that I can leave the tests here for other developers/coworkers to test themselves. I hope this will certainly convince you more than the “1 + 1” example.

Android: Some Cool and Useful Implicit Intents For Your App

Standard

I was just playing around with some INTENTS and wanted to share some useful things they can do. Note: These are what these intents do on my own personal phone.


Intent i;
//Opens an app for you to send feedback to the developer(Google Play)
i = new Intent(Intent.ACTION_APP_ERROR);

//Opens the "OK Google" assistance for you. Speak into mic.
i = new Intent(Intent.ACTION_ASSIST);

//Opens an app to send a bug report to Google.
i = new Intent(Intent.ACTION_BUG_REPORT);

//Opens Phone dialer
i = new Intent(Intent.ACTION_DIAL);

//Opens Camera
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

//Opens Google drive to select a file
i = new Intent(Intent.ACTION_PICK);

//Opens app to select image (Gallery, Picaso, etc.)
//Change "Images" in URI for Audio, Video, Files, MediaColumns
//e.g. android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
i = new Intent(Intent.ACTION_PICK, 
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

//Opens app to send email
i = new Intent(Intent.ACTION_SEND);

//Opens up an operation to set your wallpaper for home screen
i = new Intent(Intent.ACTION_SET_WALLPAPER);

//Opens up SVoice, Dialer Voice Command
i = new Intent(Intent.ACTION_VOICE_COMMAND);

startActivityForResult(Intent
.createChooser(i, "Choose an application"), REQUEST_CODE);

There are a lot more Intents to be learned, but maybe this will give you insight on what you can do with your app.

Sorting Algorithms Live Comparison

Standard

I was browsing internet some time ago and saved this gif comparison of major sorting algorithms against each other and it really gives you a good grasp on their speed.

sorting speed comparison

Here are some stats that I picked out from looking at this:

Fastest Slowest
Random Heap Selection
Nearly Sorted Insertion Selection
Reversed Shell Selection
Few Unique Quick3 Selection
Best All Around: Shell
Worst All Around: Selection

I think “Few Unique” is in random order, but I am not sure. Anyways, I thought this was interesting and is definitely easier than memorizing Big(O) notation. Let me know if I made a mistake.

Android: Customizing SmoothScroller for the RecyclerView

Standard

First, this is not a full comprehensive in-depth guide for the SmoothScroller, but I hope this gives you a jumpstart on where you can begin customizing because I could not find any clear tutorials on this topic.

What is a SmoothScroller you ask? A SmoothScroller is exactly as it seems. It’s a Class that helps a View scroll to an item’s position smoothly to create a seamless transition as opposed to snapping to it abruptly. In this example I will be using its child Class LinearSmoothScroller with the RecyclerView.

We all know that to initialize a RecylerView we need to set a LayoutManager like so:

mRecyclerView.setLayoutManager(mLinearLayoutManager);

or else an error will occur during run-time. Well, the LayoutManager is in charge of the smooth scrolling! Therefore, in order to customize the SmoothScroller we need to create our own custom LayoutManager first.
Continue reading

Android Screen Resolutions and Their Smallest Width in DP

Standard

Edit: Here a more accurate list provided by Google:  https://design.google.com/devices/

When I was developing with the Genymotion emulator, I had trouble getting a sense of how big the actual devices actually were when choosing resolution size for debugging. So I did a little test to see what their smallest width was in dp for each resolution.

A dp is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi.

Resolution and DP

Now if I want to test out a resolution, I can just look at this chart and see it’s physical size.

I’ve also added annotations for which devices are small, not long, and long, because I feel those devices are the gotchas and need their own dimens.xml file to make them look good.