Skip to main content

First game in Android/Simple funny game in android/First program in android

First game in Android/sample program in android/simple android game program

By following the tutorials given in android basics, you can now develop a simple, but funny game. I think, you can now easily handle button and its click events. Ok, first create a new Android Project. As said earlier, first click on “New Project”, select “Android” and then fill out the form with suitable details.
First add an image of your favorite actor’s or actress ‘s photo in the drawable folder(Just copy the photo and right click on the drawable folder and click on “paste”). Then go to main.xml. Now create a layout and add four image views to the main.xml.

I have created a table layout and added four image views. Then make each button clickable as we did in our first application.  Then add your favorite actor/actress images as background.

<ImageView
            android:text=""
            android:id="@+id/image1"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:background="@drawable/pic"
            android:clickable="true"
            android:onClick="button1"
</ImageView>

Then select your activity class. To access the ImageView created in xml file to your activity class, you need to call findViewById method. Add argument “R.layout.idofimageview”.  

ImageView imageview=(ImageView)findViewById(R.layout.imageview1);

Similarly create instance of four ImageView created in main.xml. To make each image clickable, create methods and pass the instance of the class View
Here, the important part of the code comes. We need some random values to load the image dynamically. For that, put some pure java code to calculate some random number.

        int a=Math.random();
        int b=a*10;
        String num=b+"";

If you know any other method to do this, you can implement that code. .
To display the score create a dynamic text view and load the mark…

By integrating all the above code, we get 

GameProjectActivity.java


package game.project;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class GameProjectActivity extends Activity {
          double a,b;
          String num;
          ImageView imgone;
          int mark;
          ImageView imgtwo,imgthree,imgfour;
    /** Called when the activity is first created. */
    @Override
        public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         imgone=(ImageView)findViewById(R.id.image1);
         imgtwo=(ImageView)findViewById(R.id.image2);
         imgthree=(ImageView)findViewById(R.id.image3);
         imgfour=(ImageView)findViewById(R.id.image4);
         a=Math.random();
         b=a*10;
         num=b+"";
       
    }
    public void display()
    {
                TextView textView=new TextView(this);
                textView.setText("Your score 4..");
                setContentView(textView);
    }
    public void button1(View v)
    {
       
        if(num.startsWith("0") || num.startsWith("3") || num.startsWith("8"))
        {
                imgone.setImageResource(R.drawable.pic);
                mark=4;
                display();
        }     
        else{imgone.setImageResource(R.drawable.crazy);}
       
    }
    public void button2(View v)
    {
        if(num.startsWith("1") || num.startsWith("5") )
        {
                imgtwo.setImageResource(R.drawable.pic);
                mark=4;
                display();

        }else{imgtwo.setImageResource(R.drawable.crazy);}
    }
    public void button3(View v)
    {
        if(num.startsWith("2") || num.startsWith("4") || num.startsWith("6"))
        {
                imgthree.setImageResource(R.drawable.pic);
                mark=4;
                display();
        }else{imgthree.setImageResource(R.drawable.crazy);}
    }
    public void button4(View v)
    {
        if(num.startsWith("7") || num.startsWith("9"))
        {
                imgfour.setImageResource(R.drawable.pic);
                mark=4;
                display();}
        else{imgfour.setImageResource(R.drawable.crazy);}
        }
    }



main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
    android:id="@+id/textView1"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_height="wrap_content"
    android:text="Which one is orginal..?"
    android:layout_width="wrap_content">
    </TextView>
    <TableLayout
    android:id="@+id/tableLayout1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_marginLeft="30dip">
        <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dip"
        >
            <ImageView
            android:text=""
            android:id="@+id/image1"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:background="@drawable/pic"
            android:clickable="true"
            android:onClick="button1">
            </ImageView>
            <ImageView
            android:id="@+id/image2"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:layout_marginLeft="30dip"
            android:background="@drawable/pic"
            android:clickable="true"
            android:onClick="button2">
            </ImageView>
        </TableRow>


RESULT

Comments

Popular posts from this blog

Capture image without surface view as background service in android

Capture image without user knowledge in android/Capture image as background service in android/Take photo without preview in android/ Camera without preview in android  package com.example.picture; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Service; import android.content.Context; import android.content.Intent; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.media.AudioManager; import android.os.IBinder; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class CameraService extends Service {       //Camera variables       //a surface holder       private SurfaceHolder sHolder ;        //a variable to control the camera       private Camera mCamera ;       //the camera parameters       private Parameters parameters ;       /** Called when the activity is first created. */     @Override     p

Open front camera in android program

How to access front camera in android/get number of cameras in android/Program to open front camera in android Using the code explained below, we can easily find out number of cameras in your android device. Camera.CameraInfo cameraInfo = new Camera.CameraInfo();         Log. d ( "No of cameras" ,Camera. getNumberOfCameras ()+ "" );         for ( int camNo = 0; camNo < Camera. getNumberOfCameras (); camNo++) {             CameraInfo camInfo = new CameraInfo();             Camera. getCameraInfo (camNo, camInfo);                         if (camInfo. facing ==(Camera.CameraInfo. CAMERA_FACING_FRONT )) {                 mCamera = Camera. open (camNo);             }         }         if ( mCamera == null ) {            // no front-facing camera, use the first back-facing camera instead.            // you may instead wish to inform the user of an error here...               mCamera = Camera. open ();         } A sample program that take photo using front camer

Custom Spinner in Android

Custom Spinner in Android/Spinner using custom adapter/spinner with icon and text  In this tutorial, i'll show you to customizing spinner in different methods.  To change the normal display of spinner view, add background image using the code " android:background="@drawable/myspinner_background" in the xml. You can also make it customizable by setting up an xml file in drawable folder Custom display with icon in the spinner   main.xml <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"     android:orientation = "vertical"     android:layout_width = "fill_parent"     android:layout_height = "fill_parent"     >     < Spinner     android:drawSelectorOnTop = "true"     android:id = "@+id/spinner1"     android:layout_width = "match_parent"     android:layout_height = "wrap_content&