Refactoring and improving my Android app – Introduction

At the start of 2016 I discovered h3h3productions and have been forever a fan of both Ethan and Hila Klein. Almost every video would spawn a new meme and the h3h3 subreddit would run wild with fantastic content from fans around the world. So I assumed that an Android soundboard app would be available when I went looking for one; alas there was only one, and it was not kept up to date with the dank memes. I headed down the merry path of developing an android app and publishing it to the Play Store…

An idea was born

I hacked together an app in probably a day that shows a list of clickable images from the YouTube channel of both h3h3 and the source videos that would play the corresponding sound-clip. It’s a convenient app that lets people play their favorite h3h3 meme without needing to watch through the entire video. Now that it’s gone on, it’s time to refactor the app to make it easier to maintain in the future.

The current situation

Android App - Activity Layout For the main layout, I quickly made a grid of ImageView elements using a root RelativeLayout, a ScrollView for scrolling motion, a vertical LinearLayout for the content and a horizontal LinearLayout for each “row”.  You might be thinking – “why the hell would you do that” to which I would reply -I have no idea. It was my first dumb idea, I didn’t plan anything out so the first thing that worked stayed. Due to this lack of foresight (fail to prepare, prepare to fail) the process of adding a new sound-clip is therefore quite tedious.

I need to first get the clip, then the image (a particular size for symmetry), then I must make a new ImageView and finally add it into either an empty slot in a LinearLayout, or make a whole new row. An ID must be assigned to the ImageView, and then the onClick XML attribute is set to a method called BRADBERRY in my Activity class. This method is, well as you can see, just awful. It is a large switch statement to set the correct Audio Resource ID to the single Sound object in the Activity. In short it’s not my best object-orientated example. And, again, the maintainability is poor. Once all of the previous work is completed, I now need to add a new switch case for any new sound clip, making sure I set the ID to the correct resource in the /res/raw directory. Not good.

    public void BRADBERRY(View v){
        
        switch (v.getId()){
            case R.id.clickwoohoo:
                sound.setSoundResourceId(R.raw.clickwoohoo);
                break;
            case R.id.imaperson:
                sound.setSoundResourceId(R.raw.imaperson);
                break;
            case R.id.brrrradberry:
                sound.setSoundResourceId(R.raw.brrrradberry);
                break;
            case R.id.ethanbradberry:
                sound.setSoundResourceId(R.raw.bradberry);
                break;
            //etc. etc.....

Where we’re headed

With the next few posts related to this app, I hope to work through the process of refactoring an Android app to be easily maintainable and much, much easier to update in the future. I will be going through replacing the current layout structure with something more suitable (did somebody say RecyclerView?) along with taking an OO approach to this app. You can grab the app from the Play Store to check out the app in its current form, stay tuned for updates!