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 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.


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=""
    tools:context=".CardDrawActivity" >

        android:text="@string/draw" />

        android:src="@drawable/back" />


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.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.aj,	// J card resource
				,	// Q card resource
														R.drawable.ak};	// K card resource
	protected void onCreate(Bundle savedInstanceState) {
		drawButton 	= (Button)findViewById(;
		cardImage	= (ImageView)findViewById(;

	public void onClick(View v) {

	private void drawCard() {
		// Set the image resource to the one being taken randomly from the cardDeck resouces table.

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.


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>