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,

src/MainActivity.java

public static final int BLACKJACK = 0;
private final Map<Integer,Class<?>> mIntentMap = new HashMap<Integer,Class<?>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mIntentMap.put(BLACKJACK, BlackjackActivity.class);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.games_array)));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Class<?> cls = mIntentMap.get(position);
if (null != cls) {
Intent intent = new Intent(CasinoStrategyActivity.this, cls);
if (null != intent) startActivity(intent);
}
}
});
}

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:

  1. Add a new Activity class to the src folder
  2. Register that new Activity in AndroidManifest.xml
  3. Add a new <Integer, Class> pair to the map in MainActivity.java
  4. Add a new string to games_array

Here are some of the interesting parts:

src/CrapsActivity.java

public class CrapsActivity extends GameActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_view);
setText(R.id.text_view, R.string.craps);
setText(R.id.text_view_title, R.string.craps_title);
}
}

res/layout/text_view.xml

<TextView
android:id="@+id/text_view_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp" />
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />

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.

Stay up to date

Get notified when I publish. Unsubscribe at any time.