Android Development: Casino Strategy v2.0
The first feature I wanted to add after I published v1.0 was the ability to change between Blackjack strategy cards by swiping rather than selecting from a menu. I found an example of a View Pager that was exactly what I needed. Modifying it to fit my app was trivial. Note: since I'm developing using API 7, I needed the SDK Tools in order to get the View Pager working.
My next idea was to add a "Share" button so if the app helped users win, they could tell their friends. It turned out to be much easier than I thought and this Stack Overflow answer explains how to add a Share Intent.
That took care of Blackjack, but since the app is called Casino Strategy, I figured it needed more casino games. I started by adding a new Activity with a list of games for the user to select. I wanted this new Activity to launch on start-up and and the answer lies in intent filters.
In AndroidManifest.xml
,
<activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity><activity android:name=".BlackjackActivity" android:label="@string/app_name" ></activity>
To switch to the other activities,
To populate the list adapter, I'm using a string array. With only one game it's overkill, but it will soon make sense.
In res/values/strings.xml
,
<string-array name="games_array"> <item>Blackjack</item></string-array>
My second favorite casino game is Craps so it was next. Rather than creating a fancy graphic, the optimal strategy can easily be expressed in text. Adding a new game is a few easy steps:
- Add a new Activity class to the src folder
- Register that new Activity in AndroidManifest.xml
- Add a new
<Integer, Class>
pair to the map in MainActivity.java - Add a new string to games_array
Here are some of the interesting parts:
Notice that CrapsActivity extends GameActivity instead of Activity. Since my Share Intent is used by all games, I moved it to a base class that Blackjack and Craps extend.
I added Roulette in an almost identical way as Craps.