Android Apps

Pokemon Team Builder

This project focused on building an application to use a JSON API to get data and display it to a user. The people involved in creating this application include Andrew Ekstedt, Trevor Hammock and Miles Young.

In particular, the Android app features that were used include multiple activities (in building a team, displaying a information about a Pokémon), a 3rd-party API (PokéAPI), lifecycle methods (ViewModel), SharedPreferences (list sort), SQLite (teams), a polished UI, and can launch the current Pokémon to have more information from the web.

Example

Weather App

This app was created to be able to work with the OpenWeatherMap API. However, since it was a school project it is in a private repository so it can not be copied. Below is some code snippits that were used in creating the project.

Canceling toasts

This code implements a method when clicking part of a text list to remove the toast message if displaying, and show a new toast method.

public class MainActivity extends AppCompatActivity implements WeatherAdapter.OnWeatherClickListener {
    private Toast toast;
    ...
    @Override
    public void onClickChange(int position) {
        if(toast!=null){ //if still open
            toast.cancel(); //cancel toast
        }
        toast = Toast.makeText(this, textList.get(position), Toast.LENGTH_LONG);
        toast.show();    //show toast
    }
}

public class adapter ... {
    private clickListner cL;
    ...
    class viewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        ...
        @Override
        public void onClick(View v) {
            cL.onClickChange(getAdapterPosition());  //get position
        }
    }
}

Network Requests

One easy way to do network requests is to use okhttp.

public class NetworkUtils {
    private static final OkHttpClient mHTTPClient = new OkHttpClient();
    public static String doHTTPGet(String url){
        Request request = new Request.Builder().url(url).build();
        Response response = mHTTPClient.newCall(request).execute();
    }
}

Json Formatting

Another easy program to use is gson to deseralize JSON into Java Objects.

public class JsonUtils {
    //for JSON parsing
    static class overall {
        listItem[] list;
    }
    static class listItem{
        String
        Int foo;
        otherClass[] bar; //collection of items
    }
    public static ArrayList<item> parseJSON(String JSONstr) {
        Gson gson = new Gson();
        overall results = gson.fromJson(JSONstr, overall.class);
        if (results != null && results.list != null) {
            ArrayList<ForecastItem> forecastItems = new ArrayList<>();
            SimpleDateFormat dateParser = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
            dateParser.setTimeZone(TimeZone.getTimeZone(TIME_ZONE));
            //loop to get each item
            for (listItem listItem : results.list) {
                Item item = new Item();
                item.barVal = listItem.bar[0];
            }
            return forecastItems;
        } else {
            return null;
        }
    }
}

Hidden API Key

For security reasons, it is good to hide an api key from an Android repo. This can be done by setting the key in gradle.properties as Text_Api=”<key>” and then adding the file to .gitignore.

Then in build.gradle in the buildTypes, add the values buildConfigField(‘String’, ‘ApiKey’, Text_Api) and resValue(‘string’, ‘api_key’, Text_Api). Then, this can be accessed with private final static String TEXT_APPID = BuildConfig.ApiKey;.