Android Card Games – the card drawing app

In one of the emails we were asked by Ching to write this code. This application is a sample “Draw a card” application. It allows you to draw a card of a card deck. In our example the deck contains only clubs. You click the button and the random card is displayed. Our application looks like this.

ITCuties - Android card games - Card Drawing App

ITCuties – Android card games – Card Drawing App

The card pictures (taken from ardisoft.net) are the application resources, so they must be copied to the res/drawable-* directories. aa.png to ak.png and back.png are the clubs card images.

ITCuties - Android card games - Card Drawing App - Project structure - drawable resources

ITCuties – Android card games – Card Drawing App – Project structure – drawable resources

If you want to find out how to quickly get started with Android development follow our tutorial – How To Setup Android Development Environment.

Let’s see how this application is coded.

activity_card_draw.xml

This example’s layout consist of the Button, which is clicked to draw a new card, and an ImageView component which is used to display the card.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CardDrawActivity" >

    <Button
        android:id="@+id/drawButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/draw" />

    <ImageView
        android:id="@+id/cardImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/back" />

</RelativeLayout>

CardDrawActivity.java

This Activity displays activity_card_draw layout. It has a final static Random attribute used to generate random numbers and a cardDeck attribute, which is an int[] table containing card resource ids. This Activity implements the OnClickListener interface so that we can set this class as the button’s onClickListener. In the onClick method, the drawCard method is called where a random card resource id is taken from the cardDeck table and set as the ImageView‘s image resource.


package com.example.itccarddrawingapp;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class CardDrawActivity extends Activity implements OnClickListener {

	// UI components
	private Button drawButton;
	private ImageView cardImage;
	
	// Random object
	private final static Random random = new Random();
	
	// The card deck
	private final static int[] cardDeck = new int[] {	R.drawable.aa,	// A card resource
														R.drawable.a2,	// 2 card resource
														R.drawable.a3,	// 3 card resource
														R.drawable.a4,	// 4 card resource
														R.drawable.a5,	// ...
														R.drawable.a6,
														R.drawable.a7,
														R.drawable.a8,
														R.drawable.a9,
														R.drawable.a10,
														R.drawable.aj,	// J card resource
														R.drawable.aq,	// Q card resource
														R.drawable.ak};	// K card resource
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_card_draw);
		
		drawButton 	= (Button)findViewById(R.id.drawButton);
		cardImage	= (ImageView)findViewById(R.id.cardImage);
		
		drawButton.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		drawCard();
	}

	private void drawCard() {
		// Set the image resource to the one being taken randomly from the cardDeck resouces table.
		cardImage.setImageResource(cardDeck[random.nextInt(cardDeck.length)]);
		
	}
	
}


Download this sample code here.

This code is available on our GitHub repository as well.

2 Responses to "Android Card Games – the card drawing app"

  1. Sakar says:

    Hi I am Sakar. I am developing a simple card game having 52 playing cards with 3 card visible to each player. I m very thankful for providing this source code which helps me to develop card application in android.
    My app contains 3 card for each users and 17 maximum user can play this game.I working on single user now having simple confusion when viewing cards randomly.I have added 3 Imageviews adn modified your drawCard(cardImage); method to view several cards in drawable randomly when pressing button. like drawcard(card1,card2,card3);
    But when i click the button there is same card in ImageView object which is not the part of most of 52 play card game. i tried alot to avoid this bug but cannot able.please provide me the logic of viewing distinct card randomly.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Java by Example App is available at Google Play Store NOW