Android App – Number Trivia


I was trying to come up with another app idea so I was browsing through mashape.com and found numbersapi.com. The creators allow it to be used by anyone. As always, with any app idea, I searched Google Play for an existing version and I did not find one. I decided to make a trivia app using the numbers api. I decided to increase my knowledge on how to use gradients instead of images on buttons.

Here’s how you define a gradient style in a design xml file. Then you just use it as a the button’s style:

<shape xmlns:android=”http://schemas.android.com/apk/res/android&#8221; android:padding=”16dp” android:shape=”rectangle” >
<corners android:radius=”6dp” />
<gradient android:angle=”270″ android:endColor=”@color/lightblue” android:startColor=”@color/darkblue” />
<stroke android:width=”2px” android:color=”@color/lightblue” />
</shape>

This gave the buttons a nice gradient, border, and rounded corners. It only took me about 10 hours to put the entire app together and that includes lots of interruptions throughout. I also included Amazon apps so I could submit the app towards earning a Kindle Fire HD for helping them beta test their new mobile ad API. Here’s the utility class I used to help insert the ads and rotate them at a set interval.


import android.app.Activity;
import android.util.*;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.amazon.device.ads.*;
import java.util.*;

public class AdUtil implements AdListener {

    public static final String APP_KEY = "app_key_goes_here";
    public static final boolean AD_TESTING = false;

    public static void loadAd(Activity activity, LinearLayout adContainer, int width, DisplayMetrics displayMetrics) {

        try {

            AdRegistration.enableLogging(false);
            AdRegistration.enableTesting(AD_TESTING);

            final AdLayout adView;

            // If you use height=wrap_content then you don't need this stuff.  I realized this afterwards.
            if(width >= 1024) {
                adView = new AdLayout(activity, AdSize.SIZE_1024x50);
            } else if(width >= 728) {
                ViewGroup.LayoutParams params = adContainer.getLayoutParams();
                params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, displayMetrics);
                adView = new AdLayout(activity, AdSize.SIZE_728x90);
            } else if(width >= 600) {
                ViewGroup.LayoutParams params = adContainer.getLayoutParams();
                params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, displayMetrics);
                adView = new AdLayout(activity, AdSize.SIZE_600x90);
            } else if(width >= 320) {
                adView = new AdLayout(activity, AdSize.SIZE_320x50);
            } else {
                adView = new AdLayout(activity, AdSize.SIZE_300x50);
            }

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            adContainer.addView(adView, lp);

            adView.setListener(new AdUtil());

            AdRegistration.setAppKey(APP_KEY);
            AdTargetingOptions adOptions = new AdTargetingOptions();
            adView.loadAd(adOptions);

            Timer timer = new Timer();
            class UpdateAdTask extends TimerTask {
                public void run() {
                    try {
                        AdTargetingOptions adOptions = new AdTargetingOptions();
                        adView.loadAd(adOptions);
                    } catch(Exception ex) {
                        Log.e("trivia", "exception", ex);
                    }
                }
            }

            TimerTask updateAd = new UpdateAdTask();
            timer.scheduleAtFixedRate(updateAd, 0, 35 * 1000);
        } catch (Exception e) {
            Log.e("trivia", "exception", e);
        }
    }
}

With this app, I also learned that you can not make network requests on the display thread. Android forces you to create an alternative request and asynchronously make requests. This is better anyway. The API is really fast so I didn’t put any loaders gifs in.

Here’s what it looks like:

Capture1 Capture3 Capture2

The app is available here:
Google Play
Amazon Marketplace

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s